-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathWhileLoop.java
More file actions
62 lines (51 loc) · 1.37 KB
/
WhileLoop.java
File metadata and controls
62 lines (51 loc) · 1.37 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
package com.coderbd;
import java.util.Scanner;
/**
*
* @author Mohammad Rajaul Islam
*/
public class WhileLoop {
public static void printFirstNumToSecondNum(int num1, int num2) {
while (num1 <= num2) {
System.out.println(num1);
num1++;
}
}
public static int makeSumFromFirstNumToSecondNum(int num1, int num2) {
int sum = 0;
while (num1 <= num2) {
sum += num1;
num1++;
}
return sum;
}
public static void workWithSentinelvalue(Scanner sc) {
int sum = 0;
int n1 = 1;
while (true) {
System.out.println("Enter Number " + n1 + " Time");
int num1 = sc.nextInt();
if (num1 != -1) {
sum += num1;
System.out.println("Sum: " + sum);
n1++;
} else {
break;
}
}
}
public static void main(String[] args) {
System.out.println("GCD: " + findOutGcd(16, 24));
}
public static int findOutGcd(int n1, int n2) {
int gcd = 1;//initial GCD
int possibleGcd = 2;
while (possibleGcd <= n1 && possibleGcd <= n2) {
if (n1 % possibleGcd == 0 && n2 % possibleGcd == 0) {
gcd = possibleGcd;
}
possibleGcd++;
}
return gcd;
}
}