Program 1: Area of a Circle and Rectangle
Code:
import java.util.Scanner;
public class AreaCalculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Area of Circle
System.out.print("Enter the radius of the circle: ");
double radius = sc.nextDouble();
double circleArea = Math.PI * radius * radius;
System.out.println("Area of Circle: " + circleArea);
// Area of Rectangle
System.out.print("Enter the length of the rectangle: ");
double length = sc.nextDouble();
System.out.print("Enter the breadth of the rectangle: ");
double breadth = sc.nextDouble();
double rectangleArea = length * breadth;
System.out.println("Area of Rectangle: " + rectangleArea);
}
}
Output
Enter the radius of the circle: 5
Area of Circle: 78.53981633974483
Enter the length of the rectangle: 4
Enter the breadth of the rectangle: 3
Area of Rectangle: 12.0
Program 2: Temperature Conversion
Code
import java.util.Scanner;
public class TemperatureConverter {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter temperature in Celsius: ");
double celsius = sc.nextDouble();
double fahrenheit = (celsius * 9 / 5) + 32;
System.out.println("Temperature in Fahrenheit: " + fahrenheit);
System.out.print("Enter temperature in Fahrenheit: ");
double fahrenheitInput = sc.nextDouble();
double celsiusOutput = (fahrenheitInput - 32) * 5 / 9;
System.out.println("Temperature in Celsius: " + celsiusOutput);
}
}
Output:
Enter temperature in Celsius: 25
Temperature in Fahrenheit: 77.0
Enter temperature in Fahrenheit: 98.6
Temperature in Celsius: 37.0
Program 3: Swapping Values
Code
public class SwapValues {
public static void main(String[] args) {
int a = 5, b = 10;
System.out.println("Before Swapping: a = " + a + ", b = " + b);
// Swapping using a temporary variable
int temp = a;
a = b;
b = temp;
System.out.println("After Swapping: a = " + a + ", b = " + b);
}
}
Output
Before Swapping: a = 5, b = 10
After Swapping: a = 10, b = 5
Program 4: Simple Interest Calculation
Code
import java.util.Scanner;
public class SimpleInterestCalculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Principal amount: ");
double principal = sc.nextDouble();
System.out.print("Enter Rate of interest: ");
double rate = sc.nextDouble();
System.out.print("Enter Time in years: ");
double time = sc.nextDouble();
double simpleInterest = (principal * rate * time) / 100;
System.out.println("Simple Interest: " + simpleInterest);
}
}
Output
Enter Principal amount: 1000
Enter Rate of interest: 5
Enter Time in years: 2
Simple Interest: 100.0
Program 5: BMI Calculator
Code
import java.util.Scanner;
public class BMICalculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter weight in kilograms: ");
double weight = sc.nextDouble();
System.out.print("Enter height in meters: ");
double height = sc.nextDouble();
double bmi = weight / (height * height);
System.out.println("BMI: " + bmi);
}
}
Output
Enter weight in kilograms: 70
Enter height in meters: 1.75
BMI: 22.857142857142858
Program 6: Perimeter of Shapes
Code
import java.util.Scanner;
public class PerimeterCalculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Perimeter of Square
System.out.print("Enter the side length of the square: ");
double side = sc.nextDouble();
double squarePerimeter = 4 * side;
System.out.println("Perimeter of Square: " + squarePerimeter);
// Perimeter of Rectangle
System.out.print("Enter the length of the rectangle: ");
double length = sc.nextDouble();
System.out.print("Enter the breadth of the rectangle: ");
double breadth = sc.nextDouble();
double rectanglePerimeter = 2 * (length + breadth);
System.out.println("Perimeter of Rectangle: " + rectanglePerimeter);
// Perimeter of Triangle
System.out.print("Enter the three sides of the triangle: ");
double side1 = sc.nextDouble();
double side2 = sc.nextDouble();
double side3 = sc.nextDouble();
double trianglePerimeter = side1 + side2 + side3;
System.out.println("Perimeter of Triangle: " + trianglePerimeter);
}
}
Output
Enter the side length of the square: 5
Perimeter of Square: 20.0
Enter the length of the rectangle: 4
Enter the breadth of the rectangle: 3
Perimeter of Rectangle: 14.0
Enter the three sides of the triangle: 3 4 5
Perimeter of Triangle: 12.0
Program 7: Comparison of Two Numbers
Code
import java.util.Scanner;
public class CompareNumbers {
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();
if (num1 > num2) {
System.out.println(num1 + " is greater than " + num2);
} else if (num2 > num1) {
System.out.println(num2 + " is greater than " + num1);
} else {
System.out.println(num1 + " and " + num2 + " are equal.");
}
}
}
Output:
Enter the first number: 8
Enter the second number: 5
8 is greater than 5
Program 8: Power and Absolute Value
Code
import java.util.Scanner;
public class MathOperations {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the base number: ");
double base = sc.nextDouble();
System.out.print("Enter the exponent: ");
double exponent = sc.nextDouble();
System.out.print("Enter a number to find its absolute value: ");
double number = sc.nextDouble();
double powerResult = Math.pow(base, exponent);
double absoluteValue = Math.abs(number);
System.out.println(base + " raised to the power of " + exponent + " is: " + powerResult);
System.out.println("The absolute value of " + number + " is: " + absoluteValue);
}
}
Output
Enter the base number: 2
Enter the exponent: 3
Enter a number to find its absolute value: -5
2.0 raised to the power of 3.0 is: 8.0
The absolute value of -5.0 is: 5.0
Program 9: Check Divisibility
Code
import java.util.Scanner;
public class DivisibilityCheck {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
if (num % 5 == 0 && num % 11 == 0) {
System.out.println(num + " is divisible by both 5 and 11.");
} else {
System.out.println(num + " is not divisible by both 5 and 11.");
}
}
}
Output
Enter a number: 55
55 is divisible by both 5 and 11.
Program 10: Check Leap Year
Code
import java.util.Scanner;
public class LeapYearCheck {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a year: ");
int year = sc.nextInt();
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
System.out.println(year + " is a leap year.");
} else {
System.out.println(year + " is not a leap year.");
}
}
}
Output
Enter a year: 2024
2024 is a leap year.
Program 11: Check Odd or Even
import java.util.Scanner;
public class OddEvenCheck {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
if (num % 2 == 0) {
System.out.println(num + " is even.");
} else {
System.out.println(num + " is odd.");
}
}
}
Output
Enter a number: 15
15 is odd.
Program 12: Eligibility to Vote
Code
import java.util.Scanner;
public class VotingEligibility {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your age: ");
int age = sc.nextInt();
if (age >= 18) {
System.out.println("You are eligible to vote.");
} else {
System.out.println("You are not eligible to vote.");
}
}
}
Output
Enter your age: 20
You are eligible to vote.
Program 13: Positive or Negative Number
Code
import java.util.Scanner;
public class PositiveNegativeCheck {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
if (num > 0) {
System.out.println(num + " is positive.");
} else if (num < 0) {
System.out.println(num + " is negative.");
} else {
System.out.println("The number is zero.");
}
}
}
Output:
Enter a number: -5
-5 is negative.
Program 14: Buzz Number
Code
import java.util.Scanner;
public class BuzzNumberCheck {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
if (num % 7 == 0 || num % 10 == 7) {
System.out.println(num + " is a Buzz number.");
} else {
System.out.println(num + " is not a Buzz number.");
}
}
}
Output
Enter a number: 27
27 is not a Buzz number.
Program 15: Discount Calculation
Code
import java.util.Scanner;
public class DiscountCalculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the original price: ");
double price = sc.nextDouble();
double discount;
if (price >= 1000) {
discount = 0.2 * price; // 20% discount
} else if (price >= 500) {
discount = 0.1 * price; // 10% discount
} else {
discount = 0.05 * price; // 5% discount
}
double finalPrice = price - discount;
System.out.println("Discount: " + discount);
System.out.println("Final Price after discount: " + finalPrice);
}
}
Output
Enter the original price: 1200
Discount: 240.0
Final Price after discount: 960.0
Program 16: Grade Calculation
Code
import java.util.Scanner;
public class GradeCalculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your percentage: ");
double percentage = sc.nextDouble();
String grade;
if (percentage >= 90) {
grade = "A+";
} else if (percentage >= 80) {
grade = "A";
} else if (percentage >= 70) {
grade = "B+";
} else if (percentage >= 60) {
grade = "B";
} else if (percentage >= 50) {
grade = "C";
} else {
grade = "F";
}
System.out.println("Your grade is: " + grade);
}
}
Output
Enter your percentage: 85
Your grade is: A
Program 17: Day of the Week
Code
import java.util.Scanner;
public class DayOfWeek {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number (1-7): ");
int day = sc.nextInt();
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println("Invalid input! Please enter a number between 1 and 7.");
}
}
}
Output
Enter a number (1-7): 3
Wednesday
Program 18: Simple Calculator
Code
import java.util.Scanner;
public class SimpleCalculator {
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("Choose an operation (+, -, *, /): ");
char operator = sc.next().charAt(0);
double result;
switch (operator) {
case '+':
result = num1 + num2;
System.out.println("Result: " + result);
break;
case '-':
result = num1 - num2;
System.out.println("Result: " + result);
break;
case '*':
result = num1 * num2;
System.out.println("Result: " + result);
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
System.out.println("Result: " + result);
} else {
System.out.println("Division by zero is not allowed.");
}
break;
default:
System.out.println("Invalid operator!");
}
}
}
Output
Enter first number: 12
Enter second number: 4
Choose an operation (+, -, *, /): *
Result: 48.0
Program 19: Prime Numbers in a Range
Code
import java.util.Scanner;
public class PrimeNumbersInRange {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the starting number: ");
int start = sc.nextInt();
System.out.print("Enter the ending number: ");
int end = sc.nextInt();
System.out.println("Prime numbers between " + start + " and " + end + ":");
for (int i = start; i <= end; i++) {
if (isPrime(i)) {
System.out.print(i + " ");
}
}
}
public static boolean isPrime(int num) {
if (num <= 1) return false;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) return false;
}
return true;
}
}
Output
Enter the starting number: 10
Enter the ending number: 20
Prime numbers between 10 and 20:
11 13 17 19
Program 20: Summation of Odd Numbers
Code
import java.util.Scanner;
public class SumOddNumbers {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the value of n: ");
int n = sc.nextInt();
int sum = 0;
for (int i = 1; i <= n * 2; i += 2) {
sum += i;
}
System.out.println("Sum of first " + n + " odd numbers: " + sum);
}
}
Output
Enter the value of n: 5
Sum of first 5 odd numbers: 25