Download as pdf or txt
Download as pdf or txt
You are on page 1of 193

Academic Year : 2022 - 2023

Computer
Applications
Project
By :
Sabarish.E
X – ‘A’
Program 1:
Shibi
Question : Write a program to calculate the time period of a Simple Pendulum by taking
length and acceleration due to gravity (g) as inputs.
Program :
import java.util.*;
class prg1{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
final double Pi = 22/7;
System.out.println("Enter the length and acceleration due to gravity");
int l = sc.nextInt();
int g = sc.nextInt();
double t = 2*Pi*Math.sqrt(l/g);
System.out.println("The time period of the pendulum is "+t);
}
}
Output :

Variable Descritpion:
Variable Type Purpose
Pi Double To store constant value of Pi
l Int To store length of pendulum
g Int To input the acceleration due to gravity.
t double To calculate the time period of pendulum.
Program 2:
Question : Write a program by using class 'Employee' to accept Basic Pay of an employee.
Calculate the allowances/deductions as given below.

Allowance / Deduction Rate

Dearness Allowance (DA) 30% of Basic Pay

House Rent Allowance (HRA) 15% of Basic Pay

Provident Fund (PF) 12.5% of Basic Pay

Finally, find and print the Gross and Net pay.

Program :
import java.util.*;

class Employee{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the basic pay");
int pay = sc.nextInt();
double da = 0.3 * pay;
double hra = 0.15 * pay;
double pf = 0.125 * pay;
double gross = pay + da + hra;
double net = gross - pf;
System.out.println("Gross Pay = "+gross);
System.out.println("Net pay = "+net);
}
}
Output :

Variable Description:
Variable Type Purpose
pay Int To input the salary of employee.
da Double To calculate Dearness Allowance.
hra Double To calculate House Rent Allowance
pf Double To calculate Provident Fund
gross Double To calculate gross pay.
net Double To calculate net pay.
Program 3:

Question : A shopkeeper offers 10% discount on the printed price of a Digital Camera.
However, a customer has to pay 6% GST on the remaining amount. Write a program in
Java to calculate the amount to be paid by the customer taking printed price as an input.
Program :
import java.util.*;

class prg3{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Printed Price");
int p = sc.nextInt();
double d = 0.1 * p;
double dp = p - d;
double gst = 0.06 * dp;
double amount = dp + gst;
System.out.println("The discount is \u20B9"+d +"\nGST is \u20B9"+gst);
System.out.println("The total amount is \u20B9"+amount);
}
}
Output :
Variable Description:
Variable Type Purpose
p Int To input PrintedPrice.
d Double To store discount.
dp Double To store price after discount.
gst Double To store GST
amount Double To store GST price.
Program 4:
Question : A shopkeeper offers 30% discount on purchasing articles whereas the other
shopkeeper offers two successive discounts 20% and 10% for purchasing the same articles.
Write a program in Java to compute and display the discounts.
Take the price of an article as the input.
Program :
import java.util.*;

class prg4{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Cost Price.");
int price = sc.nextInt();
double dis1 = 0.3 * price;
double price2 = price - dis1;
System.out.println("Price after 30% discount is "+price2);
double dis2 = 0.2 * price;
double price3 = price - dis2;
double dis3 = 0.1 * price3;
double price4 = price3 - dis3;
System.out.println("Price after succesive discounts is "+price4);

}
}
Output :
Variable Description:
Variable Type Purpose
price Int To input cost price.
dis1 Double To store discount 1
price2 Double To store price after dis1.
dis2 Double To store discount 2
price3 Double To store price after dis2
dis3 Double To store discount3
price4 Double To store the final price after dis3
Program 5:
Question : Mr. Agarwal invests certain sum at 5% per annum compound interest for three
years. Write a program in Java to calculate:

(a) the interest for the first year


(b) the interest for the second year
(c) the amount after three years.
Take sum as an input from the user.

Program :
import java.util.*;
class prg5{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the sum of money");
double sum = sc.nextInt();
double in1 = sum * 5* 1/100.0;
System.out.println("Interst for the first year is \u20B9"+in1);
sum += in1;
double in2 = sum * 5 * 1 / 100.0;
System.out.println("Interst after second year is \u20B9"+in2);
sum += in2;
double in3 = sum * 5 * 1 /100.0;
sum += in3;
System.out.println("Amount after three years is \u20B9"+sum);
}
}
Output :
Variable Description:
Variable Type Purpose
sum Double To input the amount of money
in1 Double To calculate interest for first year
in2 Double To calculate interest for second year.
in3 Double To calculate interst for third year.
Program 6:
Question : A businessman wishes to accumulate 3000 shares of a company. However, he
already has some shares of that company valuing ₹10 (nominal value) which yield 10%
dividend per annum and receive ₹2000 as dividend at the end of the year. Write a program
in Java to calculate the number of shares he has and how many more shares to be purchased
to make his target.

Program :
class prg6{
public static void main(String args[]){
int num = (2000 * 100)/(10 * 10);
System.out.println("Number of shares currently held is "+num);
int want = 3000 - num;
System.out.println("No. of Shares needed to reach 3000 is "+want);
}
}
Output :

Variable Description:
Variable Type Purpose
num Int To calculate number of shares currently held.
want Int To calculate the number of shares required.
Program 7:
Question : Write a program to input the time in seconds. Display the time after converting
them into hours, minutes and seconds.

Program :
import java.util.*;
class prg7{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter time in seconds");
int sec = sc.nextInt();
int hour = sec/3600;
sec = sec % 3600;
int min = sec / 60;
sec = sec % 60;
System.out.println(hour+" Hours "+min+" Minutes "+sec+" Seconds");
}
}
Output :

Variable Description:
Variable Type Purpose
sec Int To input time in seconds
hour Int To calculate hours
min Int To calculate mins
Program 8:
Question : Write a program to input two unequal numbers. Display the numbers after
swapping their values in the variables without using a third variable.

Program :
import java.util.*;
class prg8{
public static void main(String args[])
Scanner sc = new Scanner(System.in);
System.out.println("Enter value of A");
int a = sc.nextInt();
System.out.println("Enter value of B");
int b = sc.nextInt();
a = a+b;
b = a-b ;
a = a-b;
System.out.println("A = "+a+"\nB = "+b);
}
}
Output :
Variable Description:

Variable Type Purpose


a Int To input first number.
b int To input second number.
Program 9:
Question : A certain amount is invested at the rate 10% per annum for 3 years. Find the
difference between Compound Interest (CI) and Simple Interest (SI). Write a program to
take amount as an input.

Program :
import java.util.*;
class prg9{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the sum of money invested");
int sum = sc.nextInt();
double si = sum * 10 * 3 / 100.0;
double amount =sum*Math.pow(1+(10/100.0),3);
double ci = amount - sum;
double dif = ci - si;
System.out.println("Simple Interest : \u20B9"+si);
System.out.println("Compound Interest : \u20B9"+ci);
System.out.println("Differnce between CI and SI : \u20B9"+dif);
}
}
Output :
Variable Description:

Variable Type Purpose


sum Int To input the sum invested.
si Double To calculate the simple interest
amount Double To calculate amount
ci Double To calculate the compound interest
dif Double To find difference between ci and si.
Program 10:
Question : A shopkeeper sells two calculators for the same price. He earns 20% profit on
one and suffers a loss of 20% on the other. Write a program to find his total cost price of
the calculators by taking selling price as input.

Program :
import java.util.*;
class prg10{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the selling price of the calculators");
double sp = sc.nextDouble();
double cpProfit = sp/(1 + 20/100.0);
double cpLoss = sp/(1 - 20/100.0);
double total = cpProfit + cpLoss;
System.out.println("The Total cost Price = \u20B9"+total);
}
}
Output :

Variable Description:
Variable Type Purpose
sp Double To input the selling price.
cpProfit Double To calculate cost price of profit sale.
cpLoss Double To calculate cost price of loss sale.
total Double To find the total price of 2 calculators.
Program 11:
Question : Write a program to input three angles of a triangle and check whether a triangle
is possible or not. If possible then check whether it is an acute-angled triangle, right-angled
or an obtuse-angled triangle otherwise, display 'Triangle not possible'.

Program :
import java.util.*;
class prg1{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter three angle of a triangle");
int a = sc.nextInt();
int b= sc.nextInt();
int c = sc.nextInt();
int total = a+b+c;
if(total == 180){
if (a < 90 && b < 90 && c < 90) {
System.out.println("Acute-angled Triangle");
}
else if (a == 90 || b == 90 || c == 90) {
System.out.println("Right-angled Triangle");
}
else {
System.out.println("Obtuse-angled Triangle");
}
}else{
System.out.println("Triangle not possible");
}
}
}
Output :

Variable Type Purpose


a Int To input one angle of the triangle.
b Int To input one angle of the triangle.
c Int To input one angle of the triangle.
total Int To store the total of a,b and c.
Program 12:
Question : Write a program to input the cost price and the selling price of an article. If the
selling price is more than the cost price then calculate and display actual profit and profit
per cent otherwise, calculate and display actual loss and loss per cent. If the cost price and
the selling price are equal, the program displays the message 'Neither profit nor loss'.

Program :
import java.util.*;
class prg2{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the cost price");
double cp = sc.nextInt();
System.out.println("Enter the selling price");
double sp = sc.nextInt();
if(sp > cp){
double profit = sp - cp;
double profitp = profit / cp * 100.0;
System.out.println("Profit is \u20B9"+profit+"\nProfit percent is "+profitp);
}else if(cp > sp){
double loss = cp - sp;
double lossp = loss / cp * 100.0;
System.out.println("Loss is \u20B9"+loss+"\nLoss percent is "+lossp);
}else{
System.out.println("Neither profit nor loss");
}
}
}
Output :

Variable Description:
Variable Type Purpose
cp Double To input the cost price
sp Double To input the selling price.
profit Double To calculate the profit
profitp Double To calculate the profit percentage.
loss Double To calculate the loss
lossp Double To calculate the loss percentage.
Program 13:
Question : Write a program to input three numbers and check whether they are equal or
not. If they are unequal numbers then display the greatest among them otherwise, display
the message 'All the numbers are equal'.

Program :
import java.util.*;
class prg3{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter three numbers");
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
if(a == b && b == c){
System.out.println("All the numbers are equal");
}else{
int d = Math.max(Math.max(a,b),c);
System.out.println("Greatest nuber is "+d);
}
}
}
Output :
Variable Description:
Variable Type Purpose
a Int To input a number.
b Int To input a number.
c Int To input a number.
d int To find the greatest number.
Program 14:
Question : Write a program to accept a number and check whether the number is
divisible by 3 as well as 5. Otherwise, decide:
(a) Is the number divisible by 3 and not by 5?
(b) Is the number divisible by 5 and not by 3?
(c) Is the number neither divisible by 3 nor by 5?

