Computer Project 5
Computer Project 5
Write a program in java to display the second smallest number of the three
numbers input from the keyboard.
➢ Algorithm:
1. Start the program.
2. Read three numbers from the user using the Scanner class and store them in
variables a, b, and c.
3. Check the following conditions: a. If ((a > b && b > c) || (c > b && b > a)),
then b is the second smallest number. Print "Second smallest is " + b. b. Else if
((b > c && c > a) || (a > c && c > b)), then c is the second smallest number.
Print "Second smallest is " + c. c. Else, a is the second smallest number. Print
"Second smallest is " + a.
4. End the program.
import java.util.*;
class second_smallest
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
else
➢ Variable Description:
Variable Datatype Description
a int To read and store the value of a
b int To read and store the value of b
c int To read and store the value of c
➢ Output:
2. Write a program in Java to input a number (greater than zero) and if:
(a) It is odd positive number then print its square and cube.
(b) It is an even positive number then print sum of its square and square root.
➢ Algorithm:
1. Start the program.
2. Read a number greater than zero from the user using the Scanner class and store
it in variable n.
3. Check the following conditions:
a. If (n % 2 == 1), then the number is odd positive.
- Calculate the square of the number by multiplying n by itself and store the
result in a variable called square.
- Calculate the cube of the number by multiplying n by itself twice and store
the result in a variable called cube.
- Print "Square = " followed by the value of square.
- Print "Cube = " followed by the value of cube.
b. If (n % 2 == 0), then the number is even positive.
- Calculate the sum of the square of the number and the square root of the
number by using the Math.sqrt(n) function and store the result in a variable called
sum.
- Print the value of sum.
4. The given algorithm assumes that the input number is a positive number greater
than zero. If negative numbers or zero are allowed, additional checks and
conditions need to be added to handle those cases.
5. End the program.
import java.util.*;
class OPSC_EPSSSR
double n = sc.nextDouble();
if (n%2==1)
System.out.println("Square = "+(n*n));
System.out.println("Cube = "+(n*n*n));
if (n%2==0)
System.out.println(sum);
}
➢ Variable Description:
Variable Datatype Description
n double To read and store the value of n
sum double To calculate the sum
➢ Output:
3. Write a program in Java to find the volume, total surface area and the
diagonal of a cuboid as per the user’s choice. Take length(l), breadth(b) and
height(h) as inputs.
Volume=l*b*h
Total surface area=2*(l*b + b*h + l*h)
Diagonal = √𝑙 2 + 𝑏 2 + ℎ2
➢ Algorithm:
2. Read the length, breadth, and height of a cuboid from the user using the Scanner
class and store them in variables l, b, and h respectively.
3. Calculate the volume of the cuboid by multiplying the length, breadth, and height
and store it in the variable volume.
4. Calculate the total surface area of the cuboid using the formula 2*(l*b + b*h + l*h)
and store it in the variable tsa.
5. Calculate the diagonal of the cuboid using the formula Math.sqrt(l*l + b*b + h*h)
and store it in the variable diagonal.
6. Print the value of the volume of the cuboid using the message "Volume of a cuboid
= " + volume.
7. Print the value of the total surface area of the cuboid using the message "Total
surface area of a cuboid = " + tsa.
8. Print the value of the diagonal of the cuboid using the message "Diagonal of a
cuboid = " + diagonal.
➢ Variable Description:
Variable Datatype Description
l double To read and store the length
b double To read and store the breadth
h double To read and store the height
volume double To calculate and display the volume
tsa double To calculate and display the total surface area
diagonal double To calculate and display the diagonal
➢ Output:
4. Write a program in Java to accept radius and height and print the volume of
cone, cylinder and sphere as per the user’s choice. (Using switch case)
1
Volume of cone = 𝜋𝑟 2 ℎ
3
Volume of cylinder = 𝜋𝑟 2 ℎ
4
Volume of sphere = 𝜋𝑟 3
3
➢ Algorithm:
2. Read the radius and height of the cone, cylinder, or sphere from the user using the
Scanner class and store them in variables r and h respectively.
3. Display the message "Enter 1 for volume of cone, 2 for volume of cylinder, and 3
for volume of sphere:".
4. Read the choice of the user (1, 2, or 3) and store it in the variable ch.
5. Use a switch statement with ch as the control variable to calculate and print the
volume based on the user's choice:
- If ch is equal to 1, calculate the volume of the cone using the formula (1/3 * 3.14 *
r * r * h) and print the result.
- If ch is equal to 2, calculate the volume of the cylinder using the formula (3.14 * r
* r * h) and print the result.
- If ch is equal to 3, calculate the volume of the sphere using the formula (4/3 * 3.14
* r * r * r) and print the result.
➢ Output:
5. Write a java program to input a number and determine the number of digits.
Now form an integer that uses number of digits and the significant digit at the
one’s place. Display the number.
Sample input: 2136
Sample output: 46
➢ Algorithm:
2. Read an integer number from the user using the Scanner class and store it in the
variable n.
3. Calculate the number of digits in the input number by converting it to a string using
the `String.valueOf() method and then getting the length of the resulting string. Store
the result in the variable numberOfDigits.
4. Calculate the significant digit at the one's place of the input number by taking the
modulus of n with 10. Store the result in the variable significantDigit.
5. Form the target number by multiplying the numberOfDigits by 10 and adding the
significantDigit. Store the result in the variable formedNumber.
import java.util.*;
➢ Variable Description:
Variable Datatype Description
n int To read and store the value of n
numberOfDigits int To read and store the number of digits
significantDigit int To read and store the significant digit
formedNumber int To calculate the formed number
➢ Output:
6. Write a program in Java to accept a three-digit number and display all the
combinations of the three-digit. No digit should be repeated more than once.
➢ Algorithm:
4. Check if the entered number is less than 100 or greater than 999.
5. Extract the first digit of the number by dividing 'n' by 100 and store it in the
variable 'digit1'.
6. Extract the second digit of the number by getting the remainder of 'n' divided by
100, then dividing the result by 10. Store it in the variable 'digit2'.
7. Extract the third digit of the number by getting the remainder of 'n' divided by 10.
Store it in the variable 'digit3'.
8. Create an integer array 'a' with elements 'digit1', 'digit2', and 'digit3' in that order.
9. Check if 'digit1', 'digit2', and 'digit3' are all different (not repeated).
import java.util.*;
class three_digit_combinations
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a three digit number:");
int n=sc.nextInt();
if (n<100 || n>999)
//To check whether entered number is a three digit number or not
{
System.out.println("Invalid input. Please enter a three-digit number.");
}
else
{
int digit1 = n/100;
int digit2 = (n%100)/10;
int digit3 = n%10;
//Separating the number in three-digits
int a[] = {digit1, digit2, digit3};
if (digit1 != digit2 && digit1 != digit3 && digit2 != digit3)
//To check whether digits are repeated or not
{
for (int i=0; i<3; i++)
//Usage of loops to make different possible combinations
{
for (int j=0; j<3; j++)
{
for (int k=0; k<3; k++)
{
if (i!=j && j!=k && k!=i)
{
System.out.println(a[i] + "" + a[j] + "" + a[k]);
//To print the combinations
}
}
}
}
}
else
{
System.out.println("Invalid input. Digits should not be repeated.");
}
}
}
}
➢ Variable Description:
Variable Datatype Description
n int To read and store three-digit number
digit1 int To store first digit of the number
digit2 int To store second digit of the number
digit3 int To store third digit of the number
a int To store the three digits in an array
i int Loop variable
j int Loop variable
k int Loop variable
➢ Output:
7. Write a program in java to accept n value and print first n prime numbers.
➢ Algorithm:
class one_to_n_prime_number
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the value of n");
int n=sc.nextInt();
System.out.println("Prime numbers between 1 to " +n + " are:");
//Start checking from 2
for (int i=2; i<=n; i++)
{
int count=0;
for (int j=1; j<=i; j++)
{
//To check whether number is prime
if (i % j==0)
{
count++;
}
}
if(count==2)
System.out.print(i + " ");
//To print prime numbers till n
}
}
}
➢ Variable Description:
Variable Datatype Description
i int Loop variable
j int Loop variable
n int To print prime numbers till n
count int To count the prime number
➢ Output:
8. Write a program in Java to print the following sum series:
𝟏∗𝟐 𝟏∗𝟐∗𝟑 𝟏∗𝟐∗𝟑…∗𝒏
S= + +⋯
𝟏+𝟐 𝟏+𝟐+𝟑 𝟏+𝟐+𝟑…+𝒏
➢ Algorithm:
4. Declare and initialize a variable 'sum' to 0 to keep track of the sum of the series.
7. Start a loop from 'i' = 1 to 'n' (inclusive) to calculate the sum of the series.
- Calculate the partial sum by dividing 'factorial' by 'denominator' and casting the
result to a double.
➢ Algorithm:
7. Calculate the factorial of m using the factorial() method and store it in the variable
mFactorial.
8. Calculate the factorial of n using the factorial() method and store it in the variable
nFactorial.
9. Calculate the factorial of (n - m) using the factorial() method and store it in the
variable nMinusMFactorial.
10. Calculate the value of s by dividing nFactorial by the product of mFactorial and
nMinusMFactorial, and store it in the variable s.
➢ Output:
FUNCTION OVERLOADING
➢ Algorithm:
3. Inside the class, declare variables "a", "b", "c", "d", "x1", and "x2" as floats.
4. Create a constructor for the "Quad" class that accepts three float parameters "x",
"y", and "z".
- x1 = (-b + sqrt(d)) / (2 * a)
- x2 = (-b - sqrt(d)) / (2 * a)
6. Create the main method (public static void main) with the parameter "String args[]".
- Prompt the user to enter the value of "a" and read the float value into variable "x".
- Prompt the user to enter the value of "b" and read the float value into variable "y".
- Prompt the user to enter the value of "c" and read the float value into variable "z".
- Create an object of the "Quad" class using the values of "x", "y", and "z".
import java.util.*;
class Quad
{
float a, b, c, d, x1, x2;
public Quad (float x, float y, float z)
{
//to initialize a=x, b=y, c=z, d=0
a = x;
b = y;
c = z;
d = 0;
}
public void calculate()
{
//To calculate the roots of the equation
d= (b * b) - (4 * a * c);
if (d < 0)
System.out.println("Roots not possible");
else
{
x1 = (float)((-b + Math.sqrt(d)) / (2 * a));
x2 = (float)((-b - Math.sqrt(d)) / (2 * a));
System.out.println("x1=" + x1);
System.out.println("x2=" + x2);
}
}
public static void main(String args[])
{
//Main method to accept values of a, b, c
Scanner sc = new Scanner(System.in);
System.out.print("Enter a: ");
float x = sc.nextFloat();
System.out.print("Enter b: ");
float y = sc.nextFloat();
System.out.print("Enter c: ");
float z = sc.nextFloat();
Quad obj = new Quad(x, y, z);
obj.calculate();
}
}
➢ Variable Description:
Variable Datatype Description
a float To read and store the value of a from the equation
b float To read and store the value of b from the equation
c float To read and store the value of c from the equation
d float To calculate the value of discriminant using a, b, c
x1 float To calculate the root1 from discriminant
x2 float To calculate the root2 from discriminant
➢ Output:
2. Define a class Discount having the following description:
Class name: Discount
Data Members:
int cost - to store the price of an article
String name - to store the customer's name
double dc - to store the discount
double amt - to store the amount to be paid
Member methods:
void input() - Stores the cost of the article and name of the customer
void cal() - Calculates the discount and amount to be paid
void display() - Displays the name of the customer, cost, discount and amount
to be paid
Write a program to compute the discount according to the given
conditions and display the output as per the given format.
List Price Rate of discount
Up to ₹5,000 No discount
Output:
Name of the customer Discount Amount to be paid
.................... ........ .................
.................... ........ .................
➢ Algorithm:
3. Inside the class, declare variables "cost" (integer), "name" (String), "dc" (double),
and "amt" (double).
- Prompt the user to enter the customer name and read the string value into the "name"
variable.
- Prompt the user to enter the article cost and read the integer value into the "cost"
variable.
- Check the value of "cost" using if-else statements to determine the discount amount:
- If "cost" is greater than 5000 and less than or equal to 10000, calculate the
discount by multiplying "cost" by 0.1 and store the result in "dc".
- If "cost" is greater than 10000 and less than or equal to 15000, calculate the
discount by multiplying "cost" by 0.15 and store the result in "dc".
- Calculate the amount to be paid by subtracting "dc" from "cost" and store the result
in "amt".
7. Create the main method (public static void main) with the parameter "String args[]".
- Call the "input" method on the "obj" object to accept customer details.
- Call the "cal" method on the "obj" object to calculate the discount and amount.
- Call the "display" method on the "obj" object to display the customer details,
discount, and amount.
➢ Variable Description
Variable Datatype Description
cost int To read and store the price of an article
name String To read and store the customer’s name
dc double To calculate and store the discount
amt double To calculate and display the amount
➢ Output:
3. Bank charges interest for the vehicle loan as given below:
Number of years Rate of interest
Up to 5 years 15%
➢ Algorithm:
3. Inside the class, declare variables "time" (integer), "principal" (double), "rate"
(double), "interest" (double), and "amt" (double).
- Prompt the user to enter the time and read the integer value into the "time" variable.
- Check the value of "time" using if-else statements to determine the interest rate:
- If "time" is greater than 5 and less than or equal to 10, set "rate" to 12.0.
- Calculate the interest by multiplying "principal", "rate", and "time" and dividing the
result by 100.0. Store the result in "interest".
- Calculate the total amount payable by adding "principal" and "interest" and store the
result in "amt".
7. Create the main method (public static void main) with the parameter "String args[]".
- Call the "getdata" method on the "obj" object to accept the loan details.
- Call the "calculate" method on the "obj" object to calculate the interest and amount
payable.
- Call the "display" method on the "obj" object to display the interest and amount
payable.
➢ Output:
4. Define a class called Student to check whether a student is eligible for
taking admission in class XI with the following specifications:
Data Members:
String name - to store name
int mm - to store marks secured in Maths
int scm - to store marks secured in Science
double comp - to store marks secured in Computer
Member Methods:
Student( ) - parameterised constructor to initialize the data members by
accepting the details of a student
check( ) - to check the eligibility for course based on the table given below
display() - to print the eligibility by using check() function in nested form
Marks Eligibility
Average marks 80% or more and less than 90% Science with Hindi
Write the main method to create an object of the class and call all the
member methods.
➢ Algorithm:
3. Inside the class, declare variables "name" (String), "mm" (integer), "scm" (integer),
and "comp" (integer).
4. Create a parameterized constructor for the "Student" class that accepts four
parameters: "n" (String), "m" (integer), "sc" (integer), and "c" (integer).
- Declare a variable "course" and initialize it with the value "Not Eligible".
- Calculate the average of the marks by adding "mm", "scm", and "comp" and
dividing the sum by 3.0.
- If "mm" is greater than or equal to 90, "scm" is greater than or equal to 90,
and "comp" is greater than or equal to 90, set "course" to "Science with Computer".
- Else if the average is greater than or equal to 90, set "course" to "Bio-
Science".
- Else if the average is greater than or equal to 80, set "course" to "Science with
Hindi".
- Call the "check" method and assign the returned value to a variable named
"eligibility".
7. Create the main method (public static void main) with the parameter "String args[]".
- Prompt the user to enter the name and read the input into a variable named "n".
- Prompt the user to enter the marks in Maths and read the input into a variable named
"m".
- Prompt the user to enter the marks in Science and read the input into a variable
named "sc".
- Prompt the user to enter the marks in Computer and read the input into a variable
named "c".
- Create an object of the "Student" class named "obj" by passing the values of "n",
"m", "sc", and "c" to the constructor.
- Call the "display" method on the "obj" object to display the eligibility.
import java.util.*;
class Student
{
String name;
int mm;
int scm;
int comp;
Student(String n, int m, int sc, int c)
{
//Parameterized constructor to initialize data members by accepting details
name = n;
mm = m;
scm = sc;
comp = c;
}
String check()
{
//To check the eligibility for the course with the following conditions:
String course = "Not Eligible";
double avg = (mm + scm + comp) / 3.0;
if (mm >= 90 && scm >= 90 && comp >= 90)
course = "Science with Computer";
else if (avg >= 90)
course = "Bio-Science";
else if (avg >= 80)
course = "Science with Hindi";
return course;
}
void display()
{
String eligibility = check();
System.out.println("Eligibility: " + eligibility);
}
public static void main(String args[])
{
//To accept the details of the student
Scanner sc = new Scanner(System.in);
System.out.print("Enter Name: ");
String n = sc.nextLine();
System.out.print("Enter Marks in Maths: ");
int m = sc.nextInt();
System.out.print("Enter Marks in Science: ");
int sc = sc.nextInt();
System.out.print("Enter Marks in Computer: ");
int c = sc.nextInt();
Student obj = new Student(n, m, sc, c);
obj.display();
}
}
➢ Variable Description:
Variable Datatype Description
name String To read and store the name of the student
mm int To read and store the marks secured in maths
scm int To read and store the marks secured in science
comp int To read and store the marks in computer
avg double To calculate and store the average marks of all subjects
course String To select course as per the average marks
eligibility String To check whether the student is eligible for respective
course or not
➢ Output:
5. Define a class Salary described as below:
Class name: Salary
Data Members: name, address, phone, subject, specialization, monthly salary,
incometax
Member Methods:
(i) To accept details of the teacher including monthly salary
(ii) To display details of the teacher
(iii) To compute annual income tax at 5% of annual salary above Rs.
175000.
➢ Algorithm:
3. Inside the class, declare variables "name" (String), "address" (String), "subject"
(String), "specialization" (String), "salary" (double), "incometax" (double), and
"phone" (long).
- Prompt the user to enter the name and read the input into the "name" variable.
- Prompt the user to enter the address and read the input into the "address" variable.
- Prompt the user to enter the subject and read the input into the "subject" variable.
- Prompt the user to enter the specialization and read the input into the "specialization"
variable.
- Prompt the user to enter the phone number and read the input into the "phone"
variable.
- Prompt the user to enter the salary and read the input into the "salary" variable.
- Print "Phone number: " followed by the value of the "phone" variable.
- Print "Monthly salary: " followed by the value of the "salary" variable.
- If true, calculate the income tax by multiplying the annual salary by 0.05 and
assign it to the "incometax" variable.
- Print "Income Tax: " followed by the value of the "incometax" variable.
7. Create the main method (public static void main) with the parameter "String args[]".
- Call the "accept" method on the "obj" object to accept the details of the teacher.
- Call the "display" method on the "obj" object to display the details of the teacher.
- Call the "compute" method on the "obj" object to calculate the income tax.
class Salary
long phone;
void accept()
name = sc.nextLine();
address = sc.nextLine();
subject = sc.nextLine();
specialization = sc.nextLine();
phone = sc.nextLong();
salary = sc.nextDouble();
System.out.println();
}
void display()
System.out.println("Name: "+name);
System.out.println("Address: "+address);
System.out.println("Subject: "+subject);
System.out.println("Specialization: "+specialization);
void compute()
else
incometax = 0.0;
obj.accept();
obj.display();
obj.compute();
➢ Variable Description
Variable Datatype Description
name String To read and store the name of the teacher
address String To read and store the address of the teacher
phone long To read and store the phone number of the teacher
subject String To read and store the subject he/she teaches
specialization String To read and store in which field he/she is specialized
salary double To read and store the monthly salary of the teacher
incometax double To calculate and store the income tax
➢ Output:
6. Define a class Pay with the following specifications:
Data Members:
String name, address, city, float salary
Member Functions:
Pay (String n, String add, String cy, float s)
void outputdata() – to display all initialized values
void calculate() — To calculate the following salary components:
Dearness Allowance = 15% of salary
House Rent Allowance = 10% of salary
Provident Fund = 12% of salary
Gross Salary = Salary + Dearness Allowance + House Rent
Allowance
Net Salary = Gross Salary - Provident Fund
void display() - to display all details of the employee
Write a main method to create object of the class and call the member
methods.
➢ Algorithm:
4. Inside the class, declare variables "name" (String), "address" (String), "city"
(String), "salary" (float), "da" (double), "hra" (double), "pf" (double), "gross"
(double), and "net" (double).
5. Create a constructor for the class with parameters "n" (String), "add" (String), "cy"
(String), and "s" (float).
- Initialize the "name" variable with the value of the "n" parameter.
- Initialize the "address" variable with the value of the "add" parameter.
- Initialize the "city" variable with the value of the "cy" parameter.
- Initialize the "salary" variable with the value of the "s" parameter.
- Calculate the house rent allowance (hra) by multiplying the salary by 0.1.
- Calculate the gross salary by adding the salary, da, and hra.
- Calculate the net salary by subtracting the pf from the gross salary.
- Print "Employee Name: " followed by the value of the "name" variable.
- Print "Dearness Allowance: " followed by the value of the "da" variable.
- Print "House Rent Allowance: " followed by the value of the "hra" variable.
- Print "Provident Fund: " followed by the value of the "pf" variable.
- Print "Gross Salary: " followed by the value of the "gross" variable.
- Print "Net Salary: " followed by the value of the "net" variable.
9. Create the main method (public static void main) with the parameter "String args[]".
- Prompt the user to enter the address and read the input into the "add" variable.
- Prompt the user to enter the city and read the input into the "cy" variable.
- Prompt the user to enter the salary and read the input into the "s" variable.
- Create an object of the "Pay" class named "obj" by calling the constructor with the
"n", "add", "cy", and "s" variables.
- Call the "outputdata" method on the "obj" object to display the initialized values.
- Call the "calculate" method on the "obj" object to calculate the salary components.
- Call the "display" method on the "obj" object to display all the details of the
employee.
import java.util.*;
class Pay
{
String name, address, city;
float salary;
double da, hra, pf, gross, net;
public Pay(String n, String add, String cy, float s)
{
name = n;
address = add;
city = cy;
salary = s;
}
void outputdata()
{
//To display all initialized values
System.out.println("Name: "+name);
System.out.println("Address: "+address);
System.out.println("City: "+city);
System.out.println("Salary: "+salary);
System.out.println();
}
void calculate()
{
//To calculate the following salary components:
da = salary * 0.15;
hra = salary * 0.1;
pf = salary * 0.12;
gross = salary + da + hra;
net = gross - pf;
}
void display()
{
//To display all details of the employee
System.out.println("Employee Name: " + name);
System.out.println("Salary: " + salary);
System.out.println("Dearness Allowance: " + da);
System.out.println("House Rent Allowance: " + hra);
System.out.println("Provident Fund: " + pf);
System.out.println("Gross Salary: " + gross);
System.out.println("Net Salary: " + net);
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter Name: ");
String n = sc.nextLine();
System.out.print("Enter Address: ");
String add = sc.nextLine();
System.out.print("Enter City: ");
String cy = sc.nextLine();
System.out.println("Enter Salary: ");
float s = sc.nextFloat();
System.out.println();
Pay obj = new Pay(n, add, cy, s);
obj.outputdata();
obj.calculate();
obj.display();
}
}
➢ Variable Description:
Variable Datatype Description
name String To read and store the name of the employee
address String To read and store the address of the employee
city String To read and store the city where the employee lives
salary double To read and store the monthly salary of the employee
da double To calculate and store the dearness allowance
hra double To calculate and store the house rent allowance
pf double To calculate and store the provident fund
gross double To calculate and store the gross salary
net double To calculate and store the net salary
➢ Output: