# Compute sum of digits in all numbers from 1 to n

Given a range from (1 to n), Our goal is to calculate the sum of all digits sum in every number. For example.

`````` Example 1
Input = 15
Output = 66

[(1)+(2)+(3)+(4)+(5)+(6)+(7)+(8)+(9)+
(1+0)+(1+1)+(1+2)+(1+3)+(1+4)+(1+5) = 66]

Example 2
Input = 29
Output = 165

[(1)+(2)+(3)+(4)+(5)+(6)+(7)+(8)+
(9)+(1+0)+(1+1)+(1+2)+(1+3)+(1+4)+
(1+5)+(1+6)+(1+7)+(1+8)+(1+9)+(2+0)+
(2+1)+(2+2)+(2+3)+(2+4)+(2+5)+(2+6)+
(2+7)+(2+8)+(2+9) = 165 ]``````

Here given code implementation process.

``````// Java program for
// Compute sum of digits in all numbers from 1 to n
class Example
{
// Sum of digits of a number
public int sumOfdigit(int num)
{
int sum = 0;
while (num > 0)
{
// Sum the last digit
sum += (num % 10);
// Remove last digit
num /= 10;
}
return sum;
}
public void digitSum(int n)
{
if (n < 1)
{
return;
}
int result = 0;
// Execute this loop from 1 to n
for (int i = 1; i <= n; ++i)
{
result += sumOfdigit(i);
}
// Display given range
System.out.println("\n Given (1 to " + n + ")");
// Display calculated result
System.out.println(" Result : " + result);
}
public static void main(String[] args)
{
/*
(1)+(2)+(3)+(4)+(5)+(6)+(7)+(8)+(9)+
(1+0)+(1+1)+(1+2)+(1+3)+(1+4)+(1+5)
= 66
*/
/*
(1)+(2)+(3)+(4)+(5)+(6)+(7)+(8)+
(9)+(1+0)+(1+1)+(1+2)+(1+3)+(1+4)+
(1+5)+(1+6)+(1+7)+(1+8)+(1+9)+(2+0)+
(2+1)+(2+2)+(2+3)+(2+4)+(2+5)+(2+6)+
(2+7)+(2+8)+(2+9) = 165
*/
}
}``````

#### Output

`````` Given (1 to 15)
Result : 66

Given (1 to 29)
Result : 165``````
``````// Include namespace system
using System;
// Csharp program for
// Compute sum of digits in all numbers from 1 to n
public class Example
{
// Sum of digits of a number
public int sumOfdigit(int num)
{
var sum = 0;
while (num > 0)
{
// Sum the last digit
sum += (num % 10);
// Remove last digit
num /= 10;
}
return sum;
}
public void digitSum(int n)
{
if (n < 1)
{
return;
}
var result = 0;
// Execute this loop from 1 to n
for (var i = 1; i <= n; ++i)
{
result += this.sumOfdigit(i);
}
// Display given range
Console.WriteLine("\n Given (1 to " + n.ToString() + ")");
// Display calculated result
Console.WriteLine(" Result : " + result.ToString());
}
public static void Main(String[] args)
{
/*
(1)+(2)+(3)+(4)+(5)+(6)+(7)+(8)+(9)+
(1+0)+(1+1)+(1+2)+(1+3)+(1+4)+(1+5)
= 66
*/
/*
(1)+(2)+(3)+(4)+(5)+(6)+(7)+(8)+
(9)+(1+0)+(1+1)+(1+2)+(1+3)+(1+4)+
(1+5)+(1+6)+(1+7)+(1+8)+(1+9)+(2+0)+
(2+1)+(2+2)+(2+3)+(2+4)+(2+5)+(2+6)+
(2+7)+(2+8)+(2+9) = 165
*/
}
}``````

#### Output

`````` Given (1 to 15)
Result : 66

Given (1 to 29)
Result : 165``````
``````// Include header file
#include <iostream>

using namespace std;
// C++ program for
// Compute sum of digits in all numbers from 1 to n
class Example
{
public:
// Sum of digits of a number
int sumOfdigit(int num)
{
int sum = 0;
while (num > 0)
{
// Sum the last digit
sum += (num % 10);
// Remove last digit
num /= 10;
}
return sum;
}
void digitSum(int n)
{
if (n < 1)
{
return;
}
int result = 0;
// Execute this loop from 1 to n
for (int i = 1; i <= n; ++i)
{
result += this->sumOfdigit(i);
}
// Display given range
cout << "\n Given (1 to " << n << ")" << endl;
// Display calculated result
cout << " Result : " << result << endl;
}
};
int main()
{
/*
(1)+(2)+(3)+(4)+(5)+(6)+(7)+(8)+(9)+
(1+0)+(1+1)+(1+2)+(1+3)+(1+4)+(1+5)
= 66
*/
/*
(1)+(2)+(3)+(4)+(5)+(6)+(7)+(8)+
(9)+(1+0)+(1+1)+(1+2)+(1+3)+(1+4)+
(1+5)+(1+6)+(1+7)+(1+8)+(1+9)+(2+0)+
(2+1)+(2+2)+(2+3)+(2+4)+(2+5)+(2+6)+
(2+7)+(2+8)+(2+9) = 165
*/
return 0;
}``````

