# First N terms whose sum of digits is a multiple of 10

Given an integer N, the task is to print the first N terms whose sum of digits is a multiple of 10. First few terms of the series are 19, 28, 37, 46, 55, …

Examples:

Input: N = 5
Output: 19 28 37 46 55

Input: N = 10
Output: 19 28 37 46 55 64 73 82 91 109

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

Approach: It can be observed that to get the Nth term of the required series, find the sum of the digits of N. If the sum is already a multiple of 10 then append digit 0 in the end of N else append the minimum possible digit in the end such that the new sum of digits is a multiple of 10.

For example, to get the 19th term, since the sum of digits is already a multiple of 10 then append 0 and 190 is the 19th term of the series.
For N = 5, the minimum digit that can be appended to make the sum of digits as a multiple of 10 is 5 and 55 is the 5th term of the series.

Below is the implementation of the above approach:

## C++

 `#include ` `using` `namespace` `std; ` ` `  `const` `int` `TEN = 10; ` ` `  `// Function to return the ` `// sum of digits of n ` `int` `digitSum(``int` `n) ` `{ ` `    ``int` `sum = 0; ` `    ``while` `(n > 0) { ` ` `  `        ``// Add last digit to the sum ` `        ``sum += n % TEN; ` ` `  `        ``// Remove last digit ` `        ``n /= TEN; ` `    ``} ` ` `  `    ``return` `sum; ` `} ` ` `  `// Function to return the nth term ` `// of the required series ` `int` `getNthTerm(``int` `n) ` `{ ` `    ``int` `sum = digitSum(n); ` ` `  `    ``// If sum of digit is already ` `    ``// a multiple of 10 then append 0 ` `    ``if` `(sum % TEN == 0) ` `        ``return` `(n * TEN); ` ` `  `    ``// To store the minimum digit ` `    ``// that must be appended ` `    ``int` `extra = TEN - (sum % TEN); ` ` `  `    ``// Return n after appending ` `    ``// the required digit ` `    ``return` `((n * TEN) + extra); ` `} ` ` `  `// Function to print the first n terms ` `// of the requried series ` `void` `firstNTerms(``int` `n) ` `{ ` `    ``for` `(``int` `i = 1; i <= n; i++) ` `        ``cout << getNthTerm(i) << ``" "``; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `n = 10; ` ` `  `    ``firstNTerms(n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the above approach ` `class` `GFG  ` `{ ` `    ``final` `static` `int` `TEN = ``10``;  ` `     `  `    ``// Function to return the  ` `    ``// sum of digits of n  ` `    ``static` `int` `digitSum(``int` `n)  ` `    ``{  ` `        ``int` `sum = ``0``;  ` `        ``while` `(n > ``0``)  ` `        ``{  ` `     `  `            ``// Add last digit to the sum  ` `            ``sum += n % TEN;  ` `     `  `            ``// Remove last digit  ` `            ``n /= TEN;  ` `        ``}  ` `        ``return` `sum;  ` `    ``}  ` `     `  `    ``// Function to return the nth term  ` `    ``// of the required series  ` `    ``static` `int` `getNthTerm(``int` `n)  ` `    ``{  ` `        ``int` `sum = digitSum(n);  ` `     `  `        ``// If sum of digit is already  ` `        ``// a multiple of 10 then append 0  ` `        ``if` `(sum % TEN == ``0``)  ` `            ``return` `(n * TEN);  ` `     `  `        ``// To store the minimum digit  ` `        ``// that must be appended  ` `        ``int` `extra = TEN - (sum % TEN);  ` `     `  `        ``// Return n after appending  ` `        ``// the required digit  ` `        ``return` `((n * TEN) + extra);  ` `    ``}  ` `     `  `    ``// Function to print the first n terms  ` `    ``// of the requried series  ` `    ``static` `void` `firstNTerms(``int` `n)  ` `    ``{  ` `        ``for` `(``int` `i = ``1``; i <= n; i++)  ` `            ``System.out.print(getNthTerm(i) + ``" "``);  ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``public` `static` `void` `main (String[] args)  ` `    ``{  ` `        ``int` `n = ``10``;  ` `     `  `        ``firstNTerms(n);  ` `    ``}  ` `} ` ` `  `// This code is contributed by AnkitRai01 `

## Python3

 `# Python3 code for above implementation ` `TEN ``=` `10` ` `  `# Function to return the ` `# sum of digits of n ` `def` `digitSum(n): ` `    ``sum` `=` `0` `    ``while` `(n > ``0``): ` ` `  `        ``# Add last digit to the sum ` `        ``sum` `+``=` `n ``%` `TEN ` ` `  `        ``# Remove last digit ` `        ``n ``/``/``=` `TEN ` ` `  `    ``return` `sum` ` `  `# Function to return the nth term ` `# of the required series ` `def` `getNthTerm(n): ` `    ``sum` `=` `digitSum(n) ` ` `  `    ``# If sum of digit is already ` `    ``# a multiple of 10 then append 0 ` `    ``if` `(``sum` `%` `TEN ``=``=` `0``): ` `        ``return` `(n ``*` `TEN) ` ` `  `    ``# To store the minimum digit ` `    ``# that must be appended ` `    ``extra ``=` `TEN ``-` `(``sum` `%` `TEN) ` ` `  `    ``# Return n after appending ` `    ``# the required digit ` `    ``return` `((n ``*` `TEN) ``+` `extra) ` ` `  `# Function to print the first n terms ` `# of the requried series ` `def` `firstNTerms(n): ` `    ``for` `i ``in` `range``(``1``, n ``+` `1``): ` `        ``print``(getNthTerm(i), end ``=` `" "``) ` ` `  `# Driver code ` `n ``=` `10` ` `  `firstNTerms(n) ` ` `  `# This code is contributed by Mohit Kumar `

## C#

 `// C# Program to Find the Unique elements  ` `// in linked lists ` `using` `System; ` `     `  `class` `GFG  ` `{ ` `    ``readonly` `static` `int` `TEN = 10;  ` `     `  `    ``// Function to return the  ` `    ``// sum of digits of n  ` `    ``static` `int` `digitSum(``int` `n)  ` `    ``{  ` `        ``int` `sum = 0;  ` `        ``while` `(n > 0)  ` `        ``{  ` `     `  `            ``// Add last digit to the sum  ` `            ``sum += n % TEN;  ` `     `  `            ``// Remove last digit  ` `            ``n /= TEN;  ` `        ``}  ` `        ``return` `sum;  ` `    ``}  ` `     `  `    ``// Function to return the nth term  ` `    ``// of the required series  ` `    ``static` `int` `getNthTerm(``int` `n)  ` `    ``{  ` `        ``int` `sum = digitSum(n);  ` `     `  `        ``// If sum of digit is already  ` `        ``// a multiple of 10 then append 0  ` `        ``if` `(sum % TEN == 0)  ` `            ``return` `(n * TEN);  ` `     `  `        ``// To store the minimum digit  ` `        ``// that must be appended  ` `        ``int` `extra = TEN - (sum % TEN);  ` `     `  `        ``// Return n after appending  ` `        ``// the required digit  ` `        ``return` `((n * TEN) + extra);  ` `    ``}  ` `     `  `    ``// Function to print the first n terms  ` `    ``// of the requried series  ` `    ``static` `void` `firstNTerms(``int` `n)  ` `    ``{  ` `        ``for` `(``int` `i = 1; i <= n; i++)  ` `            ``Console.Write(getNthTerm(i) + ``" "``);  ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``public` `static` `void` `Main (String[] args)  ` `    ``{  ` `        ``int` `n = 10;  ` `     `  `        ``firstNTerms(n);  ` `    ``}  ` `} ` ` `  `// This code is contributed by 29AjayKumar `

Output:

```19 28 37 46 55 64 73 82 91 109
```

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.

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.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.