-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsumOfDigits.java
More file actions
37 lines (28 loc) · 1.11 KB
/
sumOfDigits.java
File metadata and controls
37 lines (28 loc) · 1.11 KB
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
/*
* Josh Bartlett
* Purpose: To add the sum of the digits
* April 20, 2019
* Bellevue University
* sumOfDigits.java
*/
import java.util.Scanner;
public class sumOfDigits {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
// ask user for input, declare inputNum variable,
// add input to variable inputNum
System.out.print("Enter a number between 0 an 1000: ");
int inputNum = input.nextInt();
// declares variable a, adds ones digit to variable a
int a = inputNum % 10;
// declares variable b, adds tens digit to variable b
int b = (inputNum / 10) % 10;
// declares variable c, adds hundreds digit to variable c
int c = (inputNum / 100) % 10;
// declares variable finalNum, adds all digits together,
// adds that sum to variable finalNum
int finalNum = a + b + c;
// display result to output
System.out.println("The sum of the digits is " + finalNum);
}
}