#### Output

`````` Given (1 to 15)
Result : 66

Given (1 to 29)
Result : 165``````
``````#include <stdio.h>

// C++ program for
// Compute sum of digits in all numbers from 1 to n
// Sum of digits of a number
int sumOfdigit(int num)
{
int sum = 0;
while (num > 0)
{
// Sum the last digit
sum += (num % 10);
// Remove last digit
num = num / 10;
}
return sum;
}
void digitSum(int n)
{
if (n < 1)
{
return;
}
int result = 0;
// Execute this loop from 1 to n
for (int i = 1; i <= n; ++i)
{
result += sumOfdigit(i);
}
// Display given range
printf("\n Given (1 to %d) \n", n);
// Display calculated result
printf(" Result : %d", result);
}
int main()
{
/*
(1)+(2)+(3)+(4)+(5)+(6)+(7)+(8)+(9)+
(1+0)+(1+1)+(1+2)+(1+3)+(1+4)+(1+5)
= 66
*/
digitSum(15);
/*
(1)+(2)+(3)+(4)+(5)+(6)+(7)+(8)+
(9)+(1+0)+(1+1)+(1+2)+(1+3)+(1+4)+
(1+5)+(1+6)+(1+7)+(1+8)+(1+9)+(2+0)+
(2+1)+(2+2)+(2+3)+(2+4)+(2+5)+(2+6)+
(2+7)+(2+8)+(2+9) = 165
*/
digitSum(29);
return 0;
}``````

#### Output

`````` Given (1 to 15)
Result : 66
Given (1 to 29)
Result : 165``````
``````package main
import "fmt"
// Go program for
// Compute sum of digits in all numbers from 1 to n

// Sum of digits of a number
func sumOfdigit(num int) int {
var sum int = 0
for (num > 0) {
// Sum the last digit
sum += (num % 10)
// Remove last digit
num /= 10
}
return sum
}
func digitSum(n int) {
if n < 1 {
return
}
var result int = 0
// Execute this loop from 1 to n
for i := 1 ; i <= n ; i++ {
result += sumOfdigit(i)
}
// Display given range
fmt.Println("\n Given (1 to ", n, ")")
// Display calculated result
fmt.Println(" Result : ", result)
}
func main() {

/*
(1)+(2)+(3)+(4)+(5)+(6)+(7)+(8)+(9)+
(1+0)+(1+1)+(1+2)+(1+3)+(1+4)+(1+5)
= 66
*/
digitSum(15)
/*
(1)+(2)+(3)+(4)+(5)+(6)+(7)+(8)+
(9)+(1+0)+(1+1)+(1+2)+(1+3)+(1+4)+
(1+5)+(1+6)+(1+7)+(1+8)+(1+9)+(2+0)+
(2+1)+(2+2)+(2+3)+(2+4)+(2+5)+(2+6)+
(2+7)+(2+8)+(2+9) = 165
*/
digitSum(29)
}``````

#### Output

`````` Given (1 to 15)
Result : 66

Given (1 to 29)
Result : 165``````
``````<?php
// Php program for
// Compute sum of digits in all numbers from 1 to n
class Example
{
// Sum of digits of a number
public  function sumOfdigit(\$num)
{
\$sum = 0;
while (\$num > 0)
{
// Sum the last digit
\$sum += (\$num % 10);
// Remove last digit
\$num /= 10;
}
return \$sum;
}
public  function digitSum(\$n)
{
if (\$n < 1)
{
return;
}
\$result = 0;
// Execute this loop from 1 to n
for (\$i = 1; \$i <= \$n; ++\$i)
{
\$result += \$this->sumOfdigit(\$i);
}
// Display given range
printf("%s\n", "\n Given (1 to ".strval(\$n).
")");
// Display calculated result
printf("%s\n", " Result : ".strval(\$result));
}
public static
function main(\$args)
{
/*
(1)+(2)+(3)+(4)+(5)+(6)+(7)+(8)+(9)+
(1+0)+(1+1)+(1+2)+(1+3)+(1+4)+(1+5)
= 66
*/
/*
(1)+(2)+(3)+(4)+(5)+(6)+(7)+(8)+
(9)+(1+0)+(1+1)+(1+2)+(1+3)+(1+4)+
(1+5)+(1+6)+(1+7)+(1+8)+(1+9)+(2+0)+
(2+1)+(2+2)+(2+3)+(2+4)+(2+5)+(2+6)+
(2+7)+(2+8)+(2+9) = 165
*/
}
}
Example::main(array());``````

