This Java program uses method overloading to calculate the area of different shapes - circle, square, and rectangle - based on user input. It prompts the user to select a shape, then calls the appropriate method to calculate the area based on the user-provided dimensions for that shape. There are three static methods defined - area() for circle, areaOfSquare() for square, and area() overloaded for rectangle - each taking the appropriate parameters and returning the calculated area.
This Java program uses method overloading to calculate the area of different shapes - circle, square, and rectangle - based on user input. It prompts the user to select a shape, then calls the appropriate method to calculate the area based on the user-provided dimensions for that shape. There are three static methods defined - area() for circle, areaOfSquare() for square, and area() overloaded for rectangle - each taking the appropriate parameters and returning the calculated area.
This Java program uses method overloading to calculate the area of different shapes - circle, square, and rectangle - based on user input. It prompts the user to select a shape, then calls the appropriate method to calculate the area based on the user-provided dimensions for that shape. There are three static methods defined - area() for circle, areaOfSquare() for square, and area() overloaded for rectangle - each taking the appropriate parameters and returning the calculated area.
This Java program uses method overloading to calculate the area of different shapes - circle, square, and rectangle - based on user input. It prompts the user to select a shape, then calls the appropriate method to calculate the area based on the user-provided dimensions for that shape. There are three static methods defined - area() for circle, areaOfSquare() for square, and area() overloaded for rectangle - each taking the appropriate parameters and returning the calculated area.
Menu driven program to calculate area of circle, square
and rectangle - Method Overloading
import java.util.Scanner;
public class Area {
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("1. Area of circle"); System.out.println("2. Area of square"); System.out.println("3. Area of rectangle"); System.out.print("Enter your choice: "); int choice = scanner.nextInt(); switch (choice) { case 1: System.out.print("Enter radius: "); int radius = scanner.nextInt(); double areaOfCircle = area(radius); System.out.println("Area = " + areaOfCircle); break; case 2: System.out.print("Enter side: "); int side = scanner.nextInt(); double areaOfSquare = areaOfSquare(side); System.out.println("Area = " + areaOfSquare); break; case 3: System.out.print("Enter length: "); int length = scanner.nextInt(); System.out.print("Enter breadth: "); int breadth = scanner.nextInt(); double areaOfRectangle = area(length, breadth); System.out.println("Area = " + areaOfRectangle); break; default: System.out.println("Invalid choice"); } }
public static double area(int radius) { double area = Math.PI * radius * radius; return area; }
public static double areaOfSquare(int side) { double area = side * side; return area; }
public static double area(int length, int breadth) { double area = length * breadth; return area; }
} Reverse a Number using a while loop in Java public class ReverseNumber { public static void main(String[] args) { int num = 1234, reversed = 0; while(num != 0) { int digit = num % 10; reversed = reversed * 10 + digit; num /= 10; } System.out.println("Reversed Number: " + reversed); } }
Java program to reverse a string
import java.util.*; public class ReverseString { public static void main(String args[]) { String original, reverse = ""; Scanner in = new Scanner(System.in);
System.out.println("Enter a string to reverse");
original = in.nextLine();
int length = original.length();
for (int i = length - 1 ; i >= 0 ; i--)
reverse = reverse + original.charAt(i);
System.out.println("Reverse of the string: " + reverse);