Program :
import java.util.*;
class prg4{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
int num = sc.nextInt();
if(num % 3 == 0 && num % 5 == 0){
System.out.println("Number is divisble by both 3 and 5");
}else if(num % 3 == 0 && num % 5 != 0 ){
System.out.println("Number is divisble by 3 and not by 5");
}else if(num % 3 != 0 && num % 5 == 0 ){
System.out.println("Number is divisble by 5 and not by 3");
}else{
System.out.println("The number is neither divisble by 5 nor by 3");
}
}
}
Output :
Variable Description:
Variable Type Purpose
num Int To input a number
Program 15:
Question : Write a program to input year and check whether it is:
(a) a Leap year (b) a Century Leap year (c) a Century year but not a Leap year

Program :
import java.util.*;
class prg5{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a year");
int year = sc.nextInt();
if(year % 4 == 0 && year %100 != 0){
System.out.println("The year is a leap year");
}else if(year % 4 == 0 && year %100 == 0){
System.out.println("The year is a Century leap year");
}else if(year % 4 != 0 && year %100 == 0){
System.out.println("The year is a Century year");
}else{
System.out.println("The year is neither a Century year nor a Leap year.");
}
}
}
Output :

Variable Description:
Variable Type Purpose
year Int To input a year
Program 16:
Question : Write a program to input two unequal positive numbers and check whether they
are perfect square numbers or not. If the user enters a negative number then the program
displays the message 'Square root of a negative number can't be determined'.

Program :
import java.util.*;
class prg6{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter two positive numbers");
int a = sc.nextInt();
int b = sc.nextInt();
if(a < 0 || b < 0){
System.out.println("Square root of negative numbers cannot be determined");
}else{
double sqrtA = Math.sqrt(a);
double sqrtB = Math.sqrt(b);
double c = sqrtA - Math.floor(sqrtA);
double d = sqrtB - Math.floor(sqrtB);
if(c == 0 && d ==0){
System.out.println("The numbes are perfect squares");
}else if(c == 0 && d != 0 ){
System.out.println(a+" is a Perfect Square"+"\n"+b+" is not a Perfect
Square");
}else if(c != 0 && d == 0 ){
System.out.println(a+" is not a Perfect Square"+"\n"+b+" is a Perfect
Square");

}else if(c != 0 && d != 0 ){


System.out.println("Both the numbers are not perfect squares ");
}
}
}
}
Output :

Variable Description:
Variable Type Purpose
a Int To input a number.
b Int To input a number.
sqrtA Double To store square root of a
sqrtB Double To store square root of b
c Double To store rounded off value of sqrtA
d Double To store rounded off value of sqrtB
Program 17:
Question : Without using if-else statement and ternary operators, accept three unequal
numbers and display the second biggest number.

Program :
import java.util.*;
class prg7{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter three numbers");
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int sum = a+b+c;
int big,small,result;
big = Math.max(Math.max(a,b),c);
small = Math.min(Math.min(a,b),c);
result = sum - big - small;
System.out.println("The second biggest number is "+result);
}
}
Output :
Variable Description:
Variable Type Purpose
a Int To input a number.
b Int To input a number.
c Int To input a number.
sum Int To store the sum of a,b and c
big Int To store the biggest number.
small Int To store the smallest number.
result Int To find the second biggest number.
Program 18:
Question : Write a program to input three unequal numbers. Display the greatest and the
smallest number.
Program :
import java.util.*;
class prg8{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter three numbers");
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int max = Math.max(Math.max(a,b),c);
int min = Math.min(Math.min(a,b),c);
System.out.println("Greatest number is "+max);
System.out.println("Smallest number is "+min);
}
}
Output :
Variable Description:

Variable Type Purpose


a Int To input a number.
b Int To input a number.
c Int To input a number.
min Int To find the smallest number.
max Int To find the greatest number.
Program 19:
Question : A Pre-Paid taxi charges from the passenger as per the tariff given below:

Distance Rate

Up to 5 km ₹ 100

For the next 10 km ₹ 10/km

For the next 10 km ₹ 8/km

More than 25 km ₹ 5/km

Write a program to input the distance covered and calculate the amount paid by the
passenger. The program displays the printed bill with the details given below:
Taxi No. :
Distance covered :
Amount :

Program :
import java.util.*;
class prg9{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the distance covered");
double dis = sc.nextInt();
double fare;
if(dis <= 5) {
fare = 100;
}else if(dis > 5 && dis <= 15 ){
fare = (dis - 5) * 10;
fare += 100;
}else if(dis > 15 && dis <= 25){
fare = 100 + 100 + (dis - 15) * 8;
}else{
fare = 100 + 100 + 80 + (dis - 25) * 5;
}
System.out.println("Taxi no. : TN 66 AD 7921");
System.out.println("Distance covered : "+dis);
System.out.println("Amount \u20B9: "+fare);
}
}
Output :

Varible Description:
Variable Type Purpose
dis Double To input the distance travelled.
fare Double To calculate the fare.
Program 20:
Question : A cloth showroom has announced festival discounts and the gifts on the
purchase of items, based on the total cost as given below:

Total Cost Discount Gift

Up to ₹ 2,000 5% Calculator

₹ 2,001 to ₹ 5,000 10% School Bag

₹ 5,001 to ₹ 10,000 15% Wall Clock

Above ₹ 10,000 20% Wrist Watch

Write a program to input the total cost. Compute and display the amount to be paid by the
customer along with the gift.

Program :
import java.util.*;

class prg10{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.print("Enter total cost: ");
double cost = sc.nextDouble();
String gift;
double amt;
if (cost <= 2000) {
amt = cost - (cost * 5 / 100);
gift = "Calculator";
}else if (cost > 2000 && cost <= 5000 ) {
amt = cost - (cost * 10 / 100);
gift = "School Bag";
}else if (cost > 5000 && cost <= 10000) {
amt = cost - (cost * 15 / 100);
gift = "Wall Clock";
}else {
amt = cost - (cost * 20 / 100);
gift = "Wrist Watch";
}
System.out.println("Amount to be paid: " + amt);
System.out.println("Gift: " + gift);
}
}
Output :

Variable description:
Variable Type Purpose
cost Double To input the total cost paid by the customer.
gift String To store the gift received by the customer.
amt Double To store the final amount paid by the customer.
Program 21:
Question : Given below is a hypothetical table showing rate of income tax for an India
citizen, who is below or up to 60 years.

Taxable income (TI) in ₹ Income Tax in ₹

Up to ₹ 2,50,000 Nil

More than ₹ 2,50,000 and less than or equal to ₹


(TI - 1,60,000) * 10%
5,00,000

More than ₹ 5,00,000 and less than or equal to ₹


(TI - 5,00,000) * 20% + 34,000
10,00,000

(TI - 10,00,000) * 30% +


More than ₹ 10,00,000
94,000

Write a program to input the name, age and taxable income of a person. If the age is more
than 60 years then display the message "Wrong Category". If the age is less than or equal
to 60 years then compute and display the income tax payable along with the name of tax
payer, as per the table given above.

Program :
import java.util.*;
class prg11{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter your name.");
String name = sc.nextLine();
System.out.println("Enter your age.");
int age = sc.nextInt();
System.out.println("Enter your income.");
double ti = sc.nextDouble();
double tax;
if(age > 60){
System.out.println("Wrong category");
}else{
if(ti <= 250000){
System.out.println("Name : "+name);
System.out.println("Tax amount : Nil");
}else if(ti > 250000 && ti <= 500000){
tax = (ti - 160000)*0.1;
System.out.println("Name : "+name);
System.out.println("Tax amount : \u20B9"+tax);
}else if(ti > 500000 && ti <= 1000000){
tax = (ti - 500000)* 0.2 + 34000;
System.out.println("Name : "+name);
System.out.println("Tax amount : \u20B9"+tax);
}else if(ti > 1000000){
tax = (ti - 1000000)* 0.2 + 94000;
System.out.println("Name : "+name);
System.out.println("Tax amount : \u20B9"+tax);
}
}
}
}
Output :
Variable Description:
Variable Type Purpose
name String To input the name of the person.
age Int To input the age of the person.
ti Double To input the income of the person.
tax Double To calculate the tax paid by the person.
Program 22:
Question

An employee wants to deposit certain sum of money under 'Term Deposit'


scheme in Syndicate Bank. The bank has provided the tariff of the scheme,
which is given below:

No. of Days Rate of Interest

Up to 180 days 5.5%

181 to 364 days 7.5%

Exact 365 days 9.0%

More than 365 days 8.5%

Write a program to calculate the maturity amount taking the sum and number of
days as inputs.

Program :
import java.util.*;
class prg12{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the sum of money deposited.");
double sum = sc.nextDouble();
System.out.println("Enter the No. of days.");
int days = sc.nextInt();
double amount;
double interest;
if(days <= 180){
interest = sum * 5.5/100.0;
amount = sum + interest;
System.out.println("Amount on maturity is \u20B9"+amount);
}else if(days > 180 && days <= 364){
interest = sum * 7.5/100.0;
amount = sum + interest;
System.out.println("Amount on maturity is \u20B9"+amount);
}else if(days == 365){
interest = sum * 9.0/100.0;
amount = sum + interest;
System.out.println("Amount on maturity is \u20B9"+amount);
}else if(days > 365 ){
interest = sum * 8.5/100.0;
amount = sum + interest;
System.out.println("Amount on maturity is \u20B9"+amount);
}
}
}
Output :

Variable Description:
Variable Type Purpose
sum Double To input the sum deposited.
amount Double To calculate the final amount.
days Int To input the number of days.
interest Double To find the interest amount.
Program 23:
Question : Mr. Kumar is an LIC agent. He offers discount to his policy holders on the
annual premium. However, he also gets commission on the sum assured as per the given
tariff.

Sum Assured Discount Commission

Up to ₹ 1,00,000 5% 2%

₹ 1,00,001 and up to ₹ 2,00,000 8% 3%

₹ 2,00,001 and up to ₹ 5,00,000 10% 5%

More than ₹ 5,00,000 15% 7.5%

Write a program to input name of the policy holder, the sum assured and first annual
premium. Calculate the discount of the policy holder and the commission of the agent. The
program displays all the details as:
Name of the policy holder :
Sum assured :
Premium :
Discount on the first premium :
Commission of the agent :

