Prepared by: Saroj Dhital
JAVA IMPORTANT PROGRAMS COLLECTIONS
1.Program for Arithmetic operations with initialized values.
public class ArithmeticOperations {
public static void main(String[] args) {
int a = 20;
int b = 20;
int sum = a + b;
int difference = a - b;
int product = a * b;
int quotient = a / b;
int remainder = a % b;
System.out.println("Addition of " + a + " and " + b + " is " + sum);
System.out.println("Subtraction of " + a + " and " + b + " is " + difference);
System.out.println("Multiplication of " + a + " and " + b + " is " + product);
System.out.println("Division of " + a + " and " + b + " is " + quotient);
System.out.println("Remainder of " + a + " and " + b + " is " + remainder);
}
}
OUTPUT:
Addition of 20 and 20 is 40
Subtraction of 20 and 20 is 0
Multiplication of 20 and 20 is 400
Division of 20 and 20 is 1
Remainder of 20 and 20 is 0
Prepared by: Saroj Dhital
2.Program for Arithmetic operations for user input numbers.
import java.util.Scanner;
public class ArithmeticOperations {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter first number");
int a = input.nextInt();
System.out.println("Enter second number");
int b = input.nextInt();
int sum = a + b;
int difference = a - b;
int product = a * b;
int quotient = a / b;
int remainder = a % b;
System.out.println("Addition of " + a + " and " + b + " is " + sum);
System.out.println("Subtraction of " + a + " and " + b + " is " + difference);
System.out.println("Multiplication of " + a + " and " + b + " is " + product);
System.out.println("Division of " + a + " and " + b + " is " + quotient);
System.out.println("Remainder of " + a + " and " + b + " is " + remainder);
input.close();
}
}
Prepared by: Saroj Dhital
3.Program to find the Area of Rectangle[Any Solids]
import java.util.Scanner;
public class AreaOfRectangle {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter height of rectangle");
int h = input.nextInt();
System.out.println("Enter width of rectangle");
int w = input.nextInt();
int area = h * w;
System.out.println("Area of rectangle = " + area);
input.close();
}
}
4.Program to find Area of the Circle[Constant Definition]
import java.util.Scanner;
public class AreaOfCircle {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
final double PI = 3.14; // Defining PI as a constant
System.out.println("Enter radius of circle");
double r = input.nextDouble();
double area = PI * r * r;
System.out.printf("Area of circle = %.2f\n", area);
input.close();
}
}
Prepared by: Saroj Dhital
5.Program for Calculation of Simple & Compound Interest[Math]
import java.util.Scanner;
import java.lang.Math;
public class InterestCalculation {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter principle: ");
float p = input.nextFloat();
System.out.print("Enter rate of interest: ");
float r = input.nextFloat();
System.out.print("Enter time: ");
float t = input.nextFloat();
// Simple Interest
float si = (p * r * t) / 100;
System.out.printf("Simple Interest = %f\n", si);
// Compound Interest
double amount = p * Math.pow((1 + r / 100), t);
double ci = amount - p;
System.out.printf("Compound Amount = %f\n", amount);
System.out.printf("Compound Interest = %f\n", ci);
input.close();
}
}
Prepared by: Saroj Dhital
6.Program for Swapping of 2 variables.
import java.util.Scanner;
public class SwapNumbers {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the value of a: ");
int a = input.nextInt();
System.out.print("Enter the value of b: ");
int b = input.nextInt();
System.out.println("Before swapping a = " + a + " and b = " + b);
// Swapping Numbers using a temporary variable
int c = a;
a = b;
b = c;
System.out.println("After swapping a = " + a + " and b = " + b);
input.close();
}
}
Prepared by: Saroj Dhital
Decision Making Statements
7.Program to find Greatest number among 2 numbers.
import java.util.Scanner;
public class GreaterOfTwoNumbers {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the first number: ");
int num1 = input.nextInt();
System.out.print("Enter the second number: ");
int num2 = input.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("Both numbers are equal");
}
input.close();
}
}
Prepared by: Saroj Dhital
8.Program to find the Greatest number among 3 numbers.
import java.util.Scanner;
public class GreatestOfThreeNumbers {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the first number: ");
int num1 = input.nextInt();
System.out.print("Enter the second number: ");
int num2 = input.nextInt();
System.out.print("Enter the third number: ");
int num3 = input.nextInt();
if (num1 >= num2 && num1 >= num3)
{
System.out.println(num1 + " is the greatest number.");
}
else if (num2 >= num1 && num2 >= num3)
{
System.out.println(num2 + " is the greatest number.");
}
else
{
System.out.println(num3 + " is the greatest number.");
}
input.close();
}
}
Prepared by: Saroj Dhital
9.Program to check if the number is positive, negative or zero.
import java.util.Scanner;
public class CheckNumberSign {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = input.nextInt();
if (num > 0)
{
System.out.println("The number is positive.");
}
else if (num < 0)
{
System.out.println("The number is negative.");
}
else
{
System.out.println("The number is zero.");
}
input.close();
}
}
Prepared by: Saroj Dhital
10. Program to check the entered number even or odd.
import java.util.Scanner;
public class CheckEvenOdd {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = input.nextInt();
if (num == 0)
{
System.out.println("The number is zero.");
}
else if (num % 2 == 0)
{
System.out.println("The number is even.");
}
else
{
System.out.println("The number is odd.");
}
input.close();
}
}
Prepared by: Saroj Dhital
11. Voting System Program
import java.util.Scanner;
public class VotingSystem {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter your age: ");
int age = input.nextInt();
if (age >= 18)
{
System.out.println("You are eligible to vote.");
}
else
{
System.out.println("You are not eligible to vote.");
}
input.close();
}
}
Prepared by: Saroj Dhital
12. Program to check the Validity of Triangle.
import java.util.Scanner;
public class TriangleValidity {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the first angle: ");
int angle1 = input.nextInt();
System.out.print("Enter the second angle: ");
int angle2 = input.nextInt();
System.out.print("Enter the third angle: ");
int angle3 = input.nextInt();
int sum = angle1 + angle2 + angle3;
if (sum == 180 && angle1 > 0 && angle2 > 0 && angle3 > 0)
{
System.out.println("The angles form a valid triangle.");
}
else
{
System.out.println("The angles do not form a valid triangle.");
}
input.close();
}
}
Prepared by: Saroj Dhital
13. Program to Calculate Profit and Loss.
import java.util.Scanner;
public class CalculateProfitLoss {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the Cost Price (CP): ");
float CP = input.nextFloat();
System.out.print("Enter the Selling Price (SP): ");
float SP = input.nextFloat();
if (SP > CP)
{
float P = SP - CP;
System.out.printf("Profit (P): %.2f\n", P);
}
else if (CP > SP)
{
float L = CP - SP;
System.out.printf("Loss (L): %.2f\n", L);
}
else
{
System.out.println("No Profit, No Loss.");
}
input.close();
}
}
Prepared by: Saroj Dhital
14. Program to Check the entered alphabet Uppercase/Lowercase.
import java.util.Scanner;
public class CaseChecker {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter an alphabet: ");
char ch = input.next().charAt(0); ●
if (ch >= 'A' && ch <= 'Z')
{
System.out.println("It is an uppercase alphabet.");
}
else if (ch >= 'a' && ch <= 'z')
{
System.out.println("It is a lowercase alphabet.");
}
else
{
System.out.println("It is not an alphabet.");
}
input.close();
}
}
Prepared by: Saroj Dhital
15. Program to check entered alphabet Vowel/Consonant.
import java.util.Scanner;
public class CheckVowelConsonant {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter an alphabet: ");
char ch = input.next().charAt(0);
// Check if character is a letter (A-Z or a-z)
if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))
{
// Check for both uppercase and lowercase vowels
if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U' ||
ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
{
System.out.println("It is a vowel.");
}
else
{
System.out.println("It is a consonant.");
}
}
else
{
System.out.println("It is not an alphabet.");
}
input.close();
}
}
Prepared by: Saroj Dhital
16. Program to calculate grade from marks in 3 subjects.
import java.util.Scanner;
public class GradeCalculator {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter marks for IT: ");
int JAVA = input.nextInt();
System.out.print("Enter marks for C: ");
int DS = input.nextInt();
System.out.print("Enter marks for Math: ");
int DSA = input.nextInt();
int totalMarks = JAVA + DS + DSA;
double percentage = (double) totalMarks / 3;
System.out.println("Total Marks: " + totalMarks);
System.out.printf("Percentage: %.2f%%\n", percentage);
if (percentage >= 90) {
System.out.println("Grade: A+");
} else if (percentage >= 80) {
System.out.println("Grade: A");
} else if (percentage >= 70) {
System.out.println("Grade: B+");
} else if (percentage >= 60) {
System.out.println("Grade: B");
} else if (percentage >= 50) {
System.out.println("Grade: C+");
} else if (percentage >= 40) {
System.out.println("Grade: C");
} else {
System.out.println("Grade: Fail");
}
input.close();
}
}
Prepared by: Saroj Dhital
17. Login Problem Program
import java.util.Scanner;
public class LoginProblem {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String passwordStored = "12345";
System.out.print("Enter username: ");
String username = input.nextLine();
System.out.print("Enter password: ");
String password = input.nextLine();
// In Java, use .equals() for string comparison, not ==
if (username.equals(usernameStored) && password.equals(passwordStored))
{
System.out.println("Login Successful");
}
else
{
System.out.println("Wrong username or password");
}
input.close();
}
}
Prepared by: Saroj Dhital
Loop Statements
18. Program to display the Table of 1.
import java.util.Scanner;
public class TableOfOne {
public static void main(String[] args) {
int num = 1;
System.out.println("Table of 1 is:");
for (int i = 1; i <= 10; i++)
{
System.out.println(num + " x " + i + " = " + (num * i));
}
}
}
19. Program to display Table from 1 to 5.
public class TableOneToFive {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++)
{
System.out.println("Table of " + i + ":");
for (int j = 1; j <= 10; j++)
{
System.out.println(i + " x " + j + " = " + (i * j));
}
System.out.println();
}
}
}
Prepared by: Saroj Dhital
20. Program to display the Natural numbers upto ‘n’.
import java.util.Scanner;
public class NaturalNumbersUptoN {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the value of n: ");
int n = input.nextInt();
if (n <= 0)
{
System.out.println("Please enter a positive integer.");
}
else
{
System.out.print("Natural numbers from 1 to " + n + " are:\n");
for (int i = 1; i <= n; i++)
{
System.out.print(i + " ");
}
System.out.println();
}
input.close();
}
}
Prepared by: Saroj Dhital
21. Program to display Natural numbers upto ‘n’ in reverse order.
import java.util.Scanner;
public class NaturalNumbersReverse {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the value of n: ");
int n = input.nextInt();
if (n <= 0)
{
System.out.println("Please enter a positive integer.");
}
else
{
System.out.println("Natural numbers from 1 to " + n + " in reverse are:");
for (int i = n; i >= 1; i--)
{
System.out.print(i + " ");
}
System.out.println();
}
input.close();
}
}
Prepared by: Saroj Dhital
22. Program to find the sum of ‘n’ Natural numbers.
import java.util.Scanner;
public class SumOfNaturalNumbers {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int sum = 0;
System.out.print("Enter the value of n: ");
int n = input.nextInt();
if (n <= 0)
{
System.out.println("Please enter a positive integer.");
}
else
{
for (int i = 1; i <= n; i++)
{
sum = sum + i;
}
System.out.println("Sum of natural numbers from 1 to " + n + " is: " + sum);
}
input.close();
}
}
Prepared by: Saroj Dhital
23. Program to display Even numbers upto ‘n’.
import java.util.Scanner;
public class EvenNumbersUptoN {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter number upto which even number is to be printed: ");
int n = input.nextInt();
System.out.println("Even numbers up to " + n + " are:");
for (int i = 2; i <= n; i = i + 2)
{
System.out.print(i + " ");
}
System.out.println();
input.close();
}
}
24. Program to display Odd numbers upto ‘n’.
import java.util.Scanner;
public class OddNumbersUptoN {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter number upto which odd number is to be printed: ");
int n = input.nextInt();
System.out.println("Odd numbers up to " + n + " are:");
for (int i = 1; i <= n; i = i + 2)
{
System.out.print(i + " ");
}
System.out.println();
input.close();
}
}
Prepared by: Saroj Dhital
25. Program to find the Factorial of entered number.
import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n;
long f = 1;
System.out.print("Enter any number: ");
n = input.nextInt();
for (int i = 1; i <= n; i++)
{
f = f * i;
}
System.out.println("Factorial of " + n + " is " + f);
input.close();
}
}
Prepared by: Saroj Dhital
26. Program to find the Power of entered number.
import java.util.Scanner;
public class PowerOfNumber {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int b, p;
long f = 1;
System.out.print("Enter base: ");
b = input.nextInt();
System.out.print("Enter power: ");
p = input.nextInt();
for (int i = 1; i <= p; i++)
{
f = f * b;
}
System.out.println("Result = " + f);
input.close();
}
}
Prepared by: Saroj Dhital
27. Program to check entered number Prime/Composite.
import java.util.Scanner;
public class CheckPrimeComposite {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n, i, count = 0;
System.out.print("Enter any number: ");
n = input.nextInt();
if (n <= 1)
{
System.out.println("Entered number is composite.");
input.close();
return;
}
for (i = 1; i <= n; i++)
{
if (n % i == 0)
{
count++;
}
}
if (count == 2)
{
System.out.println("Entered number is prime.");
}
else
{
System.out.println("Entered number is composite.");
}
input.close();
}
}
Prepared by: Saroj Dhital
28. Program to print Fibonaccai Series.
import java.util.Scanner;
public class FibonacciSeries {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int i, n;
int a = 0;
int b = 1;
int c = 0;
System.out.print("Enter any number upto you want to print fibonacci series: ");
n = input.nextInt();
for (i = 1; i <= n; i++)
{
System.out.print(c + " ");
a = b;
b = c;
c = a + b;
}
System.out.println();
input.close();
}
}
Prepared by: Saroj Dhital
29. Program to find the Reverse of entered digit.
import java.util.Scanner;
public class ReverseDigit {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num, rev = 0, rem;
System.out.print("Enter a number: ");
num = input.nextInt();
while (num != 0)
{
rem = num % 10;
rev = rev * 10 + rem;
num = num / 10;
}
System.out.println("Reversed number: " + rev);
input.close();
}
}
Prepared by: Saroj Dhital
30. Program to find the sum of numbers of entered digit.
import java.util.Scanner;
public class SumOfDigits {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num, sum = 0, rem;
System.out.print("Enter a number: ");
num = input.nextInt();
while (num != 0)
{
rem = num % 10;
sum = sum + rem;
num = num / 10;
}
System.out.println("Sum of digits: " + sum);
input.close();
}
}