forked from sathishmepco/Java-Interview-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSumOfDigits.java
More file actions
38 lines (32 loc) · 737 Bytes
/
SumOfDigits.java
File metadata and controls
38 lines (32 loc) · 737 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
34
35
36
37
38
package com.java.numbers;
import java.util.Scanner;
//This program will calculate the sum of digits of a given number
/*
* say N = 153
* output is = 9 (1 + 5 + 3)
*
* say N = 1986
* output is = 24 (1 + 9 + 8 + 6)
*/
public class SumOfDigits {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter any number :: ");
int N = Integer.parseInt(scanner.nextLine().trim());
int tempN = N;
int sum = 0;
while( N > 0){
int lastDigit = N %10;
sum += lastDigit;
N = N /10;
}
System.out.println("Sum of digits of "+tempN+" is :: "+sum);
scanner.close();
}
}
/*
OUTPUT
Please enter any number ::
1986
Sum of digits of :: 1986 is :: 24
*/