Program :
import java.util.*;
class prg13{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the name of the policy holder.");
String name = sc.nextLine();
System.out.println("Enter the sum assured.");
double sum = sc.nextDouble();
System.out.println("Enter the first annual premium");
double pre = sc.nextDouble();
double dis = 0.0,com = 0.0;
if(sum <= 100000){
dis = pre * 5.0/100.0;
com = sum * 2.0/100.0;
}else if(sum > 100000 && sum <= 200000){
dis = pre * 8.0/100.0;
com = sum * 3.0/100.0;
}else if(sum >= 200000 && sum <= 500000){
dis = pre * 10.0/100.0;
com = sum * 5.0/100.0;
}else if (sum > 500000){
dis = pre * 15.0/100.0;
com = sum * 7.5/100.0;
}
System.out.println("Name of policy holder : "+name);
System.out.println("Sum assured : \u20B9"+sum);
System.out.println("Premium : \u20B9"+pre);
System.out.println("Discount on first premium : \u20B9"+dis);
System.out.println("Commission of the agent : \u20B9"+com);
}
}
Output :
Variable Description:
Variable Type Purpose
name String To input the name of the person
sum Double To input the sum assured.
pre Double To input the annual premium.
dis Double To store the discount on premium.
com Double To store the agent’s commission
Program 24:
Question : A company announces revised Dearness Allowance (DA) and Special
Allowances (SA) for their employees as per the tariff given below:

Basic Dearness Allowance (DA) Special Allowance (SA)

Up to ₹ 10,000 10% 5%

₹ 10,001 - ₹ 20,000 12% 8%

₹ 20,001 - ₹ 30,000 15% 10%

₹ 30,001 and above 20% 12%

Write a program to accept name and Basic Salary (BS) of an employee.


Calculate and display gross salary.
Gross Salary = Basic + Dearness Allowance + Special Allowance
Print the information in the given format:
Name Basic DA Spl. Allowance Gross Salary
xxx xxx xxx xxx xxx
Program :
import java.util.*;
class prg14{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the name.");
String name = sc.nextLine();
System.out.println("Enter the basic pay");
double basic = sc.nextDouble();
double da = 0.0, sa = 0.0;
if(basic <= 10000){
da = basic * 10.0/100.0;
sa = basic * 5.0/100.0;
}else if(basic > 10000 && basic <= 20000 ){
da = basic * 12.0/100.0;
sa = basic * 8.0/100.0;
}else if(basic > 20000 && basic <= 30000 ){
da = basic * 15.0/100.0;
sa = basic * 10.0/100.0;
}else if(basic > 30000){
da = basic * 20.0/100.0;
sa = basic * 12.0/100.0;
}
double gross = basic + da + sa;
System.out.println("Name\tBasic\tDA\tSpl.Allowance\tGross Salary");
System.out.println(name+"\t"+basic+"\t"+da+"\t"+sa+"\t \t"+gross);
}
}
Output :

Variable Description:

Variable Type Purpose

name String To input the name.

basic Double To input the basic pay.

da Double To calculate the dearness allowance

sa Double To calculate the special allowance

gross Double To calculate the gross pay.


Program 25:
Question : Using a switch case statement, write a menu driven program to convert a given
temperature from Fahrenheit to Celsius and vice-versa. For an incorrect choice, an
appropriate message should be displayed.
Program :
import java.util.*;
class prg15{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Type 1 to convert Farenheit to Celsius");
System.out.println("Type 2 to convert Celsius to Farenheit");
int num = sc.nextInt();
double f = 0.0, c = 0.0;
switch(num){
case 1 :
System.out.println("Enter temperature in Farenheit");
f = sc.nextDouble();
c = 5.0/9.0 * (f - 32);
System.out.println("Temperature in Celsius : "+c);
break ;
case 2 :
System.out.println("Enter temperature in Celsius");
c = sc.nextDouble();
f = 1.8 * c+32;
System.out.println("Temperature in Farenheit : "+f);
break ;
default :
System.out.println("Enter 1 or 2 and try again");
}
}
}
Output :

Variable Description:
Variable Type Purpose
num Int To input user‟s choice.
f Double To store temperature in Fahrenheit
c Double To store temperature in Celsius.
Program 26:
Question : The volume of solids, viz. cuboid, cylinder and cone can be calculated by the
formula:

1. Volume of a cuboid (v = l*b*h)


2. Volume of a cylinder (v = π*r2*h)
3. Volume of a cone (v = (1/3)*π*r 2*h)

Using a switch case statement, write a program to find the volume of different solids by
taking suitable variables and data types.

Program :
import java.util.*;
class prg16{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter 1 to find volume of cuboid.");
System.out.println("Enter 2 to find volume of cylinder.");
System.out.println("Enter 3 to find volume of cone.");
int choice = sc.nextInt();
final double Pi = 22/7;
switch(choice){
case 1 :
System.out.println("Enter length, bredth and height of a cuboid");
double l = sc.nextDouble();
double b = sc.nextDouble();
double h = sc.nextDouble();
double v = l*b*h;
System.out.println("Volume of the cuboid is "+v);
break;
case 2 :
System.out.println("Enter radius and height of a cylinder");
double r = sc.nextDouble();
double hei = sc.nextDouble();
double vol = Pi * Math.pow(r,2) * hei;
System.out.println("Volume of the cylinder is "+vol);
break;
case 3 :
System.out.println("Enter radius and height of a cone");
double rad = sc.nextDouble();
double height = sc.nextDouble();
double volume = 1/3.0 * Pi * Math.pow(rad,2) * height;
System.out.println("Volume of the cone is "+volume);
break;
default :
System.out.println("Invalid Input!!!\nTry again.");
}
}
}
Output :
Variable Description:
Variable Type Purpose
choice Int To store the choice of the user.
Pi Double To store the constant value of Pi
l Double To store length of cuboid.
b Double To store breadth of cuboid.
h Double To store height of cuboid.
v Double To store volume of cuboid.
r Double To store radius of cylinder
hei Double To store height of cylinder.
vol Double To store volume of cylinder.
rad Double To store radius of cone.
height Double To store height of cone.
volume Double To store volume of cone.
Program 27:
Question : The Simple Interest (SI) and Compound Interest (CI) of a sum (P) for a given
time (T) and rate (R) can be calculated as:

(a) SI = (p * r * t) / 100 (b) CI = P * ((1 + (R / 100)) T - 1)

Write a program to input sum, rate, time and type of Interest ('S' for Simple Interest and 'C'
for Compound Interest). Calculate and display the sum and the interest earned.

Program :
import java.util.*;
class prg19{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter 'S' to calculate Simple Interest.");
System.out.println("Enter 'C' to calculate Compound Interest.");
char ch = sc.next().charAt(0);
System.out.println("Enter the sum.");
double p = sc.nextDouble();
System.out.println("Enter the time.");
double n = sc.nextDouble();
System.out.println("Enter the rate of interest.");
double r = sc.nextDouble();
switch(ch){
case 'S':
double si = (p * r * n)/100.0;
double am = p +si;
System.out.println("Simple Interest : ");
System.out.println("Interset = \u20B9"+si+"\nAmount = \u20B9"+am);
break;

case 'C' :
double amount = p * (Math.pow((1+r/100.0),n));
double ci = amount - p;
System.out.println("Compound Interest : ");
System.out.println("Interset = \u20B9"+ci+"\nAmount = \u20B9"+amount);
break;
default :
System.out.println("Type 'S' or 'C'.\nTry again.");
}
}
}
Output :
Variable Description:
Variable Type Purpose
ch Char To store the choice of the user.
p Double To input the principal amount.
n Double To input the time period.
r Double To store the rate of interest.
si Double To store interest for simple interest.
am Double To store amount of maturity.
amount Double To store amount of maturity
ci Double To store interst for compound interest.
Program 28:
Question : Kumar Electronics' has announced the following seasonal discounts on
purchase of certain items.

Purchase Amount Discount on Laptop Discount on Desktop PC

Up to ₹ 25000 0.0% 5.0%

₹ 25,001 to ₹ 50,000 5% 7.5%

₹ 50,001 to ₹ 1,00,000 7.5% 10.0%

More than ₹ 1,00,000 10.0% 15.0%

