Program 1: Program to Print Your CV Using Print and Println Method.
public class PrintCV {
public static void main(String[] args) {
System.out.println("-------------------------------------");
System.out.println("| CURRICULUM VITAE |");
System.out.println("-------------------------------------");
System.out.println("Name: Yogita ");
System.out.println("Phone: 8709333546");
System.out.println("Email: [email protected]");
System.out.println("LinkedIn:https://www.linkedin.com/in/yogita-jaitley-
273194597/ ");
System.out.println("GitHub: https://github.com/yogiita29");
System.out.println("\nEducation:");
System.out.println("Bachelor of Computer Applications (BCA)");
System.out.println("VaishMahilaMahavidyalaya, Rohtak, Haryana ");
System.out.println("Current Year: 3rd Year");
System.out.println("\nExperience:");
System.out.println("- Web Development Intern (Sept 2023-Oct 2023) ");
System.out.println("- Data Science Intern");
Page | 3
System.out.println("\nSkills:");
System.out.print("HTML, CSS, JavaScript, ");
System.out.println("React, Python, Data Science");
System.out.println("\nProjects:");
System.out.println("- flutedu web development website");
System.out.println("- Smart Attendance System using Face Detection with Open
CV & Software Engineering");
System.out.println("\nAchievements:");
System.out.println("- Completed multiple web development projects");
System.out.println("- Artificial Intelligence Primer Certification");
System.out.println("\nExtra-Curricular Activities:");
System.out.println("- Freelance Novelist | Independent Writer ");
System.out.println("\n----------------------------");
System.out.println("| END OF CV |");
System.out.println("----------------------------");
Page | 4
OUTPUT:
-------------------------------------
| CURRICULUM VITAE |
-------------------------------------
Name: Yogita
Phone: 8709333546
Email: [email protected]
LinkedIn:https://www.linkedin.com/in/yogita-jaitley-273194597/
GitHub: https://github.com/yogiita29
Education:
Bachelor of Computer Applications (BCA)
VaishMahilaMahavidyalaya, Rohtak, Haryana
Current Year: 3rd Year
Experience:
- Web Development Intern (Sept 2023-Oct 2023)
- Data Science Intern
Skills:
HTML, CSS, JavaScript, React, Python, Data Science
Page | 5
Projects:
- flutedu web development website
- Smart Attendance System using Face Detection with Open CV & Software
Engineering
Achievements:
- Completed multiple web development projects
- Artificial Intelligence Primer Certification
Extra-Curricular Activities:
- Freelance Novelist | Independent Writer
----------------------------
| END OF CV |
----------------------------
Page | 6
Program 2: Program to Implement Simple Calculator (+,-,*,/,%) Using
int Data Type.
import java.util.Scanner;
public class SimpleCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input: two numbers
System.out.print("Enter first number: ");
int num1 = scanner.nextInt();
System.out.print("Enter second number: ");
int num2 = scanner.nextInt();
// Input: operation choice
System.out.println("Choose an operation (+, -, *, /, %): ");
char operation = scanner.next().charAt(0);
// Switch case to handle different operations
int result;
Page | 7
switch (operation) {
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: Division by zero is not allowed.");
break;
case '%':
Page | 8
result = num1 % num2;
System.out.println("Result: " + result);
break;
default:
System.out.println("Invalid operation! Please use one of the following: +, -, *, /,
%.");
scanner.close();
OUTPUT:
Enter first number: 78
Enter second number: 45
Choose an operation (+, -, *, /, %):
Result: 33
Page | 9
Program 3: Program to Implement Simple Calculator (+,-,*,/%) Using
Float Data Type.
import java.util.Scanner;
public class SimpleCalculatorFloat {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input: two numbers
System.out.print("Enter first number: ");
float num1 = scanner.nextFloat();
System.out.print("Enter second number: ");
float num2 = scanner.nextFloat();
// Input: operation choice
System.out.println("Choose an operation (+, -, *, /, %): ");
char operation = scanner.next().charAt(0);
// Switch case to handle different operations
float result;
Page | 10
switch (operation) {
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: Division by zero is not allowed.");
break;
case '%':
Page | 11
result = num1 % num2;
System.out.println("Result: " + result);
break;
default:
System.out.println("Invalid operation! Please use one of the following: +, -, *, /,
%.");
scanner.close();
OUTPUT:
Enter first number: 78
Enter second number: 98
Choose an operation (+, -, *, /, %):
Result: 176.0
Page | 12
Program 4: Program to Print Average of 3 Numbers
import java.util.Scanner;
public class AverageOfThreeNumbers {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input: three numbers
System.out.print("Enter first number: ");
float num1 = scanner.nextFloat();
System.out.print("Enter second number: ");
float num2 = scanner.nextFloat();
System.out.print("Enter third number: ");
float num3 = scanner.nextFloat();
// Calculate the average
float average = (num1 + num2 + num3) / 3;
// Output the average
System.out.println("The average of the three numbers is: " + average);
Page | 13
scanner.close();
OUTPUT:
Enter first number: 65
Enter second number: 123
Enter third number: 112
The average of the three numbers is: 100.0
Page | 14
Program 5: Program to Print Greatest of 5 Numbers
import java.util.Scanner;
public class GreatestOfFive {
public static void main(String[] args) {
// Create a scanner object to read input
Scanner scanner = new Scanner(System.in);
// Ask the user for 5 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();
System.out.print("Enter the fourth number: ");
int num4 = scanner.nextInt();
System.out.print("Enter the fifth number: ");
Page | 15
int num5 = scanner.nextInt();
// Close the scanner object
scanner.close();
// Find the greatest number using if-else statements
int greatest = num1;
if (num2 > greatest) {
greatest = num2;
if (num3 > greatest) {
greatest = num3;
if (num4 > greatest) {
greatest = num4;
if (num5 > greatest) {
greatest = num5;
// Print the greatest number
Page | 16
System.out.println("The greatest number is: " + greatest);
OUTPUT:
Enter the first number: 89
Enter the second number: 567
Enter the third number: 867
Enter the fourth number: 453
Enter the fifth number: 90
The greatest number is: 867
Page | 17
Program 6: Program to Find the Greater of 4 Number Using
Conditional Operator Only.
import java.util.Scanner;
public class GreatestOfFour {
public static void main(String[] args) {
// Create a scanner object to read input
Scanner scanner = new Scanner(System.in);
// Ask the user for 4 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();
System.out.print("Enter the fourth number: ");
int num4 = scanner.nextInt();
Page | 18
// Close the scanner object
scanner.close();
// Using conditional (ternary) operator to find the greatest number
int greatest = (num1 > num2) ? ((num1 > num3) ? ((num1 > num4) ? num1 :
num4) : ((num3 > num4) ? num3 : num4))
: ((num2 > num3) ? ((num2 > num4) ? num2 : num4) : ((num3
> num4) ? num3 : num4));
// Print the greatest number
System.out.println("The greatest number is: " + greatest);
OUTPUT:
Enter the first number: 79
Enter the second number: 90
Enter the third number: 564
Enter the fourth number: 675
The greatest number is: 675
Page | 19
Program 7: Program to Find the Following Given no. (For
reference....output like this:)
First -: Greatest
Second -: Remaining
Third -:Smallest
import java.util.Scanner;
public class NumberComparison {
public static void main(String[] args) {
// Create a scanner object to read input
Scanner scanner = new Scanner(System.in);
// Ask the user for 3 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();
Page | 20
// Close the scanner object
scanner.close();
// Find the greatest, remaining, and smallest using conditional operator
int greatest = (num1 > num2) ? ((num1 > num3) ? num1 : num3)
: ((num2 > num3) ? num2 : num3);
int smallest = (num1 < num2) ? ((num1 < num3) ? num1 : num3)
: ((num2 < num3) ? num2 : num3);
int remaining = (num1 != greatest && num1 != smallest) ? num1
: (num2 != greatest && num2 != smallest) ? num2
: num3;
// Print the results
System.out.println("First -: " + greatest); // Greatest
System.out.println("Second -: " + remaining); // Remaining
System.out.println("Third -: " + smallest); // Smallest
Page | 21
OUTPUT:
Enter the first number: 67
Enter the second number: 90
Enter the third number: 56
First -: 90
Second -: 67
Third -: 56
Page | 22
Program 8: Program to Implement Bitwise Operators (Biwise AND,
Biwise OR, Biwise XOR) on the Value 21 and 33.
public class BitwiseOperations {
public static void main(String[] args) {
// Defining the two numbers
int num1 = 21; // In binary: 10101
int num2 = 33; // In binary: 100001
// Bitwise AND (&)
intandResult = num1 & num2;
System.out.println("Bitwise AND (21 & 33): " + andResult);
// Bitwise OR (|)
intorResult = num1 | num2;
System.out.println("Bitwise OR (21 | 33): " + orResult);
// Bitwise XOR (^)
intxorResult = num1 ^ num2;
System.out.println("Bitwise XOR (21 ^ 33): " + xorResult);
}
Page | 23
OUTPUT:
Bitwise AND (21 & 33): 1
Bitwise OR (21 | 33): 53
Bitwise XOR (21 ^ 33): 52
Page | 24
Program 9: Program to Apply Bitwise operators (1's compliment, left
shift, right shift, Right shift with zero fill) on the value 53.
public class BitwiseOperators {
public static void main(String[] args) {
// Define the number
intnum = 53; // In binary: 00110101
// 1's Complement (~)
intonesComplement = ~num;
System.out.println("1's Complement (~53): " + onesComplement);
// Left Shift (<<)
intleftShift = num<< 2;
System.out.println("Left Shift (53 << 2): " + leftShift);
// Right Shift (>>)
intrightShift = num>> 2;
System.out.println("Right Shift (53 >> 2): " + rightShift);
// Right Shift with Zero Fill (>>>)
intrightShiftZeroFill = num>>> 2;
Page | 25
System.out.println("Right Shift with Zero Fill (53 >>> 2): " + rightShiftZeroFill);
OUTPUT:
1's Complement (~53): -54
Left Shift (53 << 2): 212
Right Shift (53 >> 2): 13
Right Shift with Zero Fill (53 >>> 2): 13
Page | 26
Program 10: Program of Arithmetic Calculation by Using Two Classes
// Class for arithmetic calculations
class Calculator {
// Method to add two numbers
publicint add(int num1, int num2) {
return num1 + num2;
// Method to subtract two numbers
publicint subtract(int num1, int num2) {
return num1 - num2;
// Method to multiply two numbers
publicint multiply(int num1, int num2) {
return num1 * num2;
// Method to divide two numbers
public double divide(int num1, int num2) {
if (num2 == 0) {
Page | 27
System.out.println("Error: Division by zero");
return 0; // Return 0 if division by zero
return (double) num1 / num2;
// Main class to use Calculator
public class Main {
public static void main(String[] args) {
// Creating an instance of Calculator class
Calculator calculator = new Calculator();
// Numbers for arithmetic operations
int num1 = 10;
int num2 = 5;
// Performing arithmetic operations
int sum = calculator.add(num1, num2);
int difference = calculator.subtract(num1, num2);
int product = calculator.multiply(num1, num2);
double quotient = calculator.divide(num1, num2);
Page | 28
// Displaying the results
System.out.println("Arithmetic Calculations:");
System.out.println("Sum: " + sum);
System.out.println("Difference: " + difference);
System.out.println("Product: " + product);
System.out.println("Quotient: " + quotient);
OUTPUT:
Arithmetic Calculations:
Sum: 15
Difference: 5
Product: 50
Quotient: 2.0
Page | 29
Program 11: Program to Find the Area of Different Shapes
(rectangle,circle) using Overloading Constructor.
// Class to calculate area of different shapes using constructor overloading
public class Shape {
// Constructor for calculating the area of a rectangle
private double area;
public Shape(double length, double width) {
area = length * width;
// Constructor for calculating the area of a circle
public Shape(double radius) {
area = Math.PI * radius * radius;
// Method to return the calculated area
public double getArea() {
return area;
Page | 30
public static void main(String[] args) {
// Creating a rectangle with length 5 and width 3
Shape rectangle = new Shape(5, 3);
System.out.println("Area of Rectangle: " + rectangle.getArea());
// Creating a circle with radius 4
Shape circle = new Shape(4);
System.out.println("Area of Circle: " + circle.getArea());
OUTPUT:
Area of Rectangle: 15.0
Area of Circle: 50.26548245743669
Page | 31
Program 12: Program a to Find the Characteristics of a number
(Even, odd, prime or perfect no.)
import java.util.Scanner;
public class NumberCharacteristics {
// Method to check if the number is even
public static booleanisEven(int number) {
return number % 2 == 0;
// Method to check if the number is odd
public static booleanisOdd(int number) {
return number % 2 != 0;
// Method to check if the number is prime
public static booleanisPrime(int number) {
if (number <= 1) {
return false;
for (inti = 2; i<= Math.sqrt(number); i++) {
Page | 32
if (number % i == 0) {
return false;
return true;
// Method to check if the number is a perfect number
public static booleanisPerfect(int number) {
int sum = 0;
for (inti = 1; i< number; i++) {
if (number % i == 0) {
sum += i;
return sum == number;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Taking input from the user
Page | 33
System.out.print("Enter a number: ");
int number = scanner.nextInt();
// Checking if the number is even or odd
if (isEven(number)) {
System.out.println(number + " is Even.");
} else if (isOdd(number)) {
System.out.println(number + " is Odd.");
// Checking if the number is prime
if (isPrime(number)) {
System.out.println(number + " is Prime.");
} else {
System.out.println(number + " is not Prime.");
// Checking if the number is perfect
if (isPerfect(number)) {
System.out.println(number + " is a Perfect Number.");
} else {
System.out.println(number + " is not a Perfect Number.");
Page | 34
}
scanner.close();
OUTPUT:
Enter a number: 67
67 is Odd.
67 is Prime.
67 is not a Perfect Number.
Page | 35
Program 13: Program to Find Area and Perimeter of Rectangle Using
Single Inheritance.
// Parent class: Shape (for calculating area and perimeter)
class Shape {
protected double length;
protected double width;
// Constructor to initialize the dimensions of the rectangle
public Shape(double length, double width) {
this.length = length;
this.width = width;
// Method to calculate area
public double calculateArea() {
return length * width;
// Method to calculate perimeter
public double calculatePerimeter() {
return 2 * (length + width);
Page | 36
}
// Child class: Rectangle (inherits from Shape)
class Rectangle extends Shape {
// Constructor to initialize the dimensions of the rectangle
public Rectangle(double length, double width) {
super(length, width); // Call the parent class constructor
// You can override methods if needed (not necessary here as we already have
them in the parent class)
public class Main {
public static void main(String[] args) {
// Create an object of Rectangle class
Rectangle rectangle = new Rectangle(5.0, 3.0);
// Calculate and display the area and perimeter of the rectangle
double area = rectangle.calculateArea();
Page | 37
double perimeter = rectangle.calculatePerimeter();
System.out.println("Area of Rectangle: " + area);
System.out.println("Perimeter of Rectangle: " + perimeter);
OUTPUT
Area of Rectangle: 15.0
Perimeter of Rectangle: 16.0
Page | 38
Program 14: Program to Find Simple Interest, Compound Interest
and Difference Between both by using Multiple Inheritance.
// Interface for Simple Interest Calculation
interface SimpleInterest {
doublecalculateSimpleInterest(double principal, double rate, double time);
// Interface for Compound Interest Calculation
interface CompoundInterest {
doublecalculateCompoundInterest(double principal, double rate, double time);
// Class implementing both interfaces
classInterestCalculator implements SimpleInterest, CompoundInterest {
// Implementing method to calculate Simple Interest
public double calculateSimpleInterest(double principal, double rate, double time)
{
return (principal * rate * time) / 100;
Page | 39
// Implementing method to calculate Compound Interest
public double calculateCompoundInterest(double principal, double rate, double
time) {
return principal * Math.pow(1 + rate / 100, time) - principal;
// Method to find the difference between Compound Interest and Simple
Interest
public double calculateDifference(double principal, double rate, double time) {
doublesimpleInterest = calculateSimpleInterest(principal, rate, time);
doublecompoundInterest = calculateCompoundInterest(principal, rate, time);
returncompoundInterest - simpleInterest;
public class Main {
public static void main(String[] args) {
// Example values for the calculation
double principal = 1000; // Principal amount
double rate = 5; // Rate of interest (in %)
double time = 2; // Time (in years)
Page | 40
// Create an instance of InterestCalculator
InterestCalculator calculator = new InterestCalculator();
// Calculate Simple Interest
doublesimpleInterest = calculator.calculateSimpleInterest(principal, rate, time);
System.out.println("Simple Interest: " + simpleInterest);
// Calculate Compound Interest
doublecompoundInterest = calculator.calculateCompoundInterest(principal, rate,
time);
System.out.println("Compound Interest: " + compoundInterest);
// Calculate Difference between Compound Interest and Simple Interest
double difference = calculator.calculateDifference(principal, rate, time);
System.out.println("Difference between Compound and Simple Interest: " +
difference);
Page | 41
OUTPUT:
Simple Interest: 100.0
Compound Interest: 102.5
Difference between Compound and Simple Interest: 2.5
Page | 42
Program 15 .
a)Program to Find the Sum and Subtraction of two no. By using
Hierarchical Inheritance.
// Parent class for performing basic operations
class Operations {
// Method to add two numbers
publicint add(int a, int b) {
return a + b;
// Subclass for Sum operation inheriting the Operations class
class Sum extends Operations {
// Inherits the add method from Operations class
public void displaySum(int a, int b) {
int result = add(a, b); // Using the inherited add method
System.out.println("Sum: " + result);
// Subclass for Subtraction operation inheriting the Operations class
class Subtraction extends Operations {
Page | 43
// Method to subtract two numbers
public void displaySubtraction(int a, int b) {
int result = a - b;
System.out.println("Subtraction: " + result);
public class Main {
public static void main(String[] args) {
// Create objects of Sum and Subtraction classes
Sum sumObj = new Sum();
Subtraction subObj = new Subtraction();
// Example values
int a = 20;
int b = 10;
// Call methods to display the sum and subtraction
sumObj.displaySum(a, b); // Output: Sum: 30
subObj.displaySubtraction(a, b); // Output: Subtraction: 10
}
Page | 44
OUTPUT:
Sum: 30
Subtraction: 10
Page | 45
b) Program to Find the Sum and Division of two numbers By Using
Hierarchical Inheritance.
// Parent class for performing basic operations
class Operations {
// Method to add two numbers
publicint add(int a, int b) {
return a + b;
// Method to divide two numbers
public double divide(int a, int b) {
if (b == 0) {
System.out.println("Division by zero is not allowed.");
return 0; // Return 0 if division by zero
return (double) a / b;
// Subclass for Sum operation inheriting the Operations class
class Sum extends Operations {
Page | 46
// Inherits the add method from Operations class
public void displaySum(int a, int b) {
int result = add(a, b); // Using the inherited add method
System.out.println("Sum: " + result);
// Subclass for Division operation inheriting the Operations class
class Division extends Operations {
// Inherits the divide method from Operations class
public void displayDivision(int a, int b) {
double result = divide(a, b); // Using the inherited divide method
if (b != 0) {
System.out.println("Division: " + result);
public class Main {
public static void main(String[] args) {
// Create objects of Sum and Division classes
Sum sumObj = new Sum();
Page | 47
Division divObj = new Division();
// Example values
int a = 20;
int b = 4;
// Call methods to display the sum and division
sumObj.displaySum(a, b); // Output: Sum: 24
divObj.displayDivision(a, b); // Output: Division: 5.0
OUTPUT:
Sum: 24
Division: 5.0
Page | 48
c) Program to Find the Sum and Multiplication of Two numbers By
using Hierarchical Inheritance.
// Parent class for performing basic operations
class Operations {
// Method to add two numbers
publicint add(int a, int b) {
return a + b;
// Method to multiply two numbers
publicint multiply(int a, int b) {
return a * b;
// Subclass for Sum operation inheriting the Operations class
class Sum extends Operations {
// Inherits the add method from Operations class
public void displaySum(int a, int b) {
int result = add(a, b); // Using the inherited add method
System.out.println("Sum: " + result);
Page | 49
}
// Subclass for Multiplication operation inheriting the Operations class
class Multiplication extends Operations {
// Inherits the multiply method from Operations class
public void displayMultiplication(int a, int b) {
int result = multiply(a, b); // Using the inherited multiply method
System.out.println("Multiplication: " + result);
public class Main {
public static void main(String[] args) {
// Create objects of Sum and Multiplication classes
Sum sumObj = new Sum();
Multiplication mulObj = new Multiplication();
// Example values
int a = 10;
int b = 5;
Page | 50
// Call methods to display the sum and multiplication
sumObj.displaySum(a, b); // Output: Sum: 15
mulObj.displayMultiplication(a, b); // Output: Multiplication: 50
OUTPUT:
Sum: 15
Multiplication: 50
Page | 51
Program 16: Program to make an Interface Shape which consists of
methods (Area and Perimeter) . Now, create 3 classes with name
Traingle, Rectangle, Circle which implements the above interface and
Calculates the Result.
import java.util.Scanner;
// Interface Shape
interface Shape {
void area();
void perimeter();
// Triangle class
class Triangle implements Shape {
double a, b, c; // sides
double base, height;
Triangle(double a, double b, double c, double base, double height) {
this.a = a;
this.b = b;
this.c = c;
Page | 52
this.base = base;
this.height = height;
public void area() {
double area = 0.5 * base * height;
System.out.println("Triangle Area: " + area);
public void perimeter() {
double perimeter = a + b + c;
System.out.println("Triangle Perimeter: " + perimeter);
// Rectangle class
class Rectangle implements Shape {
double length, width;
Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
Page | 53
public void area() {
double area = length * width;
System.out.println("Rectangle Area: " + area);
public void perimeter() {
double perimeter = 2 * (length + width);
System.out.println("Rectangle Perimeter: " + perimeter);
// Circle class
class Circle implements Shape {
double radius;
Circle(double radius) {
this.radius = radius;
public void area() {
double area = Math.PI * radius * radius;
System.out.println("Circle Area: " + area);
Page | 54
}
public void perimeter() {
double perimeter = 2 * Math.PI * radius;
System.out.println("Circle Perimeter: " + perimeter);
// Main class
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Triangle input
System.out.println("Enter sides a, b, c and base and height of the triangle:");
double a = sc.nextDouble();
double b = sc.nextDouble();
double c = sc.nextDouble();
double base = sc.nextDouble();
double height = sc.nextDouble();
Triangle triangle = new Triangle(a, b, c, base, height);
triangle.area();
Page | 55
triangle.perimeter();
// Rectangle input
System.out.println("\nEnter length and width of the rectangle:");
double length = sc.nextDouble();
double width = sc.nextDouble();
Rectangle rectangle = new Rectangle(length, width);
rectangle.area();
rectangle.perimeter();
// Circle input
System.out.println("\nEnter radius of the circle:");
double radius = sc.nextDouble();
Circle circle = new Circle(radius);
circle.area();
circle.perimeter();
sc.close();
}
Page | 56
OUTPUT:
Enter sides a, b, c and base and height of the triangle:
23
54
Triangle Area: 621.0
Triangle Perimeter: 18.0
Enter length and width of the rectangle:
Rectangle Area: 48.0
Rectangle Perimeter: 28.0
Enter radius of the circle:
Circle Area: 201.06192982974676
Circle Perimeter: 50.26548245743669
Page | 57
Program 17: Program to Create a Package with 2 classes, one class
Find the Factorial of a number and other class Find the Fibnocci
Series.
// File: Main.java
package mathutils;
import java.util.Scanner;
class Factorial {
public int findFactorial(int n) {
int fact = 1;
for(inti = 1; i<= n; i++) {
fact *= i;
return fact;
class Fibonacci {
public void generateFibonacci(int count) {
int a = 0, b = 1;
System.out.print("Fibonacci Series: " + a + " " + b);
Page | 58
for (inti = 2; i< count; i++) {
int c = a + b;
System.out.print(" " + c);
a = b;
b = c;
System.out.println();
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Factorial
System.out.print("Enter a number to find factorial: ");
intnum = sc.nextInt();
Factorial fact = new Factorial();
System.out.println("Factorial of " + num + " is: " + fact.findFactorial(num));
// Fibonacci
System.out.print("Enter number of terms for Fibonacci series: ");
Page | 59
int count = sc.nextInt();
Fibonacci fib = new Fibonacci();
fib.generateFibonacci(count);
sc.close();
OUTPUT:
Enter a number to find factorial: 5
Factorial of 5 is: 120
Enter number of terms for Fibonacci series: 7
Fibonacci Series: 0 1 1 2 3 5 8
Page | 60
Program 18: Program to Create 5 threads to Find Different
Characteristics of a number (Even , odd, prime, perfect,
armstrong number )
class EvenCheck extends Thread {
intnum;
EvenCheck(intnum) {
this.num = num;
public void run() {
if (num % 2 == 0)
System.out.println(num + " is Even");
else
System.out.println(num + " is not Even");
classOddCheck extends Thread {
intnum;
OddCheck(intnum) {
this.num = num;
Page | 61
public void run() {
if (num % 2 != 0)
System.out.println(num + " is Odd");
else
System.out.println(num + " is not Odd");
classPrimeCheck extends Thread {
intnum;
PrimeCheck(intnum) {
this.num = num;
public void run() {
booleanisPrime = true;
if (num<= 1)
isPrime = false;
for (inti = 2; i<= Math.sqrt(num); i++) {
if (num % i == 0) {
isPrime = false;
break;
}
Page | 62
}
System.out.println(num + (isPrime ? " is a Prime number" : " is not a Prime
number"));
classPerfectCheck extends Thread {
intnum;
PerfectCheck(intnum) {
this.num = num;
public void run() {
int sum = 0;
for (inti = 1; i<= num / 2; i++) {
if (num % i == 0)
sum += i;
if (sum == num)
System.out.println(num + " is a Perfect number");
else
System.out.println(num + " is not a Perfect number");
Page | 63
}
classArmstrongCheck extends Thread {
intnum;
ArmstrongCheck(intnum) {
this.num = num;
public void run() {
int original = num, sum = 0, digits = 0;
int temp = num;
while (temp != 0) {
digits++;
temp /= 10;
temp = num;
while (temp != 0) {
int digit = temp % 10;
sum += Math.pow(digit, digits);
temp /= 10;
}
Page | 64
if (sum == original)
System.out.println(num + " is an Armstrong number");
else
System.out.println(num + " is not an Armstrong number");
public class NumberProperties {
public static void main(String[] args) {
int number = 28; // Change this number to test different inputs
Thread even = new EvenCheck(number);
Thread odd = new OddCheck(number);
Thread prime = new PrimeCheck(number);
Thread perfect = new PerfectCheck(number);
Thread armstrong = new ArmstrongCheck(number);
even.start();
odd.start();
prime.start();
perfect.start();
Page | 65
armstrong.start();
OUTPUT:
28 is a Perfect number
28 is Even
28 is not Odd
28 is not an Armstrong number
28 is not a Prime number
Page | 66
Program 19: Program to Convert Decimal number to Other
(Hexadecimal , Octal, Binary number)
import java.util.Scanner;
public class DecimalConverter {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input from user
System.out.print("Enter a decimal number: ");
int decimal = scanner.nextInt();
// Conversion
String binary = Integer.toBinaryString(decimal);
String octal = Integer.toOctalString(decimal);
String hexadecimal = Integer.toHexString(decimal).toUpperCase();
// Output
System.out.println("Binary: " + binary);
System.out.println("Octal: " + octal);
System.out.println("Hexadecimal: " + hexadecimal);
Page | 67
scanner.close();
OUTPUT:
Enter a decimal number: 5
Binary: 101
Octal: 5
Hexadecimal: 5
Page | 68
Program 20: Program to Convert Binary to Other
import java.util.Scanner;
public class BinaryConverter {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input binary number
System.out.print("Enter a binary number: ");
String binaryStr = scanner.nextLine();
try {
// Convert binary to decimal
int decimal = Integer.parseInt(binaryStr, 2);
// Convert to octal and hexadecimal
String octal = Integer.toOctalString(decimal);
String hexadecimal = Integer.toHexString(decimal).toUpperCase();
// Output results
System.out.println("Decimal: " + decimal);
Page | 69
System.out.println("Octal: " + octal);
System.out.println("Hexadecimal: " + hexadecimal);
} catch (NumberFormatException e) {
System.out.println("Invalid binary number!");
scanner.close();
OUTPUT:
Enter a binary number: 0001
Decimal: 1
Octal: 1
Hexadecimal: 1
Page | 70
Program 21: Program to Convert Octal to Other
import java.util.Scanner;
public class OctalConverter {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input octal number as a string
System.out.print("Enter an octal number: ");
String octalStr = scanner.nextLine();
try {
// Parse octal to decimal
int decimal = Integer.parseInt(octalStr, 8);
// Convert to binary, decimal, and hexadecimal
String binary = Integer.toBinaryString(decimal);
String hex = Integer.toHexString(decimal).toUpperCase();
// Output the results
System.out.println("Decimal: " + decimal);
Page | 71
System.out.println("Binary: " + binary);
System.out.println("Hexadecimal: " + hex);
} catch (NumberFormatException e) {
System.out.println("Invalid octal number!");
scanner.close();
OUTPUT:
Enter an octal number: 100
Decimal: 64
Binary: 1000000
Hexadecimal: 40
Page | 72
Program 22 : Program to Convert Hexadecimal to other
import java.util.Scanner;
public class HexConverter {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input from user
System.out.print("Enter a hexadecimal number: ");
String hex = scanner.nextLine();
// Convert hex to decimal
int decimal = Integer.parseInt(hex, 16);
// Convert decimal to binary and octal
String binary = Integer.toBinaryString(decimal);
String octal = Integer.toOctalString(decimal);
// Output the results
System.out.println("Decimal: " + decimal);
System.out.println("Binary: " + binary);
Page | 73
System.out.println("Octal: " + octal);
scanner.close();
OUTPUT:
Enter a hexadecimal number: A
Decimal: 10
Binary: 1010
Octal: 12
Page | 74