# Largest and Smallest N-digit Octal Numbers

Given an integer N, the task is to find the smallest and largest N-digit numbers in Octal Number System.

Examples:

Input: N = 4
Output:
Largest: 7777
Smallest: 1000

Input: N = 2
Output:
Largest: 77
Smallest: 10

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: The following steps can be followed to compute the required answer:

• Largest Number: To get the largest number, every digit of the number must be maximum. The maximum digit in the Octal number system is ‘7‘. Therefore:
```1 Digit Largest Number: '7'
2 Digit Largest Number: '77'
3 Digit Largest Number: '777'
.
.
.
N Digit Largest Number: '777....(N) times'
```
• Smallest Number: The smallest number in Octal number is ‘0‘. The idea is that the first digit needs to be as minimum as possible other than 0 which is ‘1’ and the remaining digits needs to be 0. Therefore:
```1 Digit Smallest Number: '1'
2 Digit Smallest Number: '10'
3 Digit Smallest Number: '100'
.
.
.
N Digit Smallest Number: '100....(N - 1) times'
```

Below is the implementation of the above approach:

## C++

 `// C++ program to find the largest ` `// and smallest N-digit numbers ` `// in Octal Number System ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to return the largest ` `// N-digit number in Octal ` `// Number System ` `string findLargest(``int` `N) ` `{ ` `    ``// Append '7' N times ` `    ``string largest = string(N, ``'7'``); ` ` `  `    ``return` `largest; ` `} ` ` `  `// Function to return the smallest ` `// N-digit number in Octal ` `// Number System ` `string findSmallest(``int` `N) ` `{ ` `    ``// Append '0' (N - 1) times to 1 ` `    ``string smallest ` `        ``= ``"1"` `          ``+ string((N - 1), ``'0'``); ` ` `  `    ``return` `smallest; ` `} ` ` `  `// Function to print the largest and ` `// smallest N-digit Octal number ` `void` `printLargestSmallest(``int` `N) ` `{ ` `    ``cout << ``"Largest: "` `         ``<< findLargest(N) << endl; ` `    ``cout << ``"Smallest: "` `         ``<< findSmallest(N) << endl; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `N = 4; ` ` `  `    ``// Function Call ` `    ``printLargestSmallest(N); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to find the largest ` `// and smallest N-digit numbers ` `// in Octal Number System ` `class` `GFG ` `{ ` `  `  `// Function to return the largest ` `// N-digit number in Octal ` `// Number System ` `static` `String findLargest(``int` `N) ` `{ ` `    ``// Append '7' N times ` `    ``String largest = strings(N, ``'7'``); ` `  `  `    ``return` `largest; ` `} ` `  `  `// Function to return the smallest ` `// N-digit number in Octal ` `// Number System ` `static` `String findSmallest(``int` `N) ` `{ ` `    ``// Append '0' (N - 1) times to 1 ` `    ``String smallest ` `        ``= ``"1"` `          ``+ strings((N - ``1``), ``'0'``); ` `  `  `    ``return` `smallest; ` `} ` `  `  `private` `static` `String strings(``int` `N, ``char` `c) { ` `    ``String temp =``""``; ` `    ``for``(``int` `i= ``0``; i < N; i++) { ` `        ``temp+=c; ` `    ``} ` `    ``return` `temp; ` `} ` ` `  `// Function to print the largest and ` `// smallest N-digit Octal number ` `static` `void` `printLargestSmallest(``int` `N) ` `{ ` `    ``System.out.print(``"Largest: "` `         ``+ findLargest(N) +``"\n"``); ` `    ``System.out.print(``"Smallest: "` `         ``+ findSmallest(N) +``"\n"``); ` `} ` `  `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `N = ``4``; ` `  `  `    ``// Function Call ` `    ``printLargestSmallest(N);  ` `} ` `} ` ` `  `// This code is contributed by 29AjayKumar `

## Python3

 `# Python program to find the largest ` `# and smallest N-digit numbers ` `# in Octal Number System ` ` `  `# Function to return the largest ` `# N-digit number in Octal ` `# Number System ` `def` `findLargest(N): ` ` `  `    ``# Append '7' N times ` `    ``largest ``=` `strings(N, ``'7'``); ` `    ``return` `largest; ` ` `  `# Function to return the smallest ` `# N-digit number in Octal ` `# Number System ` `def` `findSmallest(N): ` ` `  `    ``# Append '0' (N - 1) times to 1 ` `    ``smallest ``=` `"1"` `+` `strings((N ``-` `1``), ``'0'``); ` `    ``return` `smallest; ` ` `  `def` `strings(N, c): ` `    ``temp ``=` `""; ` `    ``for` `i ``in` `range``(N): ` `        ``temp ``+``=` `c; ` `    ``return` `temp; ` ` `  `# Function to prthe largest and ` `# smallest N-digit Octal number ` `def` `printLargestSmallest(N): ` `    ``print``(``"Largest: "``,findLargest(N)); ` `    ``print``(``"Smallest: "``,findSmallest(N)); ` ` `  `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``N ``=` `4``; ` ` `  `    ``# Function Call ` `    ``printLargestSmallest(N); ` ` `  `# This code is contributed by sapnasingh4991 `

## C#

 `// C# program to find the largest ` `// and smallest N-digit numbers ` `// in Octal Number System ` `using` `System; ` ` `  `class` `GFG ` `{ ` `   `  `// Function to return the largest ` `// N-digit number in Octal ` `// Number System ` `static` `String findLargest(``int` `N) ` `{ ` `    ``// Append '7' N times ` `    ``String largest = strings(N, ``'7'``); ` `   `  `    ``return` `largest; ` `} ` `   `  `// Function to return the smallest ` `// N-digit number in Octal ` `// Number System ` `static` `String findSmallest(``int` `N) ` `{ ` `    ``// Append '0' (N - 1) times to 1 ` `    ``String smallest ` `        ``= ``"1"` `          ``+ strings((N - 1), ``'0'``); ` `   `  `    ``return` `smallest; ` `} ` `   `  `private` `static` `String strings(``int` `N, ``char` `c) { ` `    ``String temp =``""``; ` `    ``for``(``int` `i= 0; i < N; i++) { ` `        ``temp+=c; ` `    ``} ` `    ``return` `temp; ` `} ` `  `  `// Function to print the largest and ` `// smallest N-digit Octal number ` `static` `void` `printLargestSmallest(``int` `N) ` `{ ` `    ``Console.Write(``"Largest: "` `         ``+ findLargest(N) +``"\n"``); ` `    ``Console.Write(``"Smallest: "` `         ``+ findSmallest(N) +``"\n"``); ` `} ` `   `  `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` `    ``int` `N = 4; ` `   `  `    ``// Function Call ` `    ``printLargestSmallest(N);  ` `} ` `} ` ` `  `// This code is contributed by PrinciRaj1992 `

Output:

```Largest: 7777
Smallest: 1000
```

Time Complexity: O(N) where N is the length of the string.

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.