Day:38-While loop practice 4
Example1:[TBD] package While; public class Eaten { public static void main(String[] args) { int total=8; int count=1; while (count

Example1:[TBD]
package While;
public class Eaten {
public static void main(String[] args) {
int total=8;
int count=1;
while (count<=3) {
eaten = total/2;
total=total+eaten;
count+=1;
}
System.out.println(total);
}
}
Example2:
package While;
public class Div {
public static void main(String[] args) {
int no = 120;
int div = 1;
int count_of_divisors = 0;
while (div<=no) {
if(no%div == 0)
{
//System.out.println(div);
count_of_divisors+=1;
}
div+=1;
}
System.out.println(count_of_divisors);
}
}
output:
16
Example3:
package While;
public class Div2 {
public static void findDivisors(int no) {
int div = 2;
int countOfDivisors = 0;
while (div < no) {
if (no % div == 0) {
System.out.println(div);
countOfDivisors++;
}
div++;
}
System.out.println(countOfDivisors);
}
public static void main(String[] args) {
findDivisors(17);
}
}
output:
0
Example4:
package While;
public class Div3 {
public static boolean checkPrime(int no) {
int countOfDivisors = 0;
for (int div = 2; div < no; div++) {
if (no % div == 0) {
System.out.println(div);
countOfDivisors++;
}
}
if (countOfDivisors == 0) {
return true; // It's a prime number
}
return false; // Not a prime number
}
public static void main(String[] args) {
boolean result = checkPrime(120);
if (result) {
System.out.println("Prime Number");
} else {
System.out.println("Not Prime Number");
}
}
}
output:
2
3
4
5
6
8
10
12
15
20
24
30
40
60
Not Prime Number
Example5:
package While;
public class Box {
public static void main(String[] args) {
double box = 1024;
int securityCount = 0;
while (box > 1) {
box = box / 2;
securityCount++;
}
System.out.println(securityCount);
}
}
output:
10
Different between / and // in python
- / — Floating-point division
- // — Floor division (Integer division)
- / — Floating-point division Always returns a float, even if the division is exact. Example:
print(10 / 2) # Output: 5.0 (float)
print(7 / 2) # Output: 3.5
- // — Floor division (Integer division)
- Returns the largest integer less than or equal to the result.[TBD]
- Can return int or float depending on operands.
- It truncates the decimal part.[TBD] Example:
print(10 // 2) # Output: 5
print(7 // 2) # Output: 3
print(-7 // 2) # Output: -4 (floors toward negative infinity)