The document contains several Java code examples demonstrating various programming concepts including if statements, switch cases, while loops, for loops, nested loops, and prime number checking. It also includes a program that generates multiplication tables based on user input. Each code snippet is structured to illustrate specific programming techniques and logic.
The document contains several Java code examples demonstrating various programming concepts including if statements, switch cases, while loops, for loops, nested loops, and prime number checking. It also includes a program that generates multiplication tables based on user input. Each code snippet is structured to illustrate specific programming techniques and logic.
The document contains several Java code examples demonstrating various programming concepts including if statements, switch cases, while loops, for loops, nested loops, and prime number checking. It also includes a program that generates multiplication tables based on user input. Each code snippet is structured to illustrate specific programming techniques and logic.
The document contains several Java code examples demonstrating various programming concepts including if statements, switch cases, while loops, for loops, nested loops, and prime number checking. It also includes a program that generates multiplication tables based on user input. Each code snippet is structured to illustrate specific programming techniques and logic.
2. public static void main(String[] args) { 3. int number = 5; 4. 5. if (number == 0) { 6. System.out.println("The number is zero."); 7. } else if (number == 1) { 8. System.out.println("The number is one."); 9. } else if (number == 2) { 10. System.out.println("The number is two."); 11. } else if (number == 3) { 12. System.out.println("The number is three."); 13. } else if (number == 4) { 14. System.out.println("The number is four."); 15. } else if (number == 5) { 16. System.out.println("The number is five."); 17. } else { 18. System.out.println("The number is not within the range of 0 to 5."); 19. } 20. } 21. } Code: D2: previous code using ‘case’ 1. public class MessageBasedOnInteger { 2. public static void main(String[] args) { 3. int number = 5; 4. 5. switch (number) { 6. case 0: 7. System.out.println("The number is zero."); 8. break; 9. case 1: 10. System.out.println("The number is one."); 11. break; 12. case 2: 13. System.out.println("The number is two."); 14. break; 15. case 3: 16. System.out.println("The number is three."); 17. break; 18. case 4: 19. System.out.println("The number is four."); 20. break; 21. case 5: 22. System.out.println("The number is five."); 23. break; 24. default: 25. System.out.println("The number is not within the range of 0 to 5."); 26. } 27. } 28. } Code: D3: example: While loop 1. public class TableOfTwo_WhileLoop { 2. public static void main(String[] args) { 3. System.out.println("Table of 2 using while loop:"); 4. int i = 1; 5. while (i <= 10) { 6. System.out.println("2 * " + i + " = " + (2 * i)); 7. i++; 8. } 9. } 10. } Code: D4: example: For loop 1. public class TableOfTwo_ForLoop { 2. public static void main(String[] args) { 3. System.out.println("Table of 2 using for loop:"); 4. for (int i = 1; i <= 10; i++) { 5. System.out.println("2 * " + i + " = " + (2 * i)); 6. } 7. } 8. } Code: D5: Nested While Loop 1. public class MultiplicationTable { 2. public static void main(String[] args) { 3. // Define the range of multiplication tables 4. int start = 2; 5. int end = 3; 6. // Define the size of the multiplication table 7. int size = 10; 8. 9. // Loop through each number in the range 10. int num = start; 11. while (num <= end) { 12. System.out.println("Multiplication table for " + num + ":"); 13. // Initialize the loop index 14. int i = 1; 15. // Nested loop to generate the multiplication table for the current number 16. while (i <= size) { 17. // Calculate the product of the current number and the loop index 18. int result = num * i; 19. // Print the result with proper formatting 20. System.out.println(num + " x " + i + " = " + result); 21. // Increment the loop index 22. i++; 23. } 24. // Separate each table with a line break 25. System.out.println(); 26. // Move to the next number 27. num++; 28. } 29. } 30. } Code: D6: Nested For Loop 1. public class MultiplicationTable { 2. public static void main(String[] args) { 3. // Define the range of multiplication tables 4. int start = 2; 5. int end = 3; 6. // Define the size of the multiplication table 7. int size = 10; 8. 9. // Loop through each number in the range 10. for (int num = start; num <= end; num++) { 11. System.out.println("Multiplication table for " + num + ":"); 12. // Nested loop to generate the multiplication table for the current number 13. for (int i = 1; i <= size; i++) { 14. // Calculate the product of the current number and the loop index 15. int result = num * i; 16. // Print the result with proper formatting 17. System.out.println(num + " x " + i + " = " + result); 18. } 19. // Separate each table with a line break 20. System.out.println(); 21. } 22. } 23. } Output: Multiplication table for 2: 2x1=2 2x2=4 2x3=6 2x4=8 2 x 5 = 10 2 x 6 = 12 2 x 7 = 14 2 x 8 = 16 2 x 9 = 18 2 x 10 = 20
Multiplication table for 3:
3x1=3 3x2=6 3x3=9 3 x 4 = 12 3 x 5 = 15 3 x 6 = 18 3 x 7 = 21 3 x 8 = 24 3 x 9 = 27 3 x 10 = 30 Code: D7: example 1. public class RightTriangle { 2. 3. public static void main(String[] args) { 4. // Outer loop controls the height of the triangle 5. for (int i = 1; i <= 5; i++) { 6. // Inner loop prints asterisks for each row 7. for (int j = 1; j <= i; j++) { 8. System.out.print("*"); 9. } 10. // Move to the next line after each row 11. System.out.println(); 12. } 13. } 14. } Code: D8: example 1. public class IsPrime { 2. 3. public static void main(String[] n) { 4. int num = 11; // Change this to any number 5. boolean isPrime = true; 6. 7. // Outer loop iterates from 2 to num-1 8. for (int i = 2; i < num; i++) { 9. // Inner loop checks if num is divisible by any number between 2 and num-1 10. for (int j = 2; j <= i; j++) { 11. if (num % j == 0) { 12. isPrime = false; 13. break; // Exit the inner loop if a divisor is found 14. } 15. } 16. // Exit the outer loop if num is proven not prime 17. if (!isPrime) { 18. break; 19. } 20. } 21. 22. System.out.println(num + (isPrime ? " is prime" : " is not prime")); 23. } 24. } Code: D9: write a program that produces the following output Enter a positive number (0 to exit): 5 Multiplication table for 5: 5x1=5 5 x 2 = 10 5 x 3 = 15 5 x 4 = 20 5 x 5 = 25 5 x 6 = 30 5 x 7 = 35 5 x 8 = 40 5 x 9 = 45 5 x 10 = 50
Enter a positive number (0 to exit): 7
Multiplication table for 7: 7x1=7 7 x 2 = 14 7 x 3 = 21 7 x 4 = 28 7 x 5 = 35 7 x 6 = 42 7 x 7 = 49 7 x 8 = 56 7 x 9 = 63 7 x 10 = 70