Given an integer **N**, the task is to find the count of natural octal numbers up to **N** digits.

Examples:

Input:N = 1

Output:7

Explanation:

1, 2, 3, 4, 5, 6, 7 are 1 digit Natural Octal numbers.

Input:N = 2

Output:63

Explanation:

There are a total of 56 two digit octal numbers and 7 one digit octal numbers. Therefore, 56 + 7 = 63.

**Approach:** On observing carefully, a geometric progression series is formed [ **7 56 448 3584 28672 229376…** ] with the first term being **7** and a common ratio of **8**.

Therefore,

Nth term = Number of Octal numbers of N digits = 7 * 8^{N - 1}

Finally, count of all octal numbers up to N digits can be found out by iterating a loop from 1 to N and calculating the sum of ith term using the above formula.

Below is the implementation of the above approach:

## C++

`// C++ program to find the count of ` `// natural octal numbers upto N digits ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the count of ` `// natural octal numbers upto N digits ` `int` `count(` `int` `N) ` `{ ` ` ` `int` `sum = 0; ` ` ` ` ` `// Loop to iterate from 1 to N ` ` ` `// and calculating number of ` ` ` `// octal numbers for every 'i'th digit. ` ` ` `for` `(` `int` `i = 1; i <= N; i++) { ` ` ` `sum += 7 * ` `pow` `(8, i - 1); ` ` ` `} ` ` ` `return` `sum; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `N = 4; ` ` ` `cout << count(N); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the count of ` `// natural octal numbers upto N digits ` ` ` `public` `class` `GFG { ` ` ` ` ` `// Function to return the count of ` ` ` `// natural octal numbers upto N digits ` ` ` `static` `int` `count(` `int` `N) ` ` ` `{ ` ` ` `int` `sum = ` `0` `; ` ` ` ` ` `// Loop to iterate from 1 to N ` ` ` `// and calculating number of ` ` ` `// octal numbers for every 'i'th digit. ` ` ` `for` `(` `int` `i = ` `1` `; i <= N; i++) { ` ` ` `sum += ` `7` `* Math.pow(` `8` `, i - ` `1` `); ` ` ` `} ` ` ` `return` `sum; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `N = ` `4` `; ` ` ` `System.out.println(count(N)); ` ` ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find the count of ` `# natural octal numbers upto N digits ` ` ` `# Function to return the count of ` `# natural octal numbers upto N digits ` `def` `count(N) : ` ` ` ` ` `sum` `=` `0` `; ` ` ` ` ` `# Loop to iterate from 1 to N ` ` ` `# and calculating number of ` ` ` `# octal numbers for every 'i'th digit. ` ` ` `for` `i ` `in` `range` `(N ` `+` `1` `) : ` ` ` `sum` `+` `=` `7` `*` `(` `8` `*` `*` `(i ` `-` `1` `)); ` ` ` ` ` `return` `int` `(` `sum` `); ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `N ` `=` `4` `; ` ` ` `print` `(count(N)); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the count of ` `// natural octal numbers upto N digits ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return the count of ` ` ` `// natural octal numbers upto N digits ` ` ` `static` `int` `count(` `int` `N) ` ` ` `{ ` ` ` `int` `sum = 0; ` ` ` ` ` `// Loop to iterate from 1 to N ` ` ` `// and calculating number of ` ` ` `// octal numbers for every 'i'th digit. ` ` ` `for` `(` `int` `i = 1; i <= N; i++) ` ` ` `{ ` ` ` `sum += (` `int` `)(7 * Math.Pow(8, i - 1)); ` ` ` `} ` ` ` `return` `sum; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` `int` `N = 4; ` ` ` `Console.WriteLine(count(N)); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

**Output:**

4095

**Time Complexity:** O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Count of numbers upto N having absolute difference of at most K between any two adjacent digits
- Count of numbers upto M divisible by given Prime Numbers
- Maximize count of equal numbers in Array of numbers upto N by replacing pairs with their sum
- Count numbers < = N whose difference with the count of primes upto them is > = K
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Largest Even and Odd N-digit numbers in Octal Number System
- Largest and Smallest N-digit Octal Numbers
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Count numbers upto N which are both perfect square and perfect cube
- Count of numbers upto M with GCD equals to K when paired with M
- Count of all values of N in [L, R] such that count of primes upto N is also prime
- Minimum digits to be removed to make either all digits or alternating digits same
- Check if number is palindrome or not in Octal
- Check if Decimal representation of an Octal number is divisible by 7
- Program to Convert Octal Number to Binary Number
- Largest N digit Octal number which is a Perfect square
- Check if an Octal number is Even or Odd
- Program to Convert Octal to Hexadecimal

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.