Compute sum of digits in all numbers from 1 to n
Last Updated :
26 Dec, 2024
Given a number n, the task is to find the sum of digits in all numbers from 1 to n.
Examples:
Input: n = 5
Output: 15
Explanation: Sum of digits in numbers from 1 to 5 = 15
Input: n = 12
Output: 51
Explanation: Sum of digits in numbers from 1 to 12 = 51
[Naive Approach] By Traversing Every Number - O(n*len(n)) Time and O(1) Space
The idea is to traverse every number x from 1 to n and compute the sum in x by traversing all its digits.
C++
// C++ program to compute sum of
// digits in numbers from 1 to n
#include<bits/stdc++.h>
using namespace std;
// A utility function to compute sum
// of digits in a given number x
int sumOfDigitsFrom1ToN(int x) {
int sum = 0;
while (x != 0) {
sum += x %10;
x = x /10;
}
return sum;
}
// Returns sum of all digits in
// numbers from 1 to n
int sumOfDigits(int n) {
int result = 0;
// One by one compute sum of digits
// in every number from 1 to n
for (int x = 1; x <= n; x++)
result += sumOfDigitsFrom1ToN(x);
return result;
}
int main() {
int n = 328;
cout << sumOfDigits(n);
return 0;
}
Java
// Java program to compute sum of
// digits in numbers from 1 to n
class GfG {
// A utility function to compute sum
// of digits in a given number x
static int sumOfDigitsFrom1ToN(int x) {
int sum = 0;
while (x != 0) {
sum += x % 10;
x = x / 10;
}
return sum;
}
// Returns sum of all digits in
// numbers from 1 to n
static int sumOfDigits(int n) {
int result = 0;
// One by one compute sum of digits
// in every number from 1 to n
for (int x = 1; x <= n; x++) {
result += sumOfDigitsFrom1ToN(x);
}
return result;
}
public static void main(String[] args) {
int n = 328;
System.out.println(sumOfDigits(n));
}
}
Python
# Python program to compute sum of
# digits in numbers from 1 to n
# A utility function to compute sum
# of digits in a given number x
def sumOfDigitsFrom1ToN(x):
sum = 0
while x != 0:
sum += x % 10
x = x // 10
return sum
# Returns sum of all digits in
# numbers from 1 to n
def sumOfDigits(n):
result = 0
# One by one compute sum of digits
# in every number from 1 to n
for x in range(1, n + 1):
result += sumOfDigitsFrom1ToN(x)
return result
if __name__ == "__main__":
n = 328
print(sumOfDigits(n))
C#
// C# program to compute sum of
// digits in numbers from 1 to n
using System;
class GfG {
// A utility function to compute sum
// of digits in a given number x
static int sumOfDigitsFrom1ToN(int x) {
int sum = 0;
while (x != 0) {
sum += x % 10;
x = x / 10;
}
return sum;
}
// Returns sum of all digits in
// numbers from 1 to n
static int sumOfDigits(int n) {
int result = 0;
// One by one compute sum of digits
// in every number from 1 to n
for (int x = 1; x <= n; x++) {
result += sumOfDigitsFrom1ToN(x);
}
return result;
}
static void Main(string[] args) {
int n = 328;
Console.WriteLine(sumOfDigits(n));
}
}
JavaScript
// JavaScript program to compute sum of
// digits in numbers from 1 to n
// A utility function to compute sum
// of digits in a given number x
function sumOfDigitsFrom1ToN(x) {
let sum = 0;
while (x != 0) {
sum += x % 10;
x = Math.floor(x / 10);
}
return sum;
}
// Returns sum of all digits in
// numbers from 1 to n
function sumOfDigits(n) {
let result = 0;
// One by one compute sum of digits
// in every number from 1 to n
for (let x = 1; x <= n; x++) {
result += sumOfDigitsFrom1ToN(x);
}
return result;
}
//driver code
let n = 328;
console.log(sumOfDigits(n));
Above is a naive solution. We can do it more efficiently by finding a pattern.
Let us take a few examples.
sum(9) = 1 + 2 + 3 + 4 ........... + 9
= 9*10/2
= 45
sum(99) = 45 + (10 + 45) + (20 + 45) + ..... (90 + 45)
= 45*10 + (10 + 20 + 30 ... 90)
= 45*10 + 10(1 + 2 + ... 9)
= 45*10 + 45*10
= sum(9)*10 + 45*10
sum(999) = sum(99)*10 + 45*100
In general, we can compute sum(10d- 1) using the below formula:
- sum(10d - 1) = sum(10d-1 - 1) * 10 + 45*(10d-1)
In the below implementation, the above formula is implemented using dynamic programming as there are overlapping subproblems.
Below is the complete algorithm to find for sum(n)
- Find number of digits minus one in n. Let this value be 'd'. For 328, d is 2.
- Compute sum of digits in numbers from 1 to 10d - 1. Let this sum be w. For 328, we compute sum of digits from 1 to 99 using above formula.
- Find Most significant digit (msd) in n. For 328, msd is 3.
- Overall sum is sum of following terms:
- Sum of digits in 1 to "msd * 10d - 1". For 328, sum of digits in numbers from 1 to 299. For 328, we compute 3*sum(99) + (1 + 2)*100. Note that sum of sum(299) is sum(99) + sum of digits from 100 to 199 + sum of digits from 200 to 299. Sum of 100 to 199 is sum(99) + 1*100 and sum of 299 is sum(99) + 2*100. In general, this sum can be computed as w*msd + (msd*(msd-1)/2)*10d
- Sum of digits in msd * 10d to n. For 328, sum of digits in 300 to 328. For 328, this sum is computed as 3*29 + recursive call "sum(28)". In general, this sum can be computed as msd * (n % (msd*10d) + 1) + sum(n % (10d))
Below is the implementation of the above algorithm.
C++
// C++ program to compute sum of
// digits in numbers from 1 to n
#include <bits/stdc++.h>
using namespace std;
// Function to computer sum of digits in numbers from 1 to n
// Comments use example of 328 to explain the code
int sumOfDigits(int n) {
// base case: if n<10 return sum of
// first n natural numbers
if (n < 10)
return n * (n + 1) / 2;
// d = number of digits minus one in n. For 328, d is 2
int d = log10(n);
// computing sum of digits from 1 to 10^d-1,
// d=1 a[0]=0;
// d=2 a[1]=sum of digit from 1 to 9 = 45
// d=3 a[2]=sum of digit from 1 to 99 = a[1]*10 + 45*10^1 = 900
// d=4 a[3]=sum of digit from 1 to 999 = a[2]*10 + 45*10^2 = 13500
vector<int> a(d + 1);
a[0] = 0, a[1] = 45;
for (int i = 2; i <= d; i++)
a[i] = a[i - 1] * 10 + 45 * ceil(pow(10, i - 1));
// computing 10^d
int p = ceil(pow(10, d));
// Most significant digit (msd) of n,
// For 328, msd is 3 which can be obtained using 328/100
int msd = n / p;
// EXPLANATION FOR FIRST and SECOND TERMS IN BELOW LINE OF CODE
// First two terms compute sum of digits from 1 to 299
// (sum of digits in range 1-99 stored in a[d]) +
// (sum of digits in range 100-199, can be calculated as 1*100 + a[d]
// (sum of digits in range 200-299, can be calculated as 2*100 + a[d]
// The above sum can be written as 3*a[d] + (1+2)*100
// EXPLANATION FOR THIRD AND FOURTH TERMS IN BELOW LINE OF CODE
// The last two terms compute sum of digits in number from 300 to 328
// The third term adds 3*29 to sum as digit 3 occurs in all numbers
// from 300 to 328
// The fourth term recursively calls for 28
return msd * a[d] + (msd * (msd - 1) / 2) * p +
msd * (1 + n % p) + sumOfDigits(n % p);
}
int main() {
int n = 328;
cout << sumOfDigits(n);
return 0;
}
Java
// Java program to compute sum of
// digits in numbers from 1 to n
import java.lang.Math;
class GfG {
// Function to compute sum of digits in numbers from 1 to n
// Comments use example of 328 to explain the code
static int sumOfDigits(int n) {
// base case: if n<10 return sum of
// first n natural numbers
if (n < 10)
return n * (n + 1) / 2;
// d = number of digits minus one in n. For 328, d is 2
int d = (int) Math.log10(n);
// computing sum of digits from 1 to 10^d-1,
// d=1 a[0]=0;
// d=2 a[1]=sum of digit from 1 to 9 = 45
// d=3 a[2]=sum of digit from 1 to 99 = a[1]*10 + 45*10^1 = 900
// d=4 a[3]=sum of digit from 1 to 999 = a[2]*10 + 45*10^2 = 13500
int[] a = new int[d + 1];
a[0] = 0;
a[1] = 45;
for (int i = 2; i <= d; i++) {
a[i] = a[i - 1] * 10 + 45 * (int) Math.ceil(Math.pow(10, i - 1));
}
// computing 10^d
int p = (int) Math.ceil(Math.pow(10, d));
// Most significant digit (msd) of n,
// For 328, msd is 3 which can be obtained using 328/100
int msd = n / p;
// EXPLANATION FOR FIRST and SECOND TERMS IN BELOW LINE OF CODE
// First two terms compute sum of digits from 1 to 299
// (sum of digits in range 1-99 stored in a[d]) +
// (sum of digits in range 100-199, can be calculated as 1*100 + a[d]
// (sum of digits in range 200-299, can be calculated as 2*100 + a[d]
// The above sum can be written as 3*a[d] + (1+2)*100
// EXPLANATION FOR THIRD AND FOURTH TERMS IN BELOW LINE OF CODE
// The last two terms compute sum of digits in number from 300 to 328
// The third term adds 3*29 to sum as digit 3 occurs in all numbers
// from 300 to 328
// The fourth term recursively calls for 28
return msd * a[d] + (msd * (msd - 1) / 2) * p +
msd * (1 + n % p) + sumOfDigits(n % p);
}
public static void main(String[] args) {
int n = 328;
System.out.println(sumOfDigits(n));
}
}
Python
# Python program to compute sum of
# digits in numbers from 1 to n
import math
# Function to compute sum of digits in numbers from 1 to n
# Comments use example of 328 to explain the code
def sumOfDigits(n):
# base case: if n<10 return sum of
# first n natural numbers
if n < 10:
return n * (n + 1) // 2
# d = number of digits minus one in n. For 328, d is 2
d = int(math.log10(n))
# computing sum of digits from 1 to 10^d-1,
# d=1 a[0]=0;
# d=2 a[1]=sum of digit from 1 to 9 = 45
# d=3 a[2]=sum of digit from 1 to 99 = a[1]*10 + 45*10^1 = 900
# d=4 a[3]=sum of digit from 1 to 999 = a[2]*10 + 45*10^2 = 13500
a = [0] * (d + 1)
a[0] = 0
a[1] = 45
for i in range(2, d + 1):
a[i] = a[i - 1] * 10 + 45 * math.ceil(10 ** (i - 1))
# computing 10^d
p = math.ceil(10 ** d)
# Most significant digit (msd) of n,
# For 328, msd is 3 which can be obtained using 328//100
msd = n // p
# EXPLANATION FOR FIRST and SECOND TERMS IN BELOW LINE OF CODE
# First two terms compute sum of digits from 1 to 299
# (sum of digits in range 1-99 stored in a[d]) +
# (sum of digits in range 100-199, can be calculated as 1*100 + a[d]
# (sum of digits in range 200-299, can be calculated as 2*100 + a[d]
# The above sum can be written as 3*a[d] + (1+2)*100
# EXPLANATION FOR THIRD AND FOURTH TERMS IN BELOW LINE OF CODE
# The last two terms compute sum of digits in number from 300 to 328
# The third term adds 3*29 to sum as digit 3 occurs in all numbers
# from 300 to 328
# The fourth term recursively calls for 28
return (msd * a[d] + (msd * (msd - 1) // 2) * p +
msd * (1 + n % p) + sumOfDigits(n % p))
if __name__ == "__main__":
n = 328
print(sumOfDigits(n))
C#
// C# program to compute sum of
// digits in numbers from 1 to n
using System;
class GfG {
// Function to compute sum of digits in numbers from 1 to n
// Comments use example of 328 to explain the code
static int sumOfDigits(int n) {
// base case: if n<10 return sum of
// first n natural numbers
if (n < 10)
return n * (n + 1) / 2;
// d = number of digits minus one in n. For 328, d is 2
int d = (int)Math.Log10(n);
// computing sum of digits from 1 to 10^d-1,
// d=1 a[0]=0;
// d=2 a[1]=sum of digit from 1 to 9 = 45
// d=3 a[2]=sum of digit from 1 to 99 = a[1]*10 + 45*10^1 = 900
// d=4 a[3]=sum of digit from 1 to 999 = a[2]*10 + 45*10^2 = 13500
int[] a = new int[d + 1];
a[0] = 0;
a[1] = 45;
for (int i = 2; i <= d; i++) {
a[i] = a[i - 1] * 10 + 45 * (int)Math.Ceiling(Math.Pow(10, i - 1));
}
// computing 10^d
int p = (int)Math.Ceiling(Math.Pow(10, d));
// Most significant digit (msd) of n,
// For 328, msd is 3 which can be obtained using 328/100
int msd = n / p;
// EXPLANATION FOR FIRST and SECOND TERMS IN BELOW LINE OF CODE
// First two terms compute sum of digits from 1 to 299
// (sum of digits in range 1-99 stored in a[d]) +
// (sum of digits in range 100-199, can be calculated as 1*100 + a[d]
// (sum of digits in range 200-299, can be calculated as 2*100 + a[d]
// The above sum can be written as 3*a[d] + (1+2)*100
// EXPLANATION FOR THIRD AND FOURTH TERMS IN BELOW LINE OF CODE
// The last two terms compute sum of digits in number from 300 to 328
// The third term adds 3*29 to sum as digit 3 occurs in all numbers
// from 300 to 328
// The fourth term recursively calls for 28
return msd * a[d] + (msd * (msd - 1) / 2) * p +
msd * (1 + n % p) + sumOfDigits(n % p);
}
static void Main(string[] args) {
int n = 328;
Console.WriteLine(sumOfDigits(n));
}
}
JavaScript
// JavaScript program to compute sum of
// digits in numbers from 1 to n
// Function to compute sum of digits in numbers from 1 to n
// Comments use example of 328 to explain the code
function sumOfDigits(n) {
// base case: if n<10 return sum of
// first n natural numbers
if (n < 10)
return Math.floor(n * (n + 1) / 2);
// d = number of digits minus one in n. For 328, d is 2
let d = Math.floor(Math.log10(n));
// computing sum of digits from 1 to 10^d-1,
// d=1 a[0]=0;
// d=2 a[1]=sum of digit from 1 to 9 = 45
// d=3 a[2]=sum of digit from 1 to 99 = a[1]*10 + 45*10^1 = 900
// d=4 a[3]=sum of digit from 1 to 999 = a[2]*10 + 45*10^2 = 13500
let a = new Array(d + 1).fill(0);
a[0] = 0;
a[1] = 45;
for (let i = 2; i <= d; i++) {
a[i] = a[i - 1] * 10 + 45 * Math.ceil(Math.pow(10, i - 1));
}
// computing 10^d
let p = Math.ceil(Math.pow(10, d));
// Most significant digit (msd) of n,
// For 328, msd is 3 which can be obtained using 328/100
let msd = Math.floor(n / p);
// EXPLANATION FOR FIRST and SECOND TERMS IN BELOW LINE OF CODE
// First two terms compute sum of digits from 1 to 299
// (sum of digits in range 1-99 stored in a[d]) +
// (sum of digits in range 100-199, can be calculated as 1*100 + a[d]
// (sum of digits in range 200-299, can be calculated as 2*100 + a[d]
// The above sum can be written as 3*a[d] + (1+2)*100
// EXPLANATION FOR THIRD AND FOURTH TERMS IN BELOW LINE OF CODE
// The last two terms compute sum of digits in number from 300 to 328
// The third term adds 3*29 to sum as digit 3 occurs in all numbers
// from 300 to 328
// The fourth term recursively calls for 28
return msd * a[d] + (msd * (msd - 1) / 2) * p +
msd * (1 + n % p) + sumOfDigits(n % p);
}
//driver code
let n = 328;
console.log(sumOfDigits(n));
The efficient algorithm has one more advantage that we need to compute the array 'a[]' only once even when we are given multiple inputs.
The above implementation takes O(d2) time as each recursive call calculates dp[] array once again. The first call takes O(d), the second call takes O(d-1), the third call O(d-2), and so on. We don't need to recalculate dp[] array in each recursive call. Below is the modified implementation which works in O(d) time. Where d is a number of digits in the input number.
C++
// C++ program to compute sum of digits
// in numbers from 1 to n
#include <bits/stdc++.h>
using namespace std;
int sumOfDigitsFrom1ToNUtil(int n, vector<int> &a) {
if (n < 10)
return (n * (n + 1) / 2);
int d = (int)(log10(n));
int p = (int)(ceil(pow(10, d)));
int msd = n / p;
return (msd * a[d] + (msd * (msd - 1) / 2) * p
+ msd * (1 + n % p) + sumOfDigitsFrom1ToNUtil(n % p, a));
}
// Function to computer sum of digits in
// numbers from 1 to n
int sumOfDigits(int n) {
int d = max(((int)(log10(n))), 1);
vector<int> a(d + 1);
a[0] = 0;
a[1] = 45;
for (int i = 2; i <= d; i++)
a[i] = a[i - 1] * 10 + 45 * (int)(ceil(pow(10, i - 1)));
return sumOfDigitsFrom1ToNUtil(n, a);
}
int main() {
int n = 328;
cout << sumOfDigits(n);
}
Java
// Java program to compute sum of digits
// in numbers from 1 to n
import java.util.*;
class GfG {
static int sumOfDigitsFrom1ToNUtil(int n, int[] a) {
if (n < 10)
return (n * (n + 1) / 2);
int d = (int)(Math.log10(n));
int p = (int)(Math.ceil(Math.pow(10, d)));
int msd = n / p;
return (msd * a[d] + (msd * (msd - 1) / 2) * p
+ msd * (1 + n % p)
+ sumOfDigitsFrom1ToNUtil(n % p, a));
}
// Function to compute sum of digits in
// numbers from 1 to n
static int sumOfDigits(int n) {
int d = Math.max((int)(Math.log10(n)), 1);
int[] a = new int[d + 1];
a[0] = 0;
a[1] = 45;
for (int i = 2; i <= d; i++)
a[i] = a[i - 1] * 10
+ 45
* (int)(Math.ceil(
Math.pow(10, i - 1)));
return sumOfDigitsFrom1ToNUtil(n, a);
}
public static void main(String[] args) {
int n = 328;
System.out.println(sumOfDigits(n));
}
}
Python
# Python program to compute sum of digits
# in numbers from 1 to n
import math
def sumOfDigitsFrom1ToNUtil(n, a):
if n < 10:
return (n * (n + 1) // 2)
d = int(math.log10(n))
p = int(math.ceil(math.pow(10, d)))
msd = n // p
return (msd * a[d] + (msd * (msd - 1) // 2) * p +
msd * (1 + n % p) +
sumOfDigitsFrom1ToNUtil(n % p, a))
# Function to compute sum of digits in
# numbers from 1 to n
def sumOfDigits(n):
d = max(int(math.log10(n)), 1)
a = [0] * (d + 1)
a[0] = 0; a[1] = 45
for i in range(2, d + 1):
a[i] = a[i - 1] * 10 + 45 * \
int(math.ceil(math.pow(10, i - 1)))
return sumOfDigitsFrom1ToNUtil(n, a)
if __name__ == "__main__":
n = 328
print(sumOfDigits(n))
C#
// C# program to compute sum of digits
// in numbers from 1 to n
using System;
class GfG {
static int sumOfDigitsFrom1ToNUtil(int n, int[] a) {
if (n < 10)
return (n * (n + 1) / 2);
int d = (int)Math.Log10(n);
int p = (int)Math.Ceiling(Math.Pow(10, d));
int msd = n / p;
return (msd * a[d] + (msd * (msd - 1) / 2) * p
+ msd * (1 + n % p)
+ sumOfDigitsFrom1ToNUtil(n % p, a));
}
// Function to compute sum of digits in
// numbers from 1 to n
static int sumOfDigits(int n) {
int d = Math.Max((int)Math.Log10(n), 1);
int[] a = new int[d + 1];
a[0] = 0;
a[1] = 45;
for (int i = 2; i <= d; i++)
a[i] = a[i - 1] * 10
+ 45
* (int)Math.Ceiling(
Math.Pow(10, i - 1));
return sumOfDigitsFrom1ToNUtil(n, a);
}
static void Main(string[] args) {
int n = 328;
Console.WriteLine(sumOfDigits(n));
}
}
JavaScript
// JavaScript program to compute sum of digits
// in numbers from 1 to n
function sumOfDigitsFrom1ToNUtil(n, a) {
if (n < 10)
return Math.floor(n * (n + 1) / 2);
let d = Math.floor(Math.log10(n));
let p = Math.ceil(Math.pow(10, d));
let msd = Math.floor(n / p);
return (msd * a[d] + (msd * (msd - 1) / 2) * p +
msd * (1 + n % p) +
sumOfDigitsFrom1ToNUtil(n % p, a));
}
// Function to compute sum of digits in
// numbers from 1 to n
function sumOfDigits(n) {
let d = Math.max(Math.floor(Math.log10(n)), 1);
let a = new Array(d + 1).fill(0);
a[0] = 0; a[1] = 45;
for (let i = 2; i <= d; i++)
a[i] = a[i - 1] * 10 + 45 *
Math.ceil(Math.pow(10, i - 1));
return sumOfDigitsFrom1ToNUtil(n, a);
}
let n = 328;
console.log(sumOfDigits(n));
Similar Reads
Count total number of digits from 1 to n
Given a number n, count the total number of digits required to write all numbers from 1 to n. Examples: Input : 13 Output : 17 Numbers from 1 to 13 are 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13. So 1 - 9 require 9 digits and 10 - 13 require 8 digits. Hence 9 + 8 = 17 digits are required. Input : 4 O
8 min read
Count of numbers with all digits same in a given range
Given two integers L and R denoting the starting and end values of a range, the task is to count all numbers in that range whose all digit are same, like 1, 22, 444, 3333, etc.Example: Input: L = 12, R = 68 Output: 5 Explanation: { 22, 33, 44, 55, 66} are the numbers with same digits in the given ra
9 min read
Count numbers from 1 to n that have 4 as a digit
Given an integer n. The task is to count all numbers from 1 to n that contain the digit 4 at least once in their representation.Examples: Input: n = 20Output: 2Explanation: The numbers 4 and 14 contain the digit 4 at least once.Input: n = 50Output: 14Explanation: The numbers 4, 14, 24, 34, and 40-49
12 min read
Count of all N digit numbers such that num + Rev(num) = 10^N - 1
Given an integer N, the task is to find the count of all N digit numbers such that num + Rev(num) = 10N - 1Examples: Input: N = 2 Output: 9 All possible numbers are 18 + 81 = 99 27 + 72 = 99 36 + 45 = 99 45 + 54 = 99 54 + 45 = 99 63 + 54 = 99 72 + 27 = 99 81 + 18 = 99 90 + 09 = 99 Input: N = 4 Outpu
4 min read
Count of unique digits in a given number N
Given a number N, the task is to count the number of unique digits in the given number. Examples: Input: N = 22342 Output: 2 Explanation:The digits 3 and 4 occurs only once. Hence, the output is 2. Input: N = 99677 Output: 1Explanation:The digit 6 occurs only once. Hence, the output is 1. Naive Appr
6 min read
Count of N-digit numbers with all distinct digits
Given an integer N, the task is to find the count of N-digit numbers with all distinct digits.Examples: Input: N = 1 Output: 9 1, 2, 3, 4, 5, 6, 7, 8 and 9 are the 1-digit numbers with all distinct digits.Input: N = 3 Output: 648 Naive Approach: If N > 10 i.e. there will be atleast one digit whic
7 min read
Count numbers from a given range whose product of digits is K
Given three positive integers L, R and K, the task is to count the numbers in the range [L, R] whose product of digits is equal to K Examples: Input: L = 1, R = 130, K = 14Output: 3Explanation: Numbers in the range [1, 100] whose sum of digits is K(= 14) are: 27 => 2 * 7 = 14 72 => 7 * 2 = 14
15+ min read
Count of Octal numbers upto N digits
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 nu
4 min read
Count of repeating digits in a given Number
Given a number N, the task is to count the total number of repeating digits in the given number. Examples: Input: N = 99677 Output: 2Explanation:In the given number only 9 and 7 are repeating, hence the answer is 2. Input: N = 12Output: 0Explanation:In the given number no digits are repeating, hence
12 min read
Find sum of digits in factorial of a number
Given a number n, write code to find the sum of digits in the factorial of the number. Given n ⤠5000 Examples: Input : 10 Output : 27 Input : 100 Output : 648 Recommended PracticeSum of digits in factorial of a numberTry It!It is not possible to store a number as large as 100! under some data types
11 min read