-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloops.java
More file actions
52 lines (47 loc) · 1.39 KB
/
loops.java
File metadata and controls
52 lines (47 loc) · 1.39 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
package basic;
import java.util.Scanner;
public class loops {
Scanner scan = new Scanner(System.in);
public int sumOfNumber(int num){
int sum = 0;
for (int i = 1; i <= num; i++){
sum += i;
}
return sum;
}
// while loop
public void sumNumberWithoutZeroInput(){
int sum = 0;
System.out.println("Enter any number \n 0 to terminate");
int num = scan.nextInt();
while (num != 0){
sum += num;
num = scan.nextInt();
}
System.out.println("Sum = "+ sum);
}}
//do while loop
//
// public void options(){
// int option;
// do{
// System.out.println("Kindly select from the option below");
// System.out.println(">>1: for loop <<");
// System.out.println(">>2: while loop <<");
// System.out.println(">>3: exit <<");
// option = scan.nextInt();
// switch (option){
// case 1:
// System.out.println("Enter number to sum");
// int num = scan.nextInt();
// sumOfNumber(num);
// break;
// case 2:
// sumNumberWithoutZeroInput();
// break;
// case 3:
// System.exit(0);
// break;
// }
// }
//}