#### Output

`````` Given (1 to 15)
Result : 66

Given (1 to 29)
Result : 165``````
``````// Node JS program for
// Compute sum of digits in all numbers from 1 to n
class Example
{
// Sum of digits of a number
sumOfdigit(num)
{
var sum = 0;
while (num > 0)
{
// Sum the last digit
sum += (num % 10);
// Remove last digit
num = parseInt(num / 10);
}
return sum;
}
digitSum(n)
{
if (n < 1)
{
return;
}
var result = 0;
// Execute this loop from 1 to n
for (var i = 1; i <= n; ++i)
{
result += this.sumOfdigit(i);
}
// Display given range
console.log("\n Given (1 to " + n + ")");
// Display calculated result
console.log(" Result : " + result);
}
}

function main()
{
/*
(1)+(2)+(3)+(4)+(5)+(6)+(7)+(8)+(9)+
(1+0)+(1+1)+(1+2)+(1+3)+(1+4)+(1+5)
= 66
*/
/*
(1)+(2)+(3)+(4)+(5)+(6)+(7)+(8)+
(9)+(1+0)+(1+1)+(1+2)+(1+3)+(1+4)+
(1+5)+(1+6)+(1+7)+(1+8)+(1+9)+(2+0)+
(2+1)+(2+2)+(2+3)+(2+4)+(2+5)+(2+6)+
(2+7)+(2+8)+(2+9) = 165
*/
}
// Start program execution
main();``````

#### Output

`````` Given (1 to 15)
Result : 66

Given (1 to 29)
Result : 165``````
``````#  Python 3 program for
#  Compute sum of digits in all numbers from 1 to n
class Example :
#  Sum of digits of a number
def sumOfdigit(self, num) :
sum = 0
while (num > 0) :
#  Sum the last digit
sum += (num % 10)
#  Remove last digit
num = int(num / 10)

return sum

def digitSum(self, n) :
if (n < 1) :
return

result = 0
i = 1
#  Execute this loop from 1 to n
while (i <= n) :
result += self.sumOfdigit(i)
i += 1

#  Display given range
print("\n Given (1 to ", n ,")")
#  Display calculated result
print(" Result : ", result)

def main() :
#    (1)+(2)+(3)+(4)+(5)+(6)+(7)+(8)+(9)+
#    (1+0)+(1+1)+(1+2)+(1+3)+(1+4)+(1+5)
#    = 66
#    (1)+(2)+(3)+(4)+(5)+(6)+(7)+(8)+
#    (9)+(1+0)+(1+1)+(1+2)+(1+3)+(1+4)+
#    (1+5)+(1+6)+(1+7)+(1+8)+(1+9)+(2+0)+
#    (2+1)+(2+2)+(2+3)+(2+4)+(2+5)+(2+6)+
#    (2+7)+(2+8)+(2+9) = 165

if __name__ == "__main__": main()``````

#### Output

`````` Given (1 to  15 )
Result :  66

Given (1 to  29 )
Result :  165``````
``````#  Ruby program for
#  Compute sum of digits in all numbers from 1 to n
class Example
#  Sum of digits of a number
def sumOfdigit(num)
sum = 0
while (num > 0)
#  Sum the last digit
sum += (num % 10)
#  Remove last digit
num = num / 10
end

return sum
end

def digitSum(n)
if (n < 1)
return
end

result = 0
i = 1
#  Execute this loop from 1 to n
while (i <= n)
result += self.sumOfdigit(i)
i += 1
end

#  Display given range
print("\n Given (1 to ", n ,")", "\n")
#  Display calculated result
print(" Result : ", result, "\n")
end

end

def main()
#    (1)+(2)+(3)+(4)+(5)+(6)+(7)+(8)+(9)+
#    (1+0)+(1+1)+(1+2)+(1+3)+(1+4)+(1+5)
#    = 66
#    (1)+(2)+(3)+(4)+(5)+(6)+(7)+(8)+
#    (9)+(1+0)+(1+1)+(1+2)+(1+3)+(1+4)+
#    (1+5)+(1+6)+(1+7)+(1+8)+(1+9)+(2+0)+
#    (2+1)+(2+2)+(2+3)+(2+4)+(2+5)+(2+6)+
#    (2+7)+(2+8)+(2+9) = 165
end

