1 1. Area of Rectangle: import java.util.Scanner; public class RectangleArea { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter the length of the rectangle: "); double length = sc.nextDouble(); System.out.print("Enter the width of the rectangle: "); double width = sc.nextDouble(); // Calculate the area of the rectangle double area = length * width; System.out.println("The area of the rectangle is: " + area); sc.close(); } } 2. Addition of Two Numbers. import java.util.Scanner; public class Sum { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter First Number"); int a=sc.nextInt(); System.out.println("Enter Second Number"); int b=sc.nextInt(); int sum=a+b; System.out.println("Addition is"+sum); } } 3. Factorial of Number import java.util.Scanner; public class SimpleFactorial { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a number to calculate its factorial: "); int number = sc.nextInt(); // Calculate the factorial int factorial = 1; for (int i = 1; i <= number; i++) { factorial *= i; //factorial=factorial*i; } System.out.println("The factorial is: " + factorial); sc.close(); }
2 } 4. Even or Odd import java.util.Scanner; public class EvenOddChecker { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); int number = sc.nextInt(); // Check if the number is even or odd if (number % 2 == 0) { System.out.println(number + " is even."); } else { System.out.println(number + " is odd."); } sc.close(); } } 5. Fibonacci Series: import java.util.Scanner; public class Fibonacci { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter the number of terms: "); int n = sc.nextInt(); int a = 0, b = 1; System.out.print("Fibonacci Series: " + a + ", " + b); for (int i = 2; i < n; i++) { int next = a + b; System.out.print(", " + next); a = b; b = next; } } } 6. Reverse the number: import java.util.Scanner; class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter the number to reverse:"); int num = sc.nextInt(); int reversed = 0; System.out.println("Original Number: " + num); // run loop until num becomes 0 while(num != 0) { // get last digit from num int digit = num % 10; reversed = reversed * 10 + digit; // remove the last digit from num num /= 10;
3 } System.out.println("Reversed Number: " + reversed); } } 7. Armstrong or not: import java.util.Scanner; public class ArmstrongNumber { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); int number = sc.nextInt(); int originalNumber = number; int result = 0; int n = 0; // Count the number of digits while (originalNumber != 0) { originalNumber /= 10; ++n; } originalNumber = number; // Calculate the sum of the nth power of its digits while (originalNumber != 0) { int digit = originalNumber % 10; result += Math.pow(digit, n); originalNumber /= 10; } if (result == number) { System.out.println(number + " is an Armstrong number."); } else { System.out.println(number + " is not an Armstrong number."); } } } 8. Prime or Not: import java.util.Scanner; public class PrimeCheck { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); int number = sc.nextInt(); boolean isPrime = true; for (int i = 2; i <= number / 2; i++) { if (number % i == 0) { isPrime = false; break; } } if (isPrime) { System.out.println(number + " is a prime number."); } else { System.out.println(number + " is not a prime number."); } } }
4 9. Simple Calculator: import java.util.Scanner; public class Calculator { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter first number: "); double num1 = sc.nextDouble(); System.out.print("Enter second number: "); double num2 = sc.nextDouble(); System.out.print("Enter an operator (+, -, *, /): "); char operator = sc.next().charAt(0); double result; switch (operator) { case '+': result = num1 + num2; System.out.println("Addition is: " +result); break; case '-': result = num1 - num2; System.out.println("Subtraction is: " +result); break; case '*': result = num1 * num2; System.out.println("Multiplication is: " +result); break; case '/': result = num1 / num2; System.out.println("Devision is: " +result); break; default: System.out.println("Invalid operator!"); return; } // System.out.println("Result: " + result); } } 10. Swap two Number: import java.util.Scanner; public class SwapNumbers { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter the first number: "); int num1 = sc.nextInt(); System.out.print("Enter the second number: "); int num2 = sc.nextInt(); // Display numbers before swapping System.out.println("Before swapping: "); System.out.println("First number = " + num1); System.out.println("Second number = " + num2); // Swap the numbers int temp = num1; num1 = num2; num2 = temp; // Display numbers after swapping
5 System.out.println("After swapping: "); System.out.println("First number = " + num1); System.out.println("Second number = " + num2); sc.close(); } } 11. Pyramid Pattern of Star public class PyramidPattern { public static void main(String args[]) { int i, j, row = 6; for (i=0; i<row; i++) { for (j=row-i; j>1; j--) { System.out.print(" "); } for (j=0; j<=i; j++ ) { System.out.print("* "); } System.out.println(); } } } 12. Pattern Program: public class Pattern1 { public static void main(String args[]) { int i, j,number, n=7; for(i=0; i<n; i++) { number=1; for(j=0; j<=i; j++) { System.out.print(number+ " ");
6 number++; } System.out.println(); } } } 13. Pattern Program: public class Pattern2 { public static void main(String[] args) { int i, j, k = 1; for (i = 1; i <= 7; i++) { for (j = 1; j< i + 1; j++) { System.out.print(k++ + " "); } System.out.println(); } } } 14. Pattern Program: import java.util.*; public class Pattern5 { public static void main(String[] args) { int i, j, rows; Scanner sc = new Scanner(System.in); System.out.print("Enter the number of rows you want to print: "); rows = sc.nextInt(); for (i = 1; i <= rows; i++)
7 { for (j = 1; j <= i; j++) { System.out.print(i+" "); } System.out.println(); } }} 15. Pattern Program: public class RightAlphabaticPattern { public static void main(String[] args) { int alphabet = 65; //ASCII value of capital A is 65 for (int i = 0; i <= 8; i++) { for (int j = 0; j <= i; j++) { System.out.print((char) (alphabet + j) + " "); } System.out.println(); }}} 16. Array program Find the Sum of Array Elements public class Main { public static void main(String[] args) { // Declare and initialize an array of 5 integers int[] array = {10, 20, 30, 40, 50}; // Calculate the sum of the elements of the array int sum = 0; for (int i = 0; i < array.length; i++) { sum += array[i]; } // Print the sum of the elements System.out.println("Sum of the array elements is: " + sum); }
8 } Output Sum of the array elements is: 150 17. Find the Maximum Element in an Array public class Main { public static void main(String[] args) { // Declare and initialize an array of 5 integers int[] array = {10, 20, 30, 40, 50}; // Find the maximum element in the array int max = array[0]; for (int i = 1; i < array.length; i++) { if (array[i] > max) { max = array[i]; } } // Print the maximum element System.out.println("Maximum element in the array is: " + max); } } Output Maximum element in the array is: 50 X-X-X-X-X-X-X-X-X-X-X 18. Sum of All Elements in a 2D Array public class Main { public static void main(String[] args) { // Declare and initialize a 2D array int[][] array = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; // Calculate the sum of all elements in the 2D array int sum = 0; for (int i = 0; i < array.length; i++) { for (int j = 0; j < array[i].length; j++) { sum += array[i][j]; } } System.out.println("Sum of all elements in the 2D array is: " + sum); } } Output Sum of all elements in the 2D array is: 45
9 19. Transpose of a 2D Array public class Main { public static void main(String[] args) { // Declare and initialize a 2D array int[][] array = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; // Transpose the 2D array int[][] transpose = new int[array[0].length][array.length]; for (int i = 0; i < array.length; i++) { for (int j = 0; j < array[i].length; j++) { transpose[j][i] = array[i][j]; } } // Print the transposed array System.out.println("Transpose of the 2D array is:"); for (int i = 0; i < transpose.length; i++) { for (int j = 0; j < transpose[i].length; j++) { System.out.print(transpose[i][j] + " "); } System.out.println(); } } } Output Transpose of the 2D array is: 1 4 7 2 5 8 3 6 9 20. Multiplication of Two 2D Arrays public class Main { public static void main(String[] args) { // Declare and initialize two 2D arrays int[][] array1 = { {1, 2, 3}, {4, 5, 6} }; int[][] array2 = { {7, 8}, {9, 10}, {11, 12} }; // Multiply the two 2D arrays int[][] product = new int[array1.length][array2[0].length]; for (int i = 0; i < array1.length; i++) { for (int j = 0; j < array2[0].length; j++) {
10 for (int k = 0; k < array1[0].length; k++) { product[i][j] += array1[i][k] * array2[k][j]; } } } // Print the product of the two arrays System.out.println("Product of the two 2D arrays is:"); for (int i = 0; i < product.length; i++) { for (int j = 0; j < product[i].length; j++) { System.out.print(product[i][j] + " "); } System.out.println(); } } } Output Product of the two 2D arrays is: 58 64 139 154 21. Java Program To print Day of Week. 22. import java.util.Scanner; public class DayOfWeek { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // Prompt user for input System.out.print("Enter a number (1-7) for the day of the week: "); int day = sc.nextInt(); // Determine the day of the week using switch case String dayName; switch (day) { case 1: dayName = "Monday"; break; case 2: dayName = "Tuesday"; break; case 3: dayName = "Wednesday"; break; case 4: dayName = "Thursday"; break; case 5: dayName = "Friday"; break; case 6: dayName = "Saturday"; break; case 7:
10 dayName = "Sunday"; break; default: dayName = "Invalid input! Please enter a number between 1 and 7."; } // Output the result System.out.println("The day of the week is: " + dayName); // Close the scanner sc.close(); } } 23. Write a Java program that checks whether a given character is a vowel or consonant using a switch statement. import java.util.Scanner; public class VowelOrConsonant { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a letter: "); char letter = sc.next().toLowerCase().charAt(0); switch (letter) { case 'a': case 'e': case 'i': case 'o': case 'u': System.out.println(letter + " is a vowel."); break; default: if (Character.isLetter(letter)) { System.out.println(letter + " is a consonant."); } else { System.out.println("Invalid input, not a letter."); } } sc.close(); } } 24. Write a Java program to determine a student's grade based on marks using a switch statement. The program should output a grade based on ranges of marks. import java.util.Scanner; public class GradeCalculator { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter your marks (0-100): "); int marks = sc.nextInt(); char grade; switch (marks / 10) { case 10: case 9: grade = 'A';
10 break; case 8: grade = 'B'; break; case 7: grade = 'C'; break; case 6: grade = 'D'; break; case 5: grade = 'E'; break; default: grade = 'F'; } System.out.println("Your grade is: " + grade); sc.close(); } } 25. Write a Java program that takes a number between 1 and 12 from the user and prints the corresponding month name using a switch statement. import java.util.Scanner; public class MonthFinder { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter month number (1-12): "); int month = scanner.nextInt(); String monthName; switch (month) { case 1: monthName = "January"; break; case 2: monthName = "February"; break; case 3: monthName = "March"; break; case 4: monthName = "April"; break; case 5: monthName = "May"; break; case 6: monthName = "June"; break; case 7: monthName = "July"; break; case 8: monthName = "August"; break;
10 case 9: monthName = "September"; break; case 10: monthName = "October"; break; case 11: monthName = "November"; break; case 12: monthName = "December"; break; default: monthName = "Invalid month number!"; } System.out.println("The month is: " + monthName); sc.close(); } } 26. Write a Java program that calculates the sum of natural numbers from 1 to a user-defined value using a while loop. import java.util.Scanner; public class SumOfNum { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter the upper limit: "); int n = sc.nextInt(); int sum = 0; int i = 1; while (i <= n) { sum += i; // add current number to sum i++; // increment the counter } System.out.println("Sum of natural numbers from 1 to " + n + " is: " + sum); sc.close(); } } 27. Write a Java program that reverses a given number using a while loop. import java.util.Scanner; public class ReverseNumber { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); int number = sc.nextInt(); int reverse = 0; while (number != 0) { int digit = number % 10; reverse = reverse * 10 + digit; number /= 10; } System.out.println("Reversed number: " + reverse); sc.close(); } }

Simple 27 Java Program on basic java syntax

  • 1.
    1 1. Area ofRectangle: import java.util.Scanner; public class RectangleArea { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter the length of the rectangle: "); double length = sc.nextDouble(); System.out.print("Enter the width of the rectangle: "); double width = sc.nextDouble(); // Calculate the area of the rectangle double area = length * width; System.out.println("The area of the rectangle is: " + area); sc.close(); } } 2. Addition of Two Numbers. import java.util.Scanner; public class Sum { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter First Number"); int a=sc.nextInt(); System.out.println("Enter Second Number"); int b=sc.nextInt(); int sum=a+b; System.out.println("Addition is"+sum); } } 3. Factorial of Number import java.util.Scanner; public class SimpleFactorial { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a number to calculate its factorial: "); int number = sc.nextInt(); // Calculate the factorial int factorial = 1; for (int i = 1; i <= number; i++) { factorial *= i; //factorial=factorial*i; } System.out.println("The factorial is: " + factorial); sc.close(); }
  • 2.
    2 } 4. Even orOdd import java.util.Scanner; public class EvenOddChecker { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); int number = sc.nextInt(); // Check if the number is even or odd if (number % 2 == 0) { System.out.println(number + " is even."); } else { System.out.println(number + " is odd."); } sc.close(); } } 5. Fibonacci Series: import java.util.Scanner; public class Fibonacci { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter the number of terms: "); int n = sc.nextInt(); int a = 0, b = 1; System.out.print("Fibonacci Series: " + a + ", " + b); for (int i = 2; i < n; i++) { int next = a + b; System.out.print(", " + next); a = b; b = next; } } } 6. Reverse the number: import java.util.Scanner; class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter the number to reverse:"); int num = sc.nextInt(); int reversed = 0; System.out.println("Original Number: " + num); // run loop until num becomes 0 while(num != 0) { // get last digit from num int digit = num % 10; reversed = reversed * 10 + digit; // remove the last digit from num num /= 10;
  • 3.
    3 } System.out.println("Reversed Number: "+ reversed); } } 7. Armstrong or not: import java.util.Scanner; public class ArmstrongNumber { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); int number = sc.nextInt(); int originalNumber = number; int result = 0; int n = 0; // Count the number of digits while (originalNumber != 0) { originalNumber /= 10; ++n; } originalNumber = number; // Calculate the sum of the nth power of its digits while (originalNumber != 0) { int digit = originalNumber % 10; result += Math.pow(digit, n); originalNumber /= 10; } if (result == number) { System.out.println(number + " is an Armstrong number."); } else { System.out.println(number + " is not an Armstrong number."); } } } 8. Prime or Not: import java.util.Scanner; public class PrimeCheck { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); int number = sc.nextInt(); boolean isPrime = true; for (int i = 2; i <= number / 2; i++) { if (number % i == 0) { isPrime = false; break; } } if (isPrime) { System.out.println(number + " is a prime number."); } else { System.out.println(number + " is not a prime number."); } } }
  • 4.
    4 9. Simple Calculator: importjava.util.Scanner; public class Calculator { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter first number: "); double num1 = sc.nextDouble(); System.out.print("Enter second number: "); double num2 = sc.nextDouble(); System.out.print("Enter an operator (+, -, *, /): "); char operator = sc.next().charAt(0); double result; switch (operator) { case '+': result = num1 + num2; System.out.println("Addition is: " +result); break; case '-': result = num1 - num2; System.out.println("Subtraction is: " +result); break; case '*': result = num1 * num2; System.out.println("Multiplication is: " +result); break; case '/': result = num1 / num2; System.out.println("Devision is: " +result); break; default: System.out.println("Invalid operator!"); return; } // System.out.println("Result: " + result); } } 10. Swap two Number: import java.util.Scanner; public class SwapNumbers { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter the first number: "); int num1 = sc.nextInt(); System.out.print("Enter the second number: "); int num2 = sc.nextInt(); // Display numbers before swapping System.out.println("Before swapping: "); System.out.println("First number = " + num1); System.out.println("Second number = " + num2); // Swap the numbers int temp = num1; num1 = num2; num2 = temp; // Display numbers after swapping
  • 5.
    5 System.out.println("After swapping: "); System.out.println("Firstnumber = " + num1); System.out.println("Second number = " + num2); sc.close(); } } 11. Pyramid Pattern of Star public class PyramidPattern { public static void main(String args[]) { int i, j, row = 6; for (i=0; i<row; i++) { for (j=row-i; j>1; j--) { System.out.print(" "); } for (j=0; j<=i; j++ ) { System.out.print("* "); } System.out.println(); } } } 12. Pattern Program: public class Pattern1 { public static void main(String args[]) { int i, j,number, n=7; for(i=0; i<n; i++) { number=1; for(j=0; j<=i; j++) { System.out.print(number+ " ");
  • 6.
    6 number++; } System.out.println(); } } } 13.Pattern Program: public class Pattern2 { public static void main(String[] args) { int i, j, k = 1; for (i = 1; i <= 7; i++) { for (j = 1; j< i + 1; j++) { System.out.print(k++ + " "); } System.out.println(); } } } 14. Pattern Program: import java.util.*; public class Pattern5 { public static void main(String[] args) { int i, j, rows; Scanner sc = new Scanner(System.in); System.out.print("Enter the number of rows you want to print: "); rows = sc.nextInt(); for (i = 1; i <= rows; i++)
  • 7.
    7 { for (j =1; j <= i; j++) { System.out.print(i+" "); } System.out.println(); } }} 15. Pattern Program: public class RightAlphabaticPattern { public static void main(String[] args) { int alphabet = 65; //ASCII value of capital A is 65 for (int i = 0; i <= 8; i++) { for (int j = 0; j <= i; j++) { System.out.print((char) (alphabet + j) + " "); } System.out.println(); }}} 16. Array program Find the Sum of Array Elements public class Main { public static void main(String[] args) { // Declare and initialize an array of 5 integers int[] array = {10, 20, 30, 40, 50}; // Calculate the sum of the elements of the array int sum = 0; for (int i = 0; i < array.length; i++) { sum += array[i]; } // Print the sum of the elements System.out.println("Sum of the array elements is: " + sum); }
  • 8.
    8 } Output Sum of thearray elements is: 150 17. Find the Maximum Element in an Array public class Main { public static void main(String[] args) { // Declare and initialize an array of 5 integers int[] array = {10, 20, 30, 40, 50}; // Find the maximum element in the array int max = array[0]; for (int i = 1; i < array.length; i++) { if (array[i] > max) { max = array[i]; } } // Print the maximum element System.out.println("Maximum element in the array is: " + max); } } Output Maximum element in the array is: 50 X-X-X-X-X-X-X-X-X-X-X 18. Sum of All Elements in a 2D Array public class Main { public static void main(String[] args) { // Declare and initialize a 2D array int[][] array = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; // Calculate the sum of all elements in the 2D array int sum = 0; for (int i = 0; i < array.length; i++) { for (int j = 0; j < array[i].length; j++) { sum += array[i][j]; } } System.out.println("Sum of all elements in the 2D array is: " + sum); } } Output Sum of all elements in the 2D array is: 45
  • 9.
    9 19. Transpose ofa 2D Array public class Main { public static void main(String[] args) { // Declare and initialize a 2D array int[][] array = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; // Transpose the 2D array int[][] transpose = new int[array[0].length][array.length]; for (int i = 0; i < array.length; i++) { for (int j = 0; j < array[i].length; j++) { transpose[j][i] = array[i][j]; } } // Print the transposed array System.out.println("Transpose of the 2D array is:"); for (int i = 0; i < transpose.length; i++) { for (int j = 0; j < transpose[i].length; j++) { System.out.print(transpose[i][j] + " "); } System.out.println(); } } } Output Transpose of the 2D array is: 1 4 7 2 5 8 3 6 9 20. Multiplication of Two 2D Arrays public class Main { public static void main(String[] args) { // Declare and initialize two 2D arrays int[][] array1 = { {1, 2, 3}, {4, 5, 6} }; int[][] array2 = { {7, 8}, {9, 10}, {11, 12} }; // Multiply the two 2D arrays int[][] product = new int[array1.length][array2[0].length]; for (int i = 0; i < array1.length; i++) { for (int j = 0; j < array2[0].length; j++) {
  • 10.
    10 for (int k= 0; k < array1[0].length; k++) { product[i][j] += array1[i][k] * array2[k][j]; } } } // Print the product of the two arrays System.out.println("Product of the two 2D arrays is:"); for (int i = 0; i < product.length; i++) { for (int j = 0; j < product[i].length; j++) { System.out.print(product[i][j] + " "); } System.out.println(); } } } Output Product of the two 2D arrays is: 58 64 139 154 21. Java Program To print Day of Week. 22. import java.util.Scanner; public class DayOfWeek { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // Prompt user for input System.out.print("Enter a number (1-7) for the day of the week: "); int day = sc.nextInt(); // Determine the day of the week using switch case String dayName; switch (day) { case 1: dayName = "Monday"; break; case 2: dayName = "Tuesday"; break; case 3: dayName = "Wednesday"; break; case 4: dayName = "Thursday"; break; case 5: dayName = "Friday"; break; case 6: dayName = "Saturday"; break; case 7:
  • 11.
    10 dayName = "Sunday"; break; default: dayName= "Invalid input! Please enter a number between 1 and 7."; } // Output the result System.out.println("The day of the week is: " + dayName); // Close the scanner sc.close(); } } 23. Write a Java program that checks whether a given character is a vowel or consonant using a switch statement. import java.util.Scanner; public class VowelOrConsonant { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a letter: "); char letter = sc.next().toLowerCase().charAt(0); switch (letter) { case 'a': case 'e': case 'i': case 'o': case 'u': System.out.println(letter + " is a vowel."); break; default: if (Character.isLetter(letter)) { System.out.println(letter + " is a consonant."); } else { System.out.println("Invalid input, not a letter."); } } sc.close(); } } 24. Write a Java program to determine a student's grade based on marks using a switch statement. The program should output a grade based on ranges of marks. import java.util.Scanner; public class GradeCalculator { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter your marks (0-100): "); int marks = sc.nextInt(); char grade; switch (marks / 10) { case 10: case 9: grade = 'A';
  • 12.
    10 break; case 8: grade ='B'; break; case 7: grade = 'C'; break; case 6: grade = 'D'; break; case 5: grade = 'E'; break; default: grade = 'F'; } System.out.println("Your grade is: " + grade); sc.close(); } } 25. Write a Java program that takes a number between 1 and 12 from the user and prints the corresponding month name using a switch statement. import java.util.Scanner; public class MonthFinder { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter month number (1-12): "); int month = scanner.nextInt(); String monthName; switch (month) { case 1: monthName = "January"; break; case 2: monthName = "February"; break; case 3: monthName = "March"; break; case 4: monthName = "April"; break; case 5: monthName = "May"; break; case 6: monthName = "June"; break; case 7: monthName = "July"; break; case 8: monthName = "August"; break;
  • 13.
    10 case 9: monthName ="September"; break; case 10: monthName = "October"; break; case 11: monthName = "November"; break; case 12: monthName = "December"; break; default: monthName = "Invalid month number!"; } System.out.println("The month is: " + monthName); sc.close(); } } 26. Write a Java program that calculates the sum of natural numbers from 1 to a user-defined value using a while loop. import java.util.Scanner; public class SumOfNum { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter the upper limit: "); int n = sc.nextInt(); int sum = 0; int i = 1; while (i <= n) { sum += i; // add current number to sum i++; // increment the counter } System.out.println("Sum of natural numbers from 1 to " + n + " is: " + sum); sc.close(); } } 27. Write a Java program that reverses a given number using a while loop. import java.util.Scanner; public class ReverseNumber { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); int number = sc.nextInt(); int reverse = 0; while (number != 0) { int digit = number % 10; reverse = reverse * 10 + digit; number /= 10; } System.out.println("Reversed number: " + reverse); sc.close(); } }