SNo. Date Programs Page No.
Sign
1 Write a program that accepts
two numbers from the user
and print their sum.
2 Write a Java program to check
whether a given year is a leap
year or not.
3 Write a program to calculate
addition of two number using
prototyping of methods
4 Write a Java program to
calculate simple interest.
5 Write a Java program to find
the factorial of a number
using a for loop.
6 Create a Java program to
swap two numbers without
using a third variable.
7 Write a Java program to check
whether a string is a
palindrome.
8 Write a Java program to sort
an array of integers in
ascending order.
9 Create a Java program that
implements a simple
calculator using switch case
10 Write a Java program to find
the largest of three numbers
entered by the user
PROGRAM – 1 Write a program that accepts two numbers
from the user and print their sum.
import java.util.Scanner;
public class SumTwoNumbers {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Ask for first number
System.out.print("Enter the first number: ");
int num1 = scanner.nextInt();
// Ask for second number
System.out.print("Enter the second number: ");
int num2 = scanner.nextInt();
// Calculate sum
int sum = num1 + num2;
// Print result
System.out.println("The sum is: " + sum);
scanner.close();
}
}
PROGRAM – 2 Write a Java program to check whether a
given year is a leap year or not.
import java.util.Scanner;
public class SumTwoNumbers {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Ask for first number
System.out.print("Enter the first number: ");
int num1 = scanner.nextInt();
// Ask for second number
System.out.print("Enter the second number: ");
int num2 = scanner.nextInt();
// Calculate sum
int sum = num1 + num2;
// Print result
System.out.println("The sum is: " + sum);
scanner.close();
}
}
PROGRAM – 3 Write a program to calculate addition of two number
using prototyping of methods.
import java.util.Scanner;
public class MethodAddition {
// Method prototype (declaration) at the top
public static int add(int a, int b);
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input from user
System.out.print("Enter first number: ");
int num1 = scanner.nextInt();
System.out.print("Enter second number: ");
int num2 = scanner.nextInt();
// Call the add method
int result = add(num1, num2);
// Display result
System.out.println("The sum is: " + result);
scanner.close();
}
// Method definition
public static int add(int a, int b) {
return a + b;
}
}
PROGRAM – 4 Write a Java program to calculate simple interest.
import java.util.Scanner;
public class SimpleInterestCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input principal, rate, and time
System.out.print("Enter the Principal amount: ");
double principal = scanner.nextDouble();
System.out.print("Enter the Rate of interest (%): ");
double rate = scanner.nextDouble();
System.out.print("Enter the Time (in years): ");
double time = scanner.nextDouble();
// Calculate simple interest
double simpleInterest = (principal * rate * time) / 100;
// Display result
System.out.println("Simple Interest is: " + simpleInterest);
scanner.close();
}
}
PROGRAM – 5 Write a Java program to find the factorial of a
number using a for loop.
import java.util.Scanner;
public class FactorialUsingForLoop {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input a number from the user
System.out.print("Enter a number to find its factorial: ");
int number = scanner.nextInt();
// Initialize factorial to 1
long factorial = 1;
// Check for negative input
if (number < 0) {
System.out.println("Factorial is not defined for negative
numbers.");
} else {
// Use for loop to calculate factorial
for (int i = 1; i <= number; i++) {
factorial *= i;
}
// Display the result
System.out.println("Factorial of " + number + " is: " +
factorial);
}
scanner.close();
}
}
PROGRAM – 6 Create a Java program to swap two numbers
without using a third variable.
import java.util.Scanner;
public class SwapWithoutThirdVariable {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input two numbers
System.out.print("Enter the first number (a): ");
int a = scanner.nextInt();
System.out.print("Enter the second number (b): ");
int b = scanner.nextInt();
// Swapping without third variable
a = a + b; // a becomes sum of a and b
b = a - b; // b becomes original a
a = a - b; // a becomes original b
// Output after swapping
System.out.println("After swapping:");
System.out.println("First number (a): " + a);
System.out.println("Second number (b): " + b);
scanner.close();
}
PROGRAM – 7 Write a Java program to check whether a string
is a palindrome.
import java.util.Scanner;
public class PalindromeCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input string
System.out.print("Enter a string: ");
String original = scanner.nextLine();
// Convert to lowercase to make it case-insensitive
String str = original.toLowerCase();
// Reverse the string
String reversed = "";
for (int i = str.length() - 1; i >= 0; i--) {
reversed += str.charAt(i);
}
// Check if the string is a palindrome
if (str.equals(reversed)) {
System.out.println("\"" + original + "\" is a palindrome.");
} else {
System.out.println("\"" + original + "\" is not a
palindrome.");
}
scanner.close();
}
}
PROGRAM – 8 Write a Java program to sort an array of integers in
ascending order
import java.util.Scanner;
public class ArraySortAscending {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input array size
System.out.print("Enter the number of elements in the array:
");
int n = scanner.nextInt();
int[] arr = new int[n];
// Input array elements
System.out.println("Enter " + n + " integers:");
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
// Bubble sort in ascending order
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j + 1]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
// Display sorted array
System.out.println("Array in ascending order:");
for (int num : arr) {
System.out.print(num + " ");
scanner.close();
}
PROGRAM – 9 Create a Java program that implements a simple
calculator using switch case
import java.util.Scanner;
public class SimpleCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input first number
System.out.print("Enter the first number: ");
double num1 = scanner.nextDouble();
// Input second number
System.out.print("Enter the second number: ");
double num2 = scanner.nextDouble();
// Input operator
System.out.print("Enter an operator (+, -, *, /): ");
char operator = scanner.next().charAt(0);
double result;
// Switch case for operations
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("Error: Cannot divide by zero!");
break;
default:
System.out.println("Invalid operator!");
scanner.close();
}
PROGRAM – 10 Write a Java program to find the largest of three
numbers entered by the user.
import java.util.Scanner;
public class LargestOfThree {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input three numbers
System.out.print("Enter the first number: ");
int num1 = scanner.nextInt();
System.out.print("Enter the second number: ");
int num2 = scanner.nextInt();
System.out.print("Enter the third number: ");
int num3 = scanner.nextInt();
// Find the largest number
int largest = num1;
if (num2 > largest) {
largest = num2;
if (num3 > largest) {
largest = num3;
}
// Output the largest number
System.out.println("The largest number is: " + largest);
scanner.close();