-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSumOfIntegers.java
More file actions
70 lines (54 loc) · 1.6 KB
/
Copy pathSumOfIntegers.java
File metadata and controls
70 lines (54 loc) · 1.6 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*Title: Sum of Integers
Description: Reads an integer between 0 and 1000, and sums all of the digits in an integer
Created by: Norald Alejo
Email: Norald.Alejo@gmail.com
Date: 8/27/2016
Version: 1.01
*/
/* Psuedocode
1. Asks user for an integer between 0 and 1000
2. Takes in user input
3. while number / 10 does not equal 0 ->
number / 10
sum + = number % 10
4. Prints sum of all digits in an integer
*/
import java.util.*;
public class SumOfIntegers{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int sum = 0;
char answer;
//Ask and takes in user input
System.out.println("Welcome! Please enter an integer between 0 and 1000.");
int num = input.nextInt();
while (true){
while (num < 0 || num > 1000){
System.out.println("Invalid! Please enter an integer between 0 and 1000.");
num = input.nextInt();
}
System.out.println("You entered: " + num);
//Finds and adds each digit
while(num / 10 != 0){
sum += (num % 10);
num /= 10;
}
sum += (num % 10);
//Prints out the digits
System.out.printf("The sum of digits is %d\n", sum);
//Ask user if they want to add another integer
System.out.println("Do you want to find the sum of another integer?");
answer = input.next().charAt(0);
//Breaks if user said y
if (answer != 'y'){
break;
}
//Ask user again for another integer
System.out.println("\n---------------------------------------------------------");
System.out.println("Please enter another integer.");
num = input.nextInt();
sum = 0;
}
System.out.println("See you later!");
}
}