Write a program to input name, amount of purchase and the type of purchase (`L' for
Laptop and 'D' for Desktop) by a customer. Compute and print the net amount to be paid by
a customer along with his name.

Program :
import java.util.*;
class prg20{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter your name.");
String name = sc.nextLine();
System.out.println("Enter amount of purchase.");
double cp = sc.nextInt();
System.out.println("Enter 'D' for desktop");
System.out.println("Enter 'L' for Laptop");
char ch = sc.next().charAt(0);
double dis = 0,disPrice = 0;
switch(ch){
case 'L' :
if(cp <=25000){
dis = 0;
}else if(cp >25000 && cp <=50000){
dis = cp * 5/100.0;
}else if(cp >50000 && cp <=100000){
dis = cp * 7.5/100.0;
}else if(cp > 100000){
dis = cp * 10/100.0;
}
disPrice = cp - dis;
System.out.println("Customer name : "+name);
System.out.println("Item purchased : Laptop");
System.out.println("Amount of purchase : \u20B9"+cp);
System.out.println("Discount : \u20B9"+dis);
System.out.println("Net Price : \u20B9"+disPrice);
break;
case 'D' :
if(cp <=25000){
dis = cp * 5/100.0;
}else if(cp >25000 && cp <=50000){
dis = cp * 7.5/100.0;
}else if(cp >50000 && cp <=100000){
dis = cp * 10/100.0;
}else if(cp > 100000){
dis = cp * 15/100.0;
}
disPrice = cp - dis;
System.out.println("Customer name : "+name);
System.out.println("Item purchased : Desktop");
System.out.println("Amount of purchase : \u20B9"+cp);
System.out.println("Discount : \u20B9"+dis);
System.out.println("Net Price : \u20B9"+disPrice);
break;
default :
System.out.println("Invaild Input.\nTry again.");
}
}
}
Output :

Variable Description:
Variable Type Purpose
name String To store the name of the user.
cp Double To store the cost price.
ch Double To check for laptop or desktop .
dis Double To store discount amount.
disPrice Double To store price after discount.
Program 29:
Question : Write a program in Java to find and display the diagonal of a square by taking
side as input.

Program :
import java.util.*;

class prg1{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the side of a square");
int side = sc.nextInt();
double diagonal = Math.sqrt(2)*side;

System.out.println("Side of the square = "+side);


System.out.println("Diagonal of the square = "+diagonal);
}
}
Output :

Variable Description:
Variable Type Purpose
side Int To input the side of the square.
diagonal Double To find the diagonal of the square.
Program 30:
Question : Write a program in Java to calculate and display the hypotenuse of a Right-
Angled Triangle by taking perpendicular and base as inputs.

Program :
import java.util.*;
class prg2{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the base of the triangle.");
double b = sc.nextDouble();
System.out.println("Enter the perpendicular of the triangle.");
double p = sc.nextDouble();
double h = Math.sqrt(Math.pow(b,2)+Math.pow(p,2));
System.out.println("Hypotenuse of the triangle is "+h);
}
}
Output :

Variable Description :
Variable Type Purpose
b Double To input the base of the triangle.
p Double To input the perpendicular of the triangle.
h Double To find the hypotenuse of the triangle.
Program 31:
Question : Write a program to input a number and evaluate the results based on the number
entered by the user:
(a) Natural logarithm of the number
(b) Absolute value of the number
(c) Square root of the number
(d) Cube of the number
(e) Random numbers between 0 (zero) and 1 (one).

Program :
import java.util.*;
class prg3{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
int num = sc.nextInt();
double log = Math.log(num), abs = Math.abs(num);
double sqrt = Math.sqrt(num), cube = Math.pow(num,3);
double random = Math.random();
System.out.println("Natural logarithm of the number : "+log);
System.out.println("Absolute value of the number : "+abs);
System.out.println("Square root of the number : "+sqrt);
System.out.println("Cube of the number : "+cube);
System.out.println("Random number between 0 and 1 : "+random);
}
}
Output :
Variable Description:
Variable Type Purpose
num Int To input a number.
log Double To store the natural log of num.
abs Double To store the absolute value of num
random Double To pick a random number between 0 and 1
cube Double To store cube of num
sqrt Double To store square root of num.
Program 32:
Question : In an an examination, you have appeared for three subjects i.e. Physics,
Chemistry and Biology. Write a program in Java to calculate the average mark obtained
and finally display the marks in rounded-off form.
Take Physics, Chemistry and Biology marks as inputs.

Program :
import java.util.*;
class prg4{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter your marks in Physics, Chemistry and Biology");
int phy = sc.nextInt();
int chem = sc.nextInt();
int bio = sc.nextInt();
double total = bio+phy+chem;
double average = total/3.0;
double round = Math.round(average);
System.out.println("Average marks : "+average);
System.out.println("Rounded average : "+round);
}
}
Output :
Variable Description :
Variable Type Purpose
phy Int To input marks in physics.
chem Int To input marks in chemistry.
bio Int To input marks in biology.
total Double To store total marks.
average Double To store the average marks.
round Double To store rounded average.
Program 33:
Question : Write a program in Java to calculate and display the radius of a circle
by taking area as an input.
Program :
import java.util.*;
class prg5{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the area of a circle");
double area = sc.nextDouble();
double radius = Math.sqrt(7 * area/22.0);
System.out.println("Radius is "+radius);
}
}
Output :

Variable Type Purpose


area Double To input the area of circle.
radius Double To find the radius of circle.
Program 34:
Question : Write a program in Java to generate a pattern of a token in the form of a triangle
or in the form of an inverted triangle depending on the user‟s choice.

Program :
import java.util.*;
class prg3{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a character.");
char ch = sc.next().charAt(0);
System.out.println("Enter 1 to print a triangle pattern.");
System.out.println("Enter 2 to print a inverted triangle pattern.");
int num = sc.nextInt();
switch(num){
case 1 :
System.out.println("Triangle Pattern : ");
for(int i = 1;i<=5;i++){
for(int j = 1;j<=i;j++){
System.out.print(ch+" ");
}
System.out.println();
}
break;
case 2 :
System.out.println("Inverted Triangle Pattern : ");
for(int i = 5;i>0;i--){
for(int j = 1;j<=i;j++){
System.out.print(ch+" ");
}
System.out.println();
}
break;
default :
System.out.println("Invalid Input!!!\nTry again.");
}
}
}
Output :

Variable description:
Variable Type Purpose
ch Char To input a character.
num Int To input the user‟s choice.
i Int Used in for loop
j Int Used in for loop
i Int Used in for loop
j Int Used in for loop
Program 35:
Question : Write a Java program to input any 10 numbers and display the sum of only the
negative numbers.

Program :
import java.util.*;
class prg4{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int a = 1,sum = 0;
System.out.println("Enter 10 numbers.");
while(a<=10){
int n = sc.nextInt();
if(n < 0 ){
sum = sum +n;
}
a++;
}
System.out.println("Sum of negative numbers = "+sum);
}
}
Output :
Variable Description :

Variable Type Purpose


a Int Used in while loop.
n Int To input a number.
sum Int To store the sum of numbers.
Program 36:
Question : Write a program in Java to display the sum of any two numbers for ten iterations.
If the sum of two numbers is negative then the program terminates.

Program :
import java.util.*;
class prg7{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int a,b,c;
for(int i = 1;i<=10;i++){
System.out.println("Enter two numbers.");
a = sc.nextInt();
b = sc.nextInt();
c = a+b;
if(c < 0){
System.out.println("Program terminates.");
break;
}else {
System.out.println("Sum of the numbers is "+c);
}
}
}
Output :
Variable Description:
Variable Type Purpose
a Int To input a number.
b Int To input a number.
c Int To store sum of a and b.
i Int Used in for loop.
Program 37:
Question : Write a program in Java to input three numbers and a character. If the character is
‟s‟ then display the sum of the numbers. If the character is „p‟ then display the product of the
numbers.

Program :
import java.util.*;
class prg3c{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter three numbers");
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
System.out.println("Enter a character.(s or p)");
char ch = sc.next().charAt(0);
int display;
switch(ch){
case 's' :
display = a + b + c;
System.out.println("The sum of the numbers is "+display);
break;
case 'p' :
display = a * b * c;
System.out.println("The product of the numbers is "+display);
break;
default :
System.out.println("Invalid character input. Try again.");
}
}
}
Output :

Variable Description :
Variable Type Purpose
a Int To input a number.
b Int To input a number.
c Int To input a number.
ch char To input a user‟s choice.
display Int To display the result.
Program 38:
Question : Write a program to input two unequal numbers. If the first number is greater,
then display the square of second number and cube of first number. If the second number is
greater, then display the square of first number and cube of second number.

Program :
import java.util.*;
class prg1{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter two unequal numbers : ");
int a = sc.nextInt();
int b = sc.nextInt();
if(a > b){
System.out.println("Square of "+b+" is "+Math.pow(b,2));
System.out.println("Cube of "+a+" is "+Math.pow(a,3));
}else if(a < b){
System.out.println("Square of "+a+" is "+Math.pow(a,2));
System.out.println("Cube of "+b+" is "+Math.pow(b,3));
}else if(a == b){
System.out.println("Both the numbers are equal.");
}
}
}
Output :
Variable Description:
Variable Type Purpose
a Int To input a number.
b Int To input a number.
Program 39:
Question : Write a program to display the Mathematical Table from 5 to 10 for 10
iterations in the given format:
Program :
class prg1{
public static void main(String args[]){
for(int i = 5;i<=10;i++){
System.out.println("Table of "+i);
for(int j=1;j<=10;j++){
System.out.println(i+" * "+j+" = "+i*j);
}
}
}
}
Output :

Variable description:
Variable Type Purpose
i Int Used in for loop.
Program 40:
Question : Write a program to accept any 20 numbers and display only those
numbers which are prime.
Program :
import java.util.*;
class prg2{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter 20 numbers");
for(int i=1;i<=20;i++){
int n = sc.nextInt();
boolean isPrime = true;
for(int j=2;j<=n/2;j++){
if(n % j == 0){
isPrime =false;
break;
}
}
if(isPrime == true && n != 1){
System.out.println(n+" is a prime number.");
}
}
}
}
Output :

Variable Description :
Variable Type Purpose
i Int Used in for loop
n Int To input the number
isPrime Boolean To check whether n is prime or not.
j Int Used in for loop.
Program 41:
Question : Write a program to calculate and display the factorials of all the
numbers between 'm' and 'n' (where m<n, m>0, n>0).
Program :
import java.util.*;
class prg5{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter value of 'm' and 'n'.(where m<n, m>0, n>0).");
int m = sc.nextInt();
int n = sc.nextInt();
for(int i = m+1;i<n;i++){
int fac = 1;
for(int j = i;j>0;j--){
fac = fac * j;
}
System.out.println("Factorial of "+i+" = "+fac);
}
}
}
Output :

Variable Description :
Variable Type Purpose
n Int To input a number
n Int To input a number
fac Int To store the factorial of number.
Program 42:
Question : Write a menu driven program to display all prime and non-prime numbers from
1 to 100.
Enter 1: to display all prime numbers
Enter 2: to display all non-prime numbers

Program :
class prg6{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter 1 to display all prime numbers between 1 and 100");
System.out.println("Enter 2 to display all non-prime numbers between 1 and
100");
int num = sc.nextInt();
switch(num){
case 1 :
System.out.println("Prime numbers between 1 and 100 : ");
for(int i=2;i<=100;i++){
boolean isPrime = true;
for(int j = 2;j <= i/2;j++){
if(i % j == 0){
isPrime = false;
break;
}
}
if (isPrime)
System.out.println(i);
}
break;
case 2 :
System.out.println("Non-Prime numbers between 1 and 100 : ");
for(int i=2;i<=100;i++){
boolean isPrime = true;
for(int j = 2;j <= i/2;j++){
if(i % j == 0){
isPrime = false;
break;
}
}
if (!isPrime)
System.out.println(i);
}
break;
default :
System.out.println("Invalid Input!!!\nTry again.");
}
}
}
Output :

Variable Description :
Variable Type Purpose
num Int To input choice of user.
isPrime Boolean To check whether number is Prime number or not.
i Int Used in for loop.
j Int Used in for loop.
Program 43:
Question : Write a program to input a number and display the factorial of every digit.

Program :
import java.util.*;
class prg10{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number.");
int num = sc.nextInt();
int d = num;
while(d > 0){
int m = d%10;
int fac = 1;
for(int i = m;i>0;i--){
fac = fac *i;
}
System.out.println("Factorial of "+m+" = "+fac);
d = d/10;
}
}
}
Output :
Variable Type Purpose
num Int To input a number
d Int To duplicate num.
m Int To store digit of number.
fac Int To store the factorial of number.
i Int Used in for loop.
Program 44:
Question : Write a program to input a three digit number and display its digits raised to the
power of its position.

Program :
import java.util.*;
class prg11{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a three digit number.");
int num = sc.nextInt();
int d = num;
int n = 0,m = 0,o = 0;
while(d > 0){
n = d % 10;
d = d/10;
m = d % 10;
d = d/10;
o = d % 10;
d = 0;
}
System.out.println(o+"^1 = "+Math.pow(o,1));
System.out.println(m+"^2 = "+Math.pow(m,2));
System.out.println(n+"^3 = "+Math.pow(n,3));
}
}
Output :
Variable Description :
Variable Type Purpose
num Int To input a number
d Int To duplicate the number.
o Int To store one digit of number
m Int To store one digit of number
n Int To store one digit of number
Program 45:
Question : Write a program to input a number and check and display whether it is a
composite number or not.

Program :
import java.util.*;
class prg12{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a nummber.");
int num = sc.nextInt();
int fac = 0;
for(int i = 2;i<num;i++){
if(num % i == 0 && num % Math.pow(i,2) == 0){
fac = fac+2;
}else if(num % i ==0){
fac = fac +1;
}
}
if(fac >= 2){
System.out.println(num+" is a composite number.");
}else {
System.out.println(num+" is not a composite number.");
}
}
}
Output :
Variable Description :
Variable Type Purpose
num Int To input a number.
fac Int To store no. of factors.
Program 46:
Question : Write a program to calculate the sum of all odd numbers and even numbers
between a range of numbers from m to n (both inclusive) where m < n. Input m and n
(where m<n).

Program :
import java.util.*;
class prg3{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter two numbers m and n.(m < n)");
int m = sc.nextInt();
int n = sc.nextInt();
int sumOdd = 0, sumEven = 0;
if(m >= n ){
System.out.println("m should be lesser than n. Try again.");
}else{
for(int i = m; i<=n; i++){
if(i % 2 == 0){
sumEven += i;
}else{
sumOdd += i;
}
}
System.out.println("Sum of odd numbers = "+sumOdd);
System.out.println("Sum of even numbers = "+sumEven);
}
}
}
Output :

Variable Description:
Variable Type Purpose
m Int To input a number.
n Int To input a number.
sumOdd Int To store sum of odd numbers
sumEven Int To store sum of even numbers.
Program 47:
Question : Write a program to display all the numbers between m and n input from the
keyboard (where m<n, m>0, n>0), check and print the numbers that are perfect square.

Program :
import java.util.*;
class prg5{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter two numbers m and n.(m < n)(m>0)(n>0)");
int m = sc.nextInt();
int n = sc.nextInt();
if(m<n && m>0 && n>0){
for(int i = m+1; i<n;i++){
System.out.println("Number : "+i);
double root = Math.sqrt(i);
if(root == Math.floor(root) ){
System.out.println(i+" is a Perfect Square");
}
}
}else{
System.out.println("Invalid input \nTry again");
}
}
}
Output :
Variable Description:
Variable Type Purpose
m Int To input a number
n Int To input a number
root Double To store square root of i
i Int Used in for loop
Program 48:
Question : Write a program to display all the 'Buzz Numbers' between p and q (p<q).

Program :
import java.util.*;
class prg6{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter two numbers p and q.(p < q)");
int p = sc.nextInt();
int q = sc.nextInt();
if( p < q){
System.out.print("Buzz numbers between "+p+" and "+q+" are"+" : ");
for(int i = p+1; i < q;i++){
if(i % 7 == 0 || i % 10 == 7){
System.out.print(i+ ", ");
}
}
}else{
System.out.println("Invalid input. \n Try again.");
}
}
}
Output :
Variable Description:
Variable Type Purpose
p Int To input a number.
q Int To input a number.
i Int Used in for loop.
Program 49:
Question : Write a program to input a number and count the number of digits. The program further
checks whether the number contains odd number of digits or even number of digits.

Program :
import java.util.*;
class prg9{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
int num = sc.nextInt();
int count = 0;
String ore;
while(num >0){
count ++;
num /=10;
}
if(count % 2 == 0){
ore = "Number contains even number of digits";
}else{
ore = "Number contains odd number of digits.";
}
System.out.println("Number of digits = "+count);
System.out.println(ore);
}
}
Output :
Variable Description:

Variable Type Purpose


num Int To input a number
count Int To count the number of digits in num
ore String To print whether num has odd or even digits
Program 50:
Question : Write a program to input a number and display the new number after reversing
the digits of the original number. The program also displays the absolute difference
between the original number and the reversed number.

Program :
import java.util.*;
class prg10{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
int num = sc.nextInt();
int rev = 0,dup = num;
while(dup > 0){
int d = dup % 10;
rev = rev*10 + d;
dup = dup/10;
}
int dif = Math.abs(num - rev);
System.out.println("Reversed number : "+rev);
System.out.println("Absolute differnce is "+dif);
}
}
Output :
Variable Description:
Variable Type Purpose
num Int To input a number.
rev Int To store the reversed number of num
d Int To store the extract of num
dup Int To create a duplicate of num
dif Int To store the absolute difference between num and rev.
Program 51:
Question: Write a program to accept a word and display the vowels and the frequency
of the vowels.

Program :
import java.util.*;
class prg26{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a word.");
String s = sc.next();
int l = s.length();
int c = 0;
for(int i = 0; i <l; i++){
char ch = s.charAt(i);
if("AEIOUaeiou".indexOf(ch) != -1){
System.out.print(ch);
c = c+1;
}
}
System.out.println();
System.out.println("The frequency of vowels = "+ c);
}
}
Output :
Variable Description:
Variable Type Purpose
l Int To store the length of s
c Int To store frequency of vowels
ch char To extract character from s.
s String To input a string.
i Int Used in for loop.
Program 52:
Question: WAP to accept a word, convert it into uppercase, arrange the characters in
alphabetical order.

Program :
import java.util.*;
class prg27{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a word.");
String s = sc.next();
s = s.toUpperCase();
int l = s.length();
char ch;
System.out.println("Word in alphabetical order.");
for(int i = 65; i <= 90; i++){
for(int j = 0; j < l; j++){
ch = s.charAt(j);
if(ch == (char)i){
System.out.print(ch);
}
}
}
}
}
Output :
Variable Description:
Variable Type Purpose
l Int To store the length of the string
j Int Used in inner for loop
ch char To extract character from s.
s string To input a String.
i Int Used in outer for loop.
Program 53:
Question: WAP to accept a word, count and display the uppercase letters, lowercase,
blank space and words.

Program :
import java.util.*;
class prg28{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a sentence");
String s = sc.nextLine();
int u = 0,l = 0,b = 0,w = 1,o = 0;
int len = s.length();
for(int i = 0; i < len; i++){
char ch = s.charAt(i);
if(Character.isUpperCase(ch)){
u = u+1;
}else if(Character.isLowerCase(ch)){
l = l+1;
}else if(Character.isWhitespace(ch)){
b = b+1;
w = w+1;
}else{
o = o+1;
}
}

System.out.println("Number of Uppercase characters : "+u);


System.out.println("Number of Lowercase characters : "+l);
System.out.println("Number of Blankspace characters : "+b);
System.out.println("Number of words in sentence : "+w);
System.out.println("Number of other characters : "+o);
}
}
Output :

Variable Description:
Variable Type Purpose
u Int To store the number of upper case characters.
len Int To store length of s.
ch char To extract character from s.
s string To input a string.
i int Used in for loop.
l Int To store the number of lower case characters.
b Int To store the number of blank spaces..
w Int To store the number of words.
o Int To store the number of other characters.
Program 54:
Question: WAP to accept a word, check whether it is a palindrome otherwise convert it
into palindrome.

Program :
import java.util.*;
class prg29{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a word.");
String s = sc.next();
char ch ;
String s1 = "";
int l =s.length();
for(int i =0; i < l; i++){
ch = s.charAt(i);
s1 = ch + s1;
}
if(s.equals(s1)){
System.out.println("The word is a palindrome.");
}else{
System.out.println("The word is not a palindrome.");
String s2 = s + s1;
System.out.println("Word changed to palindrome is : "+ s2);
}
}
}
Output :
Variable Description:
Variable Type Purpose
s1 String To store the reversed form of s.
l Int To store the length of s.
ch Char To extract characters from s.
s String To input a string
i Int Used in for loop.
S2 String To create a new word by joining s and s1.
Program 55:
Question: WAP to input a sentence, print total no. of capital vowels and total no. of
small vowels from the string.

Program :
import java.util.*;
class prg30{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a sentence.");
String s = sc.nextLine();
int c = 0, sm = 0;
for(int i = 0; i < s.length(); i++){
char ch = s.charAt(i);
if("AEIOU".indexOf(ch) != -1){
c++;
}else if("aeiou".indexOf(ch) != -1){
sm++;
}
}
System.out.println("Number of capital vowels in the sentence = "+c);
System.out.println("Number of small vowels in the sentence = "+sm);
}
}
Output :
Variable Description:
Variable Type Purpose
c Int To store no. of capital alphabets
ch Char To extract characters from s.
s String To input a string.
i Int Used in for loop.
sm I To store no. of small alphabets.
Program 56:
Question: WAP to accept a word and convert it into lowercase and display the new
word by replacing only the vowels with the character following it.

Program :
import java.util.*;
class prg31{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a word.");
String s = sc.next();
String s1 = s.toLowerCase();
for(int i = 0; i < s1.length(); i++){
char ch = s1.charAt(i);
int a = ch +1;
char ch2 = (char)a;
if("aeiou".indexOf(ch) != -1){
s1 = s1.replace(ch,ch2);
}
}
System.out.println("New word : \n"+s1);
}
}
Output :
Variable Description:
Variable Type Purpose
a Int To conver char to int.
ch Char To extract character from s.
s String To input a string
i Int Used in for loop.
s1 String To convert s to lowercase.
ch2 Char To get the next character of ch in ASCII table.
Program 57:
Question: WAP to accept a word and print the characters of the word along with its
ascii code.

Program :
import java.util.*;
class prg32{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a word.");
String s1 = sc.nextLine();
for(int i = 0; i < s1.length(); i++){
char ch = s1.charAt(i);
int ascii = ch;
System.out.println(ch + " - "+ ascii);
}
}
}
Output :
Variable Description:
Variable Type Purpose
ascii Int To get ascii value of ch
ch Char To extract character from string.
i Int Used in for loop.
s1 String To input a string.
Program 58:
Question: WAP to accept a word, convert it into lowercase arrange all the alphabets
present in the string in reverse alphabetical order.

Program :
import java.util.*;
class prg33{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a word");
String s = sc.nextLine();
s = s.toLowerCase();
String s1= "";
for(int i = 97; i <=122; i++){
for(int j = 0; j < s.length(); j++){
char ch = s.charAt(j);
if(ch == (char)i){
s1 = ch + s1;;
}
}
}
System.out.println("Word in reversed order : ");
System.out.println(s1);
}
}
Output :
Variable Description:
Variable Type Purpose
j Int Used in outer for loop.
ch Char To extract character from
i Int Used in outer for loop.
s1 String To store the string in reversed alphabetical order.
s String To input a string.
Program 59:
Question: WAP to accept a word, display the Piglatin form of the word
Program :
import java.util.*;
class prg34{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a word.");
String s = sc.nextLine();
s = s.toUpperCase();
String s1 = "";
String s2 = "";
int t = 0;
for(int i = 0; i < s.length();i++){
if("AEIOU".indexOf(s.charAt(i)) != -1){
t = i;
break;
}
}
s1 = s1 + s.substring(t);
s2 = s2 + s.substring(0,t) + "AY";
String s3 = s1+s2;
System.out.println("Piglatin : "+s3);
}
}
Output :
Variable Description:
Variable Type Purpose
t Int To get the index of first vowel.
s2 String Store the second part of the piglatin word.
i Int Used in for loop.
s1 String Store the first part of piglatin word.
s String Used to input a string.
s3 String Store the piglatin word.
Program 60:
Question: WAP to input a sentence, convert it into Uppercase shift each letter one step
towards right, print original and encoded sentence.

Program :
import java.util.*;
class prg35{
public static void main(String agrs[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a sentence.");
String s = sc.nextLine();
s = s.toUpperCase();
String s1 = "";
for(int i = 0; i < s.length(); i++){
char ch = s.charAt(i);
int a = (int)ch + 1;
s1 = s1 +(char)a;
}
System.out.println(s);
System.out.println("Encoded sentence : ");
System.out.println(s1);
}
}
Output :
Variable Description:
Variable Type Purpose
a Int To convert char to int.
i Int Used in for loop.
s1 String To store the encoded sentence.
s String To input a String.
ch Char To extract character from String.
Program 61:
Question: Write a program to input a sentence. Find and display the following:
(i) Number of words present in the sentence
(ii) Number of letters present in the sentence
Assume that the sentence has neither include any digit nor a special character.
Program :
import java.util.*;
class prg36{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a sentence:");
String s = sc.nextLine();
int wCount = 0, lCount = 0;
int l = s.length();
for (int i = 0; i < l; i++) {
char ch = s.charAt(i);
if (ch == ' ')
wCount++;
else
lCount++;
}
wCount++;
System.out.println("No. of words = " + wCount);
System.out.println("No. of letters = " + lCount);
}
}
Output :
Variable Description:
Variable Type Purpose
l Int To get the length of the String.
i Int Used in for loop.
wcount Int To count the no. of words.
s String To input a string.
ch Char To extract character from string.
lcount Int To count the number of letters.
Program 62:
Question: Write a program in Java to accept a word/a String and display the new
string after removing all the vowels present in it.
Program :
import java.util.*;
class prg37{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a word or sentence:");
String s = sc.nextLine();
int l = s.length();
s = s.toUpperCase();
String s1 = "";
for (int i = 0; i < l; i++) {
char ch = s.charAt(i);
if ("AEIOU".indexOf(ch) != - 1 ){
continue;
}
s1 = s1 + ch;
}
System.out.println("String with vowels removed");
System.out.println(s1);
}
}
Output :
Variable Description:
Variable Type Purpose
l Int To store the length of the string.
i Int Used in for loop.
s1 String To store the string without vowels.
s String To input a string.
ch Char To extract characters from string.
Program 63:
Question: Write a program in Java to accept a name(Containing three words)
and Display only the initials
Program :
import java.util.*;
class prg38{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a name of 3 words:");
String s = sc.nextLine();
int len = s.length();
System.out.print(s.charAt(0) + " ");
for (int i = 1; i < len; i++) {
char ch = s.charAt(i);
if (ch == ' ') {
char ch2 = s.charAt(i + 1);
System.out.print(ch2 + " ");
}
}
}
}
Output :
Variable Description:
Variable Type Purpose
len Int To get the length of the string.
I Int Used in for loop.
S1 String To store the string with only initials.
S String To input the name.
ch Char To extract the characters from string.
ch2 Char To get the next character of ch.
Program 64:
Question: Write a program in Java to accept a name containing three words and
display the surname first, followed by the first and middle names.
Program :
import java.util.*;
class prg39{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a name of 3 words:");
String name = sc.nextLine();
int ls = name.lastIndexOf(' ');
String s= name.substring(ls + 1);
String i = name.substring(0, ls);
System.out.println(s+ " " + i);
}
}
Output :

Variable Description:
Variable Type Purpose
ls Int To get the last index of blank space.
i String To get the last name.
name String To input the name.
s String To get the first two names.
Program 65:
Question: Write a program in Java to enter a String/Sentence and display the
longest word and the length of the longest word present in the String.
Program :
import java.util.*;
class prg40{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a word or sentence:");
String str = in.nextLine();
str += " ";
String word = "", lWord = "";
int len = str.length();
for (int i = 0; i < len; i++) {
char ch = str.charAt(i);
if (ch == ' ') {
if (word.length() > lWord.length())
lWord = word;
word = "";
}
else {
word += ch;
}
}
System.out.println("The longest word: " + lWord +
": The length of the word: " + lWord.length());
}
}
Output :

Variable Description:
Variable Type Purpose
len Int To get the length of str.
i Int Used in for loop.
lword String To get the longest word.
str String To input a String.
ch Char To extract characters from str
word String To extract the words from the string.
Program 66:
Question: Write a program in Java to store 20 numbers (even and odd numbers)
in a Single Dimensional Array (SDA). Calculate and display the sum of all even
numbers and all odd numbers separately.
Program :
import java.util.*;
public class prg56
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int arr[] = new int[20];
System.out.println("Enter 20 numbers");
for (int i = 0; i < arr.length; i++) {
arr[i] = in.nextInt();
}
int oddSum = 0, evenSum = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] % 2 == 0)
evenSum += arr[i];
else
oddSum += arr[i];
}
System.out.println("Sum of Odd numbers = " + oddSum);
System.out.println("Sum of Even numbers = " + evenSum);
}
}
Output :

Variable Description:
Variable Type Purpose
arr Integer array An array to store 20 integers.
i Int Used in for loop.
oddSum Int To store sum of odd numbers
evenSum Int To store sum of even numbers.
Program 67:
Question: Write a program in Java to store 20 temperatures in °F in a Single
Dimensional Array (SDA) and display all the temperatures after converting them
into °C.
Program :
import java.util.Scanner;
public class prg57
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
double arr[] = new double[20];
System.out.println("Enter 20 temperatures in degree Fahrenheit");
for (int i = 0; i < arr.length; i++) {
arr[i] = in.nextDouble();
}
System.out.println("Temperatures in degree Celsius");
for (int i = 0; i < arr.length; i++) {
double tc = 5 * ((arr[i] - 32) / 9);
System.out.println(tc);
}
}
}
Output :

Variable Description:
Variable Type Purpose
arr Double array An array to store 20 double values.
i Int Used in for loop.
tc double To find the temperature in Celsius.
Program 68:
Question: Write a program in Java to store 10 numbers (including positive and
negative numbers) in a Single Dimensional Array (SDA). Display all the negative
numbers followed by the positive numbers without changing the order of the
numbers
Program :
import java.util.Scanner;
public class prg58
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int arr[] = new int[10];
System.out.println("Enter 10 numbers");
for (int i = 0; i < arr.length; i++) {
arr[i] = in.nextInt();
}
for (int i = 0; i < arr.length; i++) {
if (arr[i] < 0)
System.out.print(arr[i] + ", ");
}
for (int i = 0; i < arr.length; i++) {
if (arr[i] >= 0)
System.out.print(arr[i] + ", ");
}
}
}
Output :

Variable Description:
Variable Type Purpose
arr Integer array An array to store 10 integer.
i Int Used in for loop.
Program 69:
Question: Write a program in Java to store 20 numbers in a Single Dimensional
Array (SDA). Display the numbers which are prime.
Program :
import java.util.Scanner;
public class prg59
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int arr[] = new int[20];
System.out.println("Enter 20 numbers");
for (int i = 0; i < arr.length; i++) {
arr[i] = in.nextInt();
}
System.out.println("Prime Numbers:");
for (int i = 0; i < arr.length; i++) {

int c = 0;
for (int j = 0; j <= arr[i]; j++) {
if (arr[i] % j == 0) {
c++;
}
}
if (c == 2)
System.out.print(arr[i] + ", ");
}
}
}
Output :

Variable Description:
Variable Type Purpose
arr Integer array An array to store 20 integer.
i Int Used in for loop.
c Int To count the number of factors.
Program 70:
Question: Write a program to accept name and total marks of N number of
students in two single subscript arrays name[ ] and totalmarks[ ].
Calculate and print:
(i) The average of the total marks obtained by N number of students.
(ii) Deviation of each student's total marks with the average.
Program :
import java.util.Scanner;
public class prg60
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter number of students: ");
int n = in.nextInt();
String name[] = new String[n];
int totalmarks[] = new int[n];
int grandTotal = 0;
for (int i = 0; i < n; i++) {
in.nextLine();
System.out.print("Enter name of student " + (i+1) + ": ");
name[i] = in.nextLine();
System.out.print("Enter total marks of student " + (i+1) + ": ");
totalmarks[i] = in.nextInt();
grandTotal += totalmarks[i];
}
double avg = grandTotal / (double)n;
System.out.println("Average = " + avg);
for (int i = 0; i < n; i++) {
System.out.println("Deviation for " + name[i] + " = "
+ (totalmarks[i] - avg));
}
}
}

Output :

Variable Description:
Variable Type Purpose
n Int To input the number of students.
name String array To store names of n number of students.
totalmarks Integer array To store the marks of sudents.
grandTotal Int To store the total marks of all the students.
i Int Used in for loop.
avg double To find the combined average of all the students.
Program 71:
Question: The marks obtained by 50 students in a subject are tabulated.

Write a program to input the names and marks of the students in the subject.
Calculate and display:
(a) The subject average marks.
(b) The highest marks in the subject and the name of the student.
Program :
import java.util.Scanner;
public class prg61
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
String name[] = new String[50];
int marks[] = new int[50];
int total = 0;
for (int i = 0; i < name.length; i++) {
System.out.print("Enter name of student " + (i+1) + ": ");
name[i] = in.nextLine();
System.out.print("Enter marks of student " + (i+1) + ": ");
marks[i] = in.nextInt();
total += marks[i];
in.nextLine();
}
double avg = (double)total / 50.0;
System.out.println("Subject Average Marks = " + avg);
int hIdx = 0;
for (int i = 1; i < marks.length; i++) {
if (marks[i] > marks[hIdx])
hIdx = i;
}
System.out.println("Highest Marks = " + marks[hIdx]);
System.out.println("Name = " + name[hIdx]);
}
}

Output :

Variable Description:
Variable Type Purpose
marks Integer array To store the marks of 50 students..
name String array To store names of 50 students.
total Int To store the total marks 50 of sudents.
hIdx Int To find the index of highest mark.
i Int Used in for loop.
avg double To find the combined average of all the students.
Program 72:
Question: Write a program to accept a list of 20 integers. Sort the first 10
numbers in ascending order and next the 10 numbers in descending order by
using 'Bubble Sort' technique. Finally, print the complete list of integers.
Program :
import java.util.*;
public class prg62
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int arr[] = new int[20];
System.out.println("Enter 20 numbers:");
for (int i = 0; i < arr.length; i++) {
arr[i] = in.nextInt();
}
for (int i = 0; i < arr.length / 2 - 1; i++) {
for (int j = 0; j < arr.length / 2 - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int t = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = t;
}
}
}
for (int i = 0; i < arr.length / 2 - 1; i++) {
for (int j = arr.length / 2; j < arr.length - i - 1; j++) {
if (arr[j] < arr[j + 1]) {
int t = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = t;
}
}
}
System.out.println("\nSorted Array:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}

Output :
Variable Description:
Variable Type Purpose
arr Integer array To input 20 numbers.
i Int Used in for loop.
j Int Used in inner loop.
t Int To get the next number in the array.
Program 73:
Question: Write a program in Java to accept 20 numbers in a single dimensional
array arr[20]. Transfer and store all the even numbers in an array even[ ] and all
the odd numbers in another array odd[ ]. Finally, print the elements of both the
arrays.
Program :
import java.util.Scanner;
public class prg63
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int i = 0;
int arr[] = new int[20];
int even[] = new int[20];
int odd[] = new int[20];
System.out.println("Enter 20 numbers:");
for (i = 0; i < 20; i++) {
arr[i] = in.nextInt();
}
int eIdx = 0, oIdx = 0;
for (i = 0; i < 20; i++) {
if (arr[i] % 2 == 0)
even[eIdx++] = arr[i];
else
odd[oIdx++] = arr[i];
}
System.out.println("Even Numbers:");
for (i = 0; i < eIdx; i++) {
System.out.print(even[i] + " ");
}
System.out.println("\nOdd Numbers:");
for (i = 0; i < oIdx; i++) {
System.out.print(odd[i] + " ");
}
}
}
Output :

Variable Description:
Variable Type Purpose
arr Integer array To input 20 numbers.
i Int Used in for loop.
even Integer array To store the even numbers.
odd Integer array To store the odd numbers.
eIdx Int To increase the index of even array
oIdx Int To increase the index of odd array.
Program 74:
Question: Write a program to store 20 numbers in a Single Dimensional Array
(SDA). Now, display only those numbers that are perfect squares.
Program :

import java.util.Scanner;

public class prg64

public static void main(String args[]) {

Scanner in = new Scanner(System.in);

int arr[] = new int[20];

System.out.println("Enter 20 numbers");

for (int i = 0; i < arr.length; i++) {

arr[i] = in.nextInt();

System.out.println("Perfect Squares are:");

for (int i = 0; i < arr.length; i++) {

double sr = Math.sqrt(arr[i]);

if ((sr - Math.floor(sr)) == 0)

System.out.print(arr[i] + ", ");

}
Output :

Variable Description:
Variable Type Purpose
arr Integer array To input 20 numbers.
i Int Used in for loop.
sr double To get the square root of the number.
Program 75:
Question: Write a program to accept 10 different decimal numbers (double data
type) in a Single Dimensional Array (say, A). Truncate the fractional part of each
number of the array A and store their integer part in another array (say, B).
Program :
import java.util.Scanner;
public class prg65
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
double a[] = new double[10];
int b[] = new int[10];
System.out.println("Enter 10 decimal numbers");
for (int i = 0; i < a.length; i++) {
a[i] = in.nextDouble();
b[i] = (int)a[i];
}
System.out.println("Truncated numbers");
for (int i = 0; i < b.length; i++) {
System.out.print(b[i] + ", ");
}
}
}
Output :

Variable Description:
Variable Type Purpose
a Integer array To input 10 decimal numbers..
I Int Used in for loop.
b Integer array To store the truncated numbers.
Program 76:
Question: Write a program to input a number. Use a function int Armstrong(int n)
to accept the number. The function returns 1, if the number is Armstrong,
otherwise zero(0).
Program :
import java.util.Scanner;
public class prg66
{
public int armstrong(int n) {
int num = n, cubeSum = 0;
while (num > 0) {
int digit = num % 10;
cubeSum = cubeSum + (digit * digit * digit);
num /= 10;
}
if (cubeSum == n)
return 1;
else
return 0;
}
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter Number: ");
int num = in.nextInt();
prg66 obj = new prg66();
int r = obj.armstrong(num);
if (r == 1)
System.out.println(num + " is an Armstrong number");
else
System.out.println(num + " is not an Armstrong number");
}
}

Output :

Variable Description:
Variable Type Purpose
num Int To store the number.
cubeSum Int To store the sum of the cube of the digits..
digit Int To extract digits from num.
r Int To get the return value
Program 77:
Question: Write a program to input a number and check and print whether it is a
'Pronic' number or not. Use a function int Pronic(int n) to accept a number. The
function returns 1, if the number is 'Pronic', otherwise returns zero (0).
Program :
import java.util.Scanner;
public class prg67
{
public int pronic(int n) {
int isPronic = 0;
for (int i = 1; i <= n - 1; i++) {
if (i * (i + 1) == n) {
isPronic = 1;
break;
}
}
return isPronic;
}
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the number to check: ");
int num = in.nextInt();
prg67 obj = new prg67();
int r = obj.pronic(num);
if (r == 1)
System.out.println(num + " is a pronic number");
else
System.out.println(num + " is not a pronic number");
}
}
Output :

Variable Description:
Variable Type Purpose
isPronic Int To return 1 or 0
i Int Used in for loop.
num Int To input a number.
r Int To get the return value
Program 78:
Question: Write a program to enter a two digit number and find out its first factor
excluding 1 (one). The program then find the second factor (when the number is
divide by the first factor) and finally displays both the factors.
Program :

import java.util.Scanner;

public class prg68

public void fact(int n) {

if (n < 10 || n > 99) {

System.out.println("ERROR!!! Not a 2-digit number");

return;

int i;

for (i = 2; i <= n; i++) {

if (n % i == 0)

break;

int sf = n / i;

System.out.println(i + ", " + sf);

public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.print("Enter number: ");

int num = in.nextInt();

prg68 obj = new prg68();


obj.fact(num);

}
Output :

Variable Description:
Variable Type Purpose
i Int Used in for loop for finding the first factor.
num Int To input a number.
sf Int To get the second number.
Program 79:
Question: Write a function fact(int n) to find the factorial of a number n. Include a
main class to find the value of S where:
S = n! / (m!(n - m)!)
where, n! = 1 x 2 x 3 x .......... x n
Program :

import java.util.Scanner;

public class prg69

public long fact(int n) {

long f = 1;

for (int i = 1; i <= n; i++) {

f *= i;

return f;

public static void main(String args[]) {

prg69 obj = new prg69();

Scanner in = new Scanner(System.in);

System.out.print("Enter m: ");

int m = in.nextInt();

System.out.print("Enter n: ");

int n = in.nextInt();

double s = (double)(obj.fact(n)) / (obj.fact(m) * obj.fact(n - m));

System.out.println("S=" + s);

}
Output :

Variable Description:
Variable Type Purpose
i Int Used in for loop .
f Long To get the factorial of number.
m Int To input a number.
n Int To input a number.
s Double To calculate the value of S.
Program 80:
Question: Write a program in Java to accept a String from the user. Pass the
String to a function Display(String str) which displays the consonants present in
the String.
Program :

import java.util.Scanner;

public class prg70

public void display(String str) {

String t = str.toUpperCase();

int len = t.length();

for (int i = 0; i < len; i++) {

char ch = t.charAt(i);

if (ch != 'A' &&

ch != 'E' &&

ch != 'I' &&

ch != 'O' &&

ch != 'U') {

System.out.println(str.charAt(i));

public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.print("Enter string: ");

String s = in.nextLine();
prg70 obj = new prg70();

obj.display(s);

}
Output :

Variable Description:
Variable Type Purpose
t String To convert the String to uppercase.
len Int To get the length of t.
i Int Used in for loop.
s Double To input a String.
Program 81:
Question : Write a program in Java to input a character. Find and display the
next 10th character in the ASCII table.
Program :
import java.util.Scanner;
public class prg81
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a character: ");
char ch = in.next().charAt(0);
char nextCh = (char)(ch + 10);
System.out.println("Tenth character from "
+ ch + " is " + nextCh);
}
}
Output :

Variable Description:
Variable Type Purpose
ch Char To input to a character
NextCh Char To find the 10th character in the ascii table after ch
Program 82:
Question : Write a program in Java to input a character. Display next 5
characters
Program :
import java.util.Scanner;
public class prg82
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a character: ");
char ch = in.next().charAt(0);
System.out.println("Next 5 characters from "
+ ch + " are:");
for (int i = 1; i <= 5; i++) {
System.out.println(++ch);
}
}
}
Output :

Variable Description:
Variable Type Purpose
ch Char To input a character
i Int Used in for loop
Program 83:
Question : Write a program in Java to generate all the alternate letters in the
range of letters from A to Z.
Program :
public class prg83
{
public static void main(String args[]) {
for (char ch = 'A'; ch <= 'Z'; ch = (char)(ch + 2)) {
System.out.println(ch);
}
}
}
Output :

Variable Description:
Variable Type Purpose
ch Char Used in for loop to generate the characters
Program 84:
Question : Write a program to input a set of 20 letters. Convert each letter into
upper case. Find and display the number of vowels and number of consonants
present in the set of given letters.
Program :
import java.util.Scanner;
public class prg84
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter any 20 letters");
int vc = 0, cc = 0;
for (int i = 0; i < 20; i++) {
char ch = in.next().charAt(0);
ch = Character.toUpperCase(ch);
if (ch == 'A' ||
ch == 'E' ||
ch == 'I' ||
ch == 'O' ||
ch == 'U')
vc++;
else if (ch >= 'A' && ch <= 'Z')
cc++;
}
System.out.println("Number of Vowels = " + vc);
System.out.println("Number of Consonants = " + cc);
}
}
Output :

Variable Description:
Variable Type Purpose
ch Char Used to input the characters
i Int Used in for loop
Program 85:
Question : Write a program in Java to accept an integer number N such that
0<N<27. Display the corresponding letter of the alphabet
Program :
import java.util.Scanner;
public class prg85
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter integer: ");
int n = in.nextInt();
if (n > 0 && n < 27) {
char ch = (char)(n + 64);
System.out.println("Corresponding letter = " + ch);
}
else {
System.out.println("Please enter a number in 1 to 26 range");
}
}
}
Output :

Variable Description:
Variable Type Purpose
ch Char To find the corresponding character of the number
n Int Used to input the number
Program 86:
Question : Write a program to input two characters from the keyboard. Find the
difference (d) between their ASCII codes. Display the following messages:
If d=0 : both the characters are same.
If d<0 : first character is smaller.
If d>0 : second character is smaller.
Program :
import java.util.Scanner;
public class prg86
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter first character: ");
char ch1 = in.next().charAt(0);
System.out.print("Enter second character: ");
char ch2 = in.next().charAt(0);
int d = (int)ch1 - (int)ch2;
if (d > 0)
System.out.println("Second character is smaller");
else if (d < 0)
System.out.println("First character is smaller");
else
System.out.println("Both the characters are same");
}
}
Output :
Variable Description:
Variable Type Purpose
ch1 Char To input first character
d Int Used to find the difference between the character
ch2 Char To input second character
Program 87:
Question : Write a program to input a set of any 10 integer numbers. Find the
sum and product of the numbers. Join the sum and product to form a single
number. Display the concatenated number.
Program :
import java.util.Scanner;

public class prg87


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter 10 integers");
long sum = 0, prod = 1;
for (int i = 0; i < 10; i++) {
int n = in.nextInt();
sum += n;
prod *= n;
}

String s = Long.toString(sum) + Long.toString(prod);


long r = Long.parseLong(s);

System.out.println("Concatenated Number = " + r);


}
}
Output :
Variable Description:
Variable Type Purpose
prod Long To find the product of the no.s
i Int Used in for loop
sum Long To find the sum of the no.s
n Int To input the no.
s String To find concatenated no.
r Long To convert the string to integer
Program 88:
Question : Write a menu driven program to generate the upper case letters from
Z to A and lower case letters from 'a' to 'z' as per the user's choice.
Enter '1' to display upper case letters from Z to A and Enter '2' to display lower
case letters from a to z.
Program :
import java.util.Scanner;
public class 88
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter '1' to display upper case letters from Z to A");
System.out.println("Enter '2' to display lower case letters from a to z");
System.out.print("Enter your choice: ");
int ch = in.nextInt();
int count = 0;
switch (ch) {
case 1:
for (int i = 90; i > 64; i--) {
char c = (char)i;
System.out.print(c);
System.out.print(" ");
count++;
//Print 10 characters per line
if (count == 10) {
System.out.println();
count = 0;
}
}
break;
case 2:
for (int i = 97; i < 123; i++) {
char c = (char)i;
System.out.print(c);
System.out.print(" ");
count++;
//Print 10 characters per line
if (count == 10) {
System.out.println();
count = 0;
}
}
break;
default:
System.out.println("Incorrect Choice");
}
}
}
Output :
Variable Description:
Variable Type Purpose
count Int To count the no. of characters printed
i Int Used in for loop
ch Int To input the users choice
c Char To convert int to char
Program 89:
Question : Write a program to input a letter. Find its ASCII code. Reverse the
ASCII code and display the equivalent character.
Program :
import java.util.Scanner;
public class 89
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a letter: ");
char l = in.next().charAt(0);
int a = (int)l;
System.out.println("ASCII Code = " + a);
int r = 0;
while (a > 0) {
int digit = a % 10;
r = r * 10 + digit;
a /= 10;
}
System.out.println("Reversed Code = " + r);
System.out.println("Equivalent character = " + (char)r);
}
}
Output :
Variable Description:
Variable Type Purpose
a Int To convert char to int
digit Int To extract digits digits from a
r Int To store reverse ascii code
l Char To input letters
Program 90:
Question : Write a menu driven program to display
(i) first five upper case letters
(ii) last five lower case letters as per the user's choice.
Enter '1' to display upper case letters and enter '2' to display lower case letters.
Program :
import java.util.Scanner;
public class KboatMenuUpLowCase
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter '1' to display upper case letters");
System.out.println("Enter '2' to display lower case letters");
System.out.print("Enter your choice: ");
int ch = in.nextInt();
switch (ch) {
case 1:
for (int i = 65; i <= 69; i++)
System.out.println((char)i);
break;
case 2:
for (int i = 118; i <= 122; i++)
System.out.println((char)i);
break;
default:
break;
}
}
}
Output :

Variable Description:
Variable Type Purpose
i Int Used in for loop
ch Int To input the users choice
Program 91:
Question : Write a program in Java to accept a String in upper case and replace
all the vowels present in the String with Asterisk (*) sign.
Program :
import java.util.Scanner;
public class prg91
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a string in uppercase:");
String str = in.nextLine();
String newStr = "";
int len = str.length();
for (int i = 0; i < len; i++) {
char ch = str.charAt(i);
if ("AEIOU".indexOf(ch) != -1) {
newStr = newStr + '*';
}
else {
newStr = newStr + ch;
}
}
System.out.println(newStr);
}
}
Output :

Variable Description:
Variable Type Purpose
str String To input a string.
newStr String To store the final string.
len Int To store the length of str
i Int Used in for loop.
ch Char To extract character from String.
Program 92:
Question : Write a program in Java to enter a sentence. Frame a word by joining
all the first characters of each word of the sentence. Display the word.
Program :
import java.util.Scanner;
public class prg92
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a sentence:");
String str = in.nextLine();
String word = "" + str.charAt(0);
int len = str.length();
for (int i = 0; i < len; i++) {
char ch = str.charAt(i);
if (ch == ' ')
word += str.charAt(i + 1);
}
System.out.println(word);
}
}
Output :
Variable Description:
Variable Type Purpose
str String To input a string.
word String To store the new word.
len Int To store the length of str
i Int Used in for loop.
ch Char To extract character from String.
Program 93:
Question : Write a program to accept a sentence. Display the sentence in
reversing order of its word.
Program :
import java.util.Scanner;
public class prg93
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a sentence:");
String str = in.nextLine();
str = " " + str;
String word = "";
int len = str.length();
for (int i = len - 1; i >= 0; i--) {
char ch = str.charAt(i);
if (ch == ' ') {
System.out.print(word + " ");
word = "";
}
else {
word = ch + word;
}
}
}
}
Output :

Variable Description:
Variable Type Purpose
str String To input a string.
word String To extract words from the string.
len Int To store the length of str
i Int Used in for loop.
ch Char To extract character from String.
Program 94:
Question : Write a program to input a sentence and display the word of the
sentence that contains maximum number of vowels.
Program :
import java.util.Scanner;
public class KboatMaxVowelWord
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a sentence:");
String str = in.nextLine();
str = str + " ";
String word = "", mWord = "";
int count = 0, maxCount = 0;
int len = str.length();
for (int i = 0; i < len; i++) {
char ch = Character.toUpperCase(str.charAt(i));
if ("AEIOU".indexOf(ch) != -1) {
count++;
}
if (ch == ' ') {
if (count > maxCount) {
maxCount = count;
mWord = word;
}
word = "";
count = 0;
}
else {
word += ch;
}
}
System.out.println("The word with maximum number of vowels: "+ mWord);
}
}
Output :

Variable Description:
Variable Type Purpose
str String To input a string.
word String To extract words from the string.
len Int To store the length of str
i Int Used in for loop.
ch Char To extract character from String.
count Int To count the number of vowels.
maxCount Int To store the maximum number of vowels
mWord String To store the word with maximum vowels.
Program 95:
Question : Write a program to accept a string. Convert the string into upper case
letters. Count and output the number of double letter sequences that exist in the
string.
Program :
import java.util.Scanner;
public class prg95
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter string: ");
String s = in.nextLine();
String str = s.toUpperCase();
int count = 0;
int len = str.length();
for (int i = 0; i < len - 1; i++) {
if (str.charAt(i) == str.charAt(i + 1))
count++;
}
System.out.println("Double Letter Sequence Count = " + count);
}
}
Output :
Variable Description:
Variable Type Purpose
str String To convert s to uppercase.
s String To input a string.
len Int To store the length of str
i Int Used in for loop.
count Int To count the number double letter sequences.

Program 96:
Question : Write a program to accept a word (say, BLUEJ) and display the
pattern:
BLUEJ
BLUE
BLU
BL
B
Program :
import java.util.Scanner;
public class prg96
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a word: ");
String word = in.nextLine();
int len = word.length();
for (int i = len - 1; i >= 0; i--) {
for (int j = 0; j <= i; j++) {
char ch = word.charAt(j);
System.out.print(ch);
}
System.out.println();
}
}
}
Output :

Variable Description:
Variable Type Purpose
word String To input a word.
j Int Used in inner for loop.
len Int To store the length of word
i Int Used in outer for loop.
ch Char To extract characters from word.
Program 97:
Question : Write a program to accept a word (say, BLUEJ) and display the
pattern:
J
EE
UUU
LLLL
BBBBB
Program :
import java.util.Scanner;
public class prg97
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a word: ");
String word = in.nextLine();
int len = word.length();
for (int i = len - 1; i >= 0; i--) {
for (int j = len - 1; j >= i; j--) {
char ch = word.charAt(i);
System.out.print(ch);
}
System.out.println();
}
}
}
Output :

Variable Description:
Variable Type Purpose
word String To input a word.
j Int Used in inner for loop.
len Int To store the length of word
i Int Used in outer for loop.
ch Char To extract characters from word.
Program 98:
Question : Write a program to accept a word (say, BLUEJ) and display the
pattern:
BLUEJ
LUEJ
UEJ
EJ
J
Program :
import java.util.Scanner;
public class prg98
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a word: ");
String word = in.nextLine();
int len = word.length();
for (int i = 0; i < len; i++) {
for (int j = i; j < len; j++) {
char ch = word.charAt(j);
System.out.print(ch);
}
System.out.println();
}
}
}

Output :
Variable Description:
Variable Type Purpose
word String To input a word.
j Int Used in inner for loop.
len Int To store the length of word
i Int Used in outer for loop.
ch Char To extract characters from word.
Program 99:
Question : Write a program to display the pattern:

ABCDE
ABCDA
ABCAB
ABABC
AABCD
Program :
public class prg99
{
public static void main(String args[]) {
String word = "ABCDE";
int len = word.length();
for (int i = 0; i < len; i++) {
for (int j = 0; j < len - i; j++) {
System.out.print(word.charAt(j));
}
for (int k = 0; k < i; k++) {
System.out.print(word.charAt(k));
}
System.out.println();
}
}
}
Output :
Variable Description:
Variable Type Purpose
word String To store “ABCDE”
j Int Used in second inner for loop.
len Int To store the length of word
i Int Used in outer for loop.
k Char Used in second inner for loop.
Program 99:
Question : Write a program to accept 10 names in a Single Dimensional Array
(SDA). Display the names whose first letter matches with the letter entered by
the user.
Program :
import java.util.Scanner;
public class KboatSDANames
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
String names[] = new String[10];
System.out.println("Enter 10 names");
for (int i = 0; i < names.length; i++) {
names[i] = in.nextLine();
}
System.out.print("Enter a letter: ");
char ch = in.next().charAt(0);
ch = Character.toUpperCase(ch);
for (int i = 0; i < names.length; i++) {
if (Character.toUpperCase(names[i].charAt(0)) == ch) {
System.out.println(names[i]);
}
}
}
}
Output :

Variable Description:
Variable Type Purpose
names String array To store 10 names.
i Int Used in for loop.
ch Int To input a character from the user.

You might also like