In: Computer Science
Java Program
1Use a loop to add up the odd numbers between 100 and 200.
2Use a loop to determine if a number is prime. Recall: a number is prime if its only factors are 1 and itself.
3. Nested Loops:
}Write a nested loop that finds the largest prime number smaller than 125.
Question 1: //TestCode1.java public class TestCode1 { public static void main(String[] args) { int sum = 0; for(int i = 100;i<=200;i++){ if(i%2==1){ sum += i; } } } } Question 2: import java.util.Scanner; public class TestCode2 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n; System.out.print("Enter a number : "); n = scanner.nextInt(); boolean flag = true; for(int i = 2;i<n;i++){ if(n%i == 0){ flag = false; } } if (flag) { System.out.println("It is a prime number."); } else{ System.out.println("It is a composite number."); } } } Question 3: public class TestCode3 { public static void main(String[] args) { for(int j = 124;j>=1;j--) { boolean flag = true; int n = j; for (int i = 2; i < n; i++) { if (n % i == 0) { flag = false; } } if(flag){ System.out.println(n); break; } } } }