-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSumOfDigits.java
More file actions
33 lines (28 loc) · 759 Bytes
/
SumOfDigits.java
File metadata and controls
33 lines (28 loc) · 759 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*
* Program to calculate the sum of digits of a number:
* Write a Java program to calculate the sum of digits of a given number.
*/
import java.util.Scanner;
public class SumOfDigits {
public static int findSumOfDigits(int n)
{
int sum=0;
int num =n;
int rem;
while(num>0)
{
rem = num%10;
sum += rem;
num = num/10;
}
return sum;
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number: ");
int n = sc.nextInt();
sc.close();
System.out.println("The sum of the digits of the entered number "+ n + " is : "+findSumOfDigits(n));
}
}