main()``````

#### Output

`````` Given (1 to 15)
Result : 66

Given (1 to 29)
Result : 165
``````
``````// Scala program for
// Compute sum of digits in all numbers from 1 to n
class Example()
{
// Sum of digits of a number
def sumOfdigit(n: Int): Int = {
var num = n;
var sum: Int = 0;
while (num > 0)
{
// Sum the last digit
sum += (num % 10);
// Remove last digit
num = num / 10;
}
return sum;
}
def digitSum(n: Int): Unit = {
if (n < 1)
{
return;
}
var result: Int = 0;
var i: Int = 1;
// Execute this loop from 1 to n
while (i <= n)
{
result += sumOfdigit(i);
i += 1;
}
// Display given range
println("\n Given (1 to " + n + ")");
// Display calculated result
println(" Result : " + result);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Example = new Example();
/*
(1)+(2)+(3)+(4)+(5)+(6)+(7)+(8)+(9)+
(1+0)+(1+1)+(1+2)+(1+3)+(1+4)+(1+5)
= 66
*/
/*
(1)+(2)+(3)+(4)+(5)+(6)+(7)+(8)+
(9)+(1+0)+(1+1)+(1+2)+(1+3)+(1+4)+
(1+5)+(1+6)+(1+7)+(1+8)+(1+9)+(2+0)+
(2+1)+(2+2)+(2+3)+(2+4)+(2+5)+(2+6)+
(2+7)+(2+8)+(2+9) = 165
*/
}
}``````

#### Output

`````` Given (1 to 15)
Result : 66

Given (1 to 29)
Result : 165``````
``````// Swift 4 program for
// Compute sum of digits in all numbers from 1 to n
class Example
{
// Sum of digits of a number
func sumOfdigit(_ n: Int) -> Int
{
var num: Int = n;
var sum: Int = 0;
while (num > 0)
{
// Sum the last digit
sum += (num % 10);
// Remove last digit
num = num / 10;
}
return sum;
}
func digitSum(_ n: Int)
{
if (n < 1)
{
return;
}
var result: Int = 0;
var i: Int = 1;
// Execute this loop from 1 to n
while (i <= n)
{
result += self.sumOfdigit(i);
i += 1;
}
// Display given range
print("\n Given (1 to", n ,")");
// Display calculated result
print(" Result :", result);
}
}
func main()
{
/*
(1)+(2)+(3)+(4)+(5)+(6)+(7)+(8)+(9)+
(1+0)+(1+1)+(1+2)+(1+3)+(1+4)+(1+5)
= 66
*/
/*
(1)+(2)+(3)+(4)+(5)+(6)+(7)+(8)+
(9)+(1+0)+(1+1)+(1+2)+(1+3)+(1+4)+
(1+5)+(1+6)+(1+7)+(1+8)+(1+9)+(2+0)+
(2+1)+(2+2)+(2+3)+(2+4)+(2+5)+(2+6)+
(2+7)+(2+8)+(2+9) = 165
*/
}
main();``````

#### Output

`````` Given (1 to 15 )
Result : 66

Given (1 to 29 )
Result : 165``````
``````// Kotlin program for
// Compute sum of digits in all numbers from 1 to n
class Example
{
// Sum of digits of a number
fun sumOfdigit(n: Int): Int
{
var num: Int = n;
var sum: Int = 0;
while (num > 0)
{
// Sum the last digit
sum += (num % 10);
// Remove last digit
num = num / 10;
}
return sum;
}
fun digitSum(n: Int): Unit
{
if (n < 1)
{
return;
}
var result: Int = 0;
var i: Int = 1;
// Execute this loop from 1 to n
while (i <= n)
{
result += this.sumOfdigit(i);
i += 1;
}
// Display given range
println("\n Given (1 to " + n + ")");
// Display calculated result
println(" Result : " + result);
}
}
fun main(args: Array < String > ): Unit
{
/*
(1)+(2)+(3)+(4)+(5)+(6)+(7)+(8)+(9)+
(1+0)+(1+1)+(1+2)+(1+3)+(1+4)+(1+5)
= 66
*/
/*
(1)+(2)+(3)+(4)+(5)+(6)+(7)+(8)+
(9)+(1+0)+(1+1)+(1+2)+(1+3)+(1+4)+
(1+5)+(1+6)+(1+7)+(1+8)+(1+9)+(2+0)+
(2+1)+(2+2)+(2+3)+(2+4)+(2+5)+(2+6)+
(2+7)+(2+8)+(2+9) = 165
*/
}``````

#### Output

`````` Given (1 to 15)
Result : 66

Given (1 to 29)
Result : 165``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.