Given an integer **N ≥ 1**, the task is to find the smallest and the largest sum of two **N** digit numbers.

**Examples:**

Input:N = 1

Output:

Largest = 18

Smallest = 0

Largest 1-digit number is 9 and 9 + 9 = 18

Smallest 1-digit number is 0 and 0 + 0 = 0

Input:N = 2

Output:

Largest = 198

Smallest = 20

**Approach:**

**For largest:**The answer will be**2 * (10**because the series of sum of two^{N}– 1)**n**digit numbers will go on like**2 * 9, 2 * 99, 2 * 999, …****For smallest:**- If
**N = 1**then the answer will be**0**. - If
**N > 1**then the answer will be**2 * (10**because the series of sum of two^{N – 1})**n**digit numbers will go on like**0, 20, 200, 2000, …**

- If

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the smallest sum ` `// of 2 n-digit numbers ` `int` `smallestSum(` `int` `n) ` `{ ` ` ` `if` `(n == 1) ` ` ` `return` `0; ` ` ` `return` `(2 * ` `pow` `(10, n - 1)); ` `} ` ` ` `// Function to return the largest sum ` `// of 2 n-digit numbers ` `int` `largestSum(` `int` `n) ` `{ ` ` ` `return` `(2 * (` `pow` `(10, n) - 1)); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 4; ` ` ` `cout << ` `"Largest = "` `<< largestSum(n) << endl; ` ` ` `cout << ` `"Smallest = "` `<< smallestSum(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG { ` ` ` ` ` `// Function to return the smallest sum ` ` ` `// of 2 n-digit numbers ` ` ` `static` `int` `smallestSum(` `int` `n) ` ` ` `{ ` ` ` `if` `(n == ` `1` `) ` ` ` `return` `0` `; ` ` ` `return` `(` `2` `* (` `int` `)Math.pow(` `10` `, n - ` `1` `)); ` ` ` `} ` ` ` ` ` `// Function to return the largest sum ` ` ` `// of 2 n-digit numbers ` ` ` `static` `int` `largestSum(` `int` `n) ` ` ` `{ ` ` ` `return` `(` `2` `* ((` `int` `)Math.pow(` `10` `, n) - ` `1` `)); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `n = ` `4` `; ` ` ` `System.out.println(` `"Largest = "` `+ largestSum(n)); ` ` ` `System.out.print(` `"Smallest = "` `+ smallestSum(n)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the smallest sum ` `# of 2 n-digit numbers ` `def` `smallestSum(n): ` ` ` ` ` `if` `(n ` `=` `=` `1` `): ` ` ` `return` `0` ` ` `return` `(` `2` `*` `pow` `(` `10` `, n ` `-` `1` `)) ` ` ` `# Function to return the largest sum ` `# of 2 n-digit numbers ` `def` `largestSum(n): ` ` ` `return` `(` `2` `*` `(` `pow` `(` `10` `, n) ` `-` `1` `)) ` ` ` `# Driver code ` `n ` `=` `4` `print` `(` `"Largest = "` `, largestSum(n)) ` `print` `(` `"Smallest = "` `, smallestSum(n)) ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` `class` `GFG { ` ` ` ` ` `// Function to return the smallest sum ` ` ` `// of 2 n-digit numbers ` ` ` `static` `int` `smallestSum(` `int` `n) ` ` ` `{ ` ` ` `if` `(n == 1) ` ` ` `return` `0; ` ` ` `return` `(2 * (` `int` `)Math.Pow(10, n - 1)); ` ` ` `} ` ` ` ` ` `// Function to return the largest sum ` ` ` `// of 2 n-digit numbers ` ` ` `static` `int` `largestSum(` `int` `n) ` ` ` `{ ` ` ` `return` `(2 * ((` `int` `)Math.Pow(10, n) - 1)); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 4; ` ` ` `Console.WriteLine(` `"Largest = "` `+ largestSum(n)); ` ` ` `Console.Write(` `"Smallest = "` `+ smallestSum(n)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `// Function to return the smallest sum ` `// of 2 n-digit numbers ` `function` `smallestSum(` `$n` `) ` `{ ` ` ` `if` `(` `$n` `== 1) ` ` ` `return` `0; ` ` ` `return` `(2 * pow(10, ` `$n` `- 1)); ` `} ` ` ` `// Function to return the largest sum ` `// of 2 n-digit numbers ` `function` `largestSum(` `$n` `) ` `{ ` ` ` `return` `2 * ( pow(10, ` `$n` `) - 1 ); ` `} ` ` ` `// Driver code ` `$n` `= 4; ` `echo` `"Largest = "` `. largestSum(` `$n` `) . ` `"\n"` `; ` `echo` `"Smallest = "` `. smallestSum(` `$n` `); ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

Largest = 19998 Smallest = 2000

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:

- Sum and product of k smallest and k largest prime numbers in the array
- Sum and product of k smallest and k largest composite numbers in the array
- Largest and Smallest N-digit Hexadecimal Numbers
- Largest and Smallest N-digit Octal Numbers
- Nth term of a sequence formed by sum of current term with product of its largest and smallest digit
- Smallest and Largest N-digit number starting and ending with N
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Split the number into N parts such that difference between the smallest and the largest part is minimum
- Smallest and Largest N-digit perfect cubes
- Smallest and Largest Palindrome with N Digits
- Smallest and Largest N-digit perfect squares
- Largest and smallest digit of a number
- Queries for the smallest and the largest prime number of given digit
- Smallest string without any multiplication sign that represents the product of two given numbers
- Largest palindrome which is product of two n-digit numbers
- Largest of two distinct numbers without using any conditional statements or operators
- Largest palindrome which is product of two N-digit numbers : Set 2
- Form smallest number using indices of numbers chosen from Array with sum less than S
- Sum of largest divisor of numbers upto N not divisible by given prime number P
- Largest number less than N with digit sum greater than the digit sum of N

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.