Computer Project

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 38

NAME : EMSAME SHALLAM

CLASS : 10
SECTION : B
ROLL NO : 32
Program 1:
Write a program in java to input a positive number from the user. If the entered number is
negative or zero, print a suitable message, if the entered number is positive then print the
multiplication table of the number in the given format.(The table must have all the products
from 1 to 20)
Sample Input: Enter a positive number 6
Sample Output:
6x1=6
6 x 2 = 12
.
.
6 x 20 = 120

import java.util.*;
public class p1
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n,i;
System.out.println("Enter a positive number");
n=sc.nextInt();
if(n>0)
{
for(i=1;i<=20;i++)
{
System.out.println(n+" x "+i+" = "+n*i);
}
}
else
System.out.println("Not a positive number");
}
}
VARIABLE DESCRIPTION
Variable name Data Type Description
n Integer To store the number entered by the user
i Integer To run the loop
Program 2:
Write a program to generate and print all prime numbers from 1 to n. The value of n must
be taken as input from the user .
Sample Input: 20
Sample Input: The prime numbers from 1 to n are: 2,3,5,7,11,13,17,19

import java.util.*;
public class p2
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int n,i,c;
System.out.println("Enter the range");
n=sc.nextInt();
System.out.print("The prime numbers from 1 to n are: ");
for(i=1;i<=n;i++)
{
c=0;
for(int j=1;j<=i;j++)
{
if(i%j==0)
c++;
}
if(c==2)
System.out.print(+i+",");
}
}
}
VARIABLE DESCRIPTION
Variable name Date type Description
n Integer To store the range entered by the user
i Integer To run the first for loop
c Integer To check if the number is prime or not
j Integer To run the second for loop

Program 3:
Using switch case, write a program to perform the following task:
i) To convert a decimal number to binary
ii) To convert a binary number to decimal

import java.util.*;
public class p3
{
public static void main()
{
Scanner sc=new Scanner(System.in);
String bin="",bin1="";
int dec=0,pow=0,n,l,ch,i;
System.out.println("Press 1 for Decimal to Binary\nPress 2 for Binary to Decimal");
ch=sc.nextInt();
switch(ch)
{
case 1:
System.out.println("Enter the Decimal number");
n=sc.nextInt();
while(n!=0)
{
bin+=n%2;
n/=2;
}
l=bin.length();
for(i=1;i<=l;i++)
{
bin1+=bin.charAt(l-i);
}
System.out.println("The Binary equivalent: "+bin1);
break;
case 2:
System.out.println("Enter the Binary number");
n=sc.nextInt();
while(n>0)
{
i=n%10;
dec+=i*Math.pow(2,pow);
n/=10;
pow++;
}
System.out.println("The Decimal Equivalent: "+dec);
break;
default:
System.out.println("Please enter a valid choice");
}
}
}
VARIABLE DESCRIPTION
Variable name Data Type Description
bin String To store the reversed binary equivalent
bin1 String To store the binary equivalent in correct order
dec Integer To store the decimal equivalent
pow Integer To store the power f the exponent
ch Integer To store the choice made by the user
l Integer To store the length of the ‘bin’ string
i Integer To run the for loop
n Integer T store the number entered by the user

Program 4:
Write a program to input a string from the user and perform the following tasks:
i) Print the number of vowels in the string
ii) Print the number of consonants in the string
iii) Print all the vowels in the order it appeared in the string
iv) Print all the consonants in the order it appeared in the string
Sample Input: COMPUTER
Sample Output: Number of vowels: 3
Number of consonants: 5
The vowels in the String: OUE
The consonants in the String: CMPTR

import java.util.*;
public class p4
{
public static void main()
{
Scanner sc=new Scanner(System.in);
String st,vst="",cst="";
int l,v=0,c=0;
char ch;
System.out.println("Enter a string");
st=sc.nextLine();
l=st.length();
for(int i=0;i<l;i++)
{
ch=st.charAt(i);
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U'||ch=='a'||ch=='e'||ch=='i'||ch=='o'||
ch=='u')
{
v+=1;
vst=vst+ch;
}
else
{
c+=1;
cst=cst+ch;
}
}
System.out.println("Number of vowels: "+v);
System.out.println("Number of consonants:" +c);
System.out.println("The Vowels in the String: "+vst);
System.out.println("The Consonants in the String: "+cst);
}
}
VARIABLE DESCRIPTION
Variable Name Data Type Description
st String To store the string entered by the user
vst String To store the vowels in the string
cst String To store the consonants in the string
l Integer To store the length of the string
v Integer To store the number of vowels
c Integer To store the number of consonants
ch Character To store a character in the string

Program 5:
A company has 120 employees who are divided into four grades as follows:
Grade Basic (Rs. Per month) D.A.(% of Basic) H.R.A(% of Basic)
1 10,000 or more 40% 30%
2 5,000-10,000 40% 25%
3 <5,000 but >2,000 30% 20%
4 2,000 or less 30% 15%
Salary=Basic + DA+ HRA
If the salary is above Rs. 50,000 per month then Income Tax= 30%*(salary-50,000)
Using arrays accept the names of the employees and the Basic(monthly) pay for 120
employees. Print the pay slip which contains Name, Basic monthly Pay, DA, HRA, Monthly
Income Tax and Net Monthly Salary.
Write a program to perform this job.

import java.io.*;
public class p5
{
public static void main(String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
double bas[]=new double[120];
double da[]=new double[120];
double hra[]=new double[120];
double sal[]=new double[120];
double tax[]=new double[120];
String name[]=new String[120];
int i;
for(i=0;i<120;i++)
{
System.out.println("Enter the name ");
name[i]=in.readLine();
System.out.println("Enter the Basic pay ");
bas[i]=Double.parseDouble(in.readLine());
}
for(i=0;i<120;i++)
{
if(bas[i]>=10000)
{
da[i]=40.0/100.0*bas[i];
hra[i]=30.0/100.0*bas[i];
}
if(bas[i]<10000 && bas[i]>=5000)
{
da[i]=40.0/100.0*bas[i];
hra[i]=25.0/100.0*bas[i];
}
if(bas[i]<5000 && bas[i]>=2000)
{
da[i]=30.0/100.0*bas[i];
hra[i]=20.0/100.0*bas[i];
}
if(bas[i]<2000)
{
da[i]=30.0/100.0*bas[i];
hra[i]=15.0/100.0*bas[i];
}
}
for(i=0;i<120;i++)
{
sal[i]=bas[i]+da[i]+hra[i];
if(sal[i]>50000)
tax[i]=30.0/100.0*(sal[i]-50000);
System.out.println("Name: "+name[i]);
System.out.println("Basic monthly pay: "+bas[i]);
System.out.println("DA: "+da[i]);
System.out.println("HRA: "+hra[i]);
System.out.println("Monthly Income Tax: "+tax[i]);
System.out.println("Net Monthly Salary: "+sal[i]);
}
}
}
VARIABLE DESCRIPTION
Variable name Data Type Description
bas[] Double To store the basic salary of the employees
da[] Double To store the DA after calculation
hra[] Double To store the HRA after calculation
tax[] Double To store the amount of tax
sal[] Double To store the total salary
name[] String To store the names of the employees
i Integer To run the loops

Program 6:
Write a menu driven program to perform the following tasks:
a) To calculate and print the factorial of all numbers from 1 to n(stake n as input)
b) To print the series 1,2,4,7,11,16,22.... up to n (Take n as input)

import java.util.*;
public class p6
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int ch,i,j,n,ans=1;
System.out.println("Press 1 for Factorial\nPress 2 for Series");
ch=sc.nextInt();
switch(ch)
{
case 1:
System.out.println("Enter the range");
n=sc.nextInt();
for(i=1;i<=n;i++)
{
ans=1;
for(j=1;j<=i;j++)
{
ans=ans*j;
}
System.out.println(i+"="+ans);
}
break;
case 2:
System.out.println("Enter the range");
n=sc.nextInt();
for(i=0;i<=n;i++)
{
ans=ans+i;
System.out.print(ans+",");
}
break;
default:
System.out.println("Wrong choice");
}
}
}
VARIABLE DESCRIPTION
Variable name Data Type Description
ch Integer To store the choice made by the user
i Integer To run the first for loop
j Integer To run the second for loop in case 1
n Integer To store the range
ans Integer To store the value to be printed

Program 7:
Write a program to accept a word from the user and check if it’s a Palindrome word or not.
Sample Input: MADAM
Sample Output: It is a Palindrome Word

import java.util.*;
public class p7
{
public static void ain()
{
Scanner sc=new Scanner(System.in);
String st,st1="";
int l;
char ch;
System.out.println("Enter a String");
st=sc.next();
l=st.length();
for(int i=1;i<=l;i++)
{
ch=st.charAt(l-i);
st1=st1+ch;
}
if(st.equals(st1))
System.out.println("It is a Palindrome word");
else
System.out.println("It is not a Palindrome word");
}
}
VARIABLE DESCRIPTION
Variable name Data Type Description
st String To store the string entered by the user
st1 String To store the reversed string
l Integer To store the length of the string
ch Character To temporary store a character of the string
i Integer To run the for loop

Program 8:
Write a program to accept a string and check if the string is a Pangram or not.
“ A string is a pangram if it contains all the character of the alphabets ignoring the case of the
alphabets”
Sample Input: The quick brown fox jumps over a lazy dog
Sample Output: It is a Pangram String

import java.util.*;
public class p8
{
public static void main()
{
Scanner sc=new Scanner(System.in);
String str;
System.out.println("Enter a String");
str=sc.nextLine();
str=str.toLowerCase();
boolean present=true;
for(char ch='a';ch<='z';ch++)
{
if(!str.contains(String.valueOf(ch)))
{
present=false;
break;
}
}
if(present==true)
System.out.println("It is a Pangram String");
else
System.out.println("It is not a Pangram String");
}
}
VARIABLE DESCRIPTION
Variable name Data Type Description
str String To store the string entered by the user
present Boolean To check if each letter is present or not
ch Character To store the character to be searched
Program 9:
Write a program in java to initialize the given arrays
State[]={"Arunachal Pradesh","Assam","Manipur","Meghalaya","Mizoram" ,"Nagaland",
"Tripura"};
String Cap[]={"Itanagar","Dispur","Imphal","Shillong","Aizawl","Kohima","Agartala"};
Accept a name of any state from the user and search for the name in the array State[] using
binary search. If the state name is found display the name of the state along with its capital. If
the name is not present, then display ”No such state in the Northeast region”.

import java.util.*;
public class p9
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String State[]={"Arunachal Pradesh","Assam","Manipur","Meghalaya","Mizoram",
"Nagaland","Tripura"};
String Cap[]={"Itanagar","Dispur","Imphal","Shillong","Aizawl","Kohima","Agartala"};
int i;
boolean b=false;
String ch;
System.out.println("Enter the State to be searched");
ch=sc.nextLine();
for(i=0;i<7;i++)
{
if(ch.equals(State[i]))
{
b=true;
break;
}
}
if(b==true)
System.out.println("The capital of "+State[i]+" is " +Cap[i]);
else
System.out.println("No such state in the Northeast Region");
}
}
VARIABLE DESCRIPTION
Variable name Data Type Description
State[] String To store the states
Cap[] String To store the capital
i Integer To run the for loop
b Boolean To check if the state is present
ch String To store the state to be to searched

Program 10:
Write a program to declare two arrays A[] and B[] each of size 10. Accept input of numbers
for both array A[] and B[]. Now calculate the sum of corresponding values of the two arrays
and store the sum in another array S[].Finally display the three arrays in three columns.
Sample:
If array A[]={2,4,6,8,10, 12,14,16,18,20} and array B[]={1,2,3,4,5,6,7,8,9,10} then the output
must be
A[] B[] S[]
2 1 3
4 2 6
6 3 9
8 4 12
10 5 15
12 6 18
14 7 21
16 8 24
18 9 27
20 10 30

import java.util.*;
public class p10
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int a[]=new int[10];
int b[]=new int[10];
int s[]=new int[10];
int i;
for(i=0;i<10;i++)
{
System.out.println("Enter the first number");
a[i]=sc.nextInt();
System.out.println("Enter the second number");
b[i]=sc.nextInt();
}
for(i=0;i<10;i++)
{
s[i]=a[i]+b[i];
}
System.out.println("A[]\tB[]\tS[]\t");
for(i=0;i<10;i++)
{
System.out.println(a[i]+"\t"+b[i]+"\t"+s[i]);
}
}
}

VARIABLE DESCRIPTION
Variable name Data Type Description
a[] Integer To store the first number
b[] Integer To store the second number
s[] Integer To store the sum
i Integer To run the loop and determine the value of
the subscript in the arrays

Program 11:
Write a program in java to check if a number is a sunny number or not.
A number is called a sunny number if the number next t the given number is a perfect square.
In other words, a number N will be a sunny number if N+1 is a perfect square.

import java.util.*;
public class p11
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int n,i,c=0;
System.out.println("Enter a number");
n=sc.nextInt();
int n1=n+1;
for(i=1;i<=n1;i++)
{
if(i*i==n1)
{
c=1;
break;
}
}
if(c==1)
System.out.println(n+" is a Sunny Number");
else
System.out.println(n+" is not a Sunny Number");
}
}
VARIABLE DESCRIPTION
Variable name Data Type Description
n Integer To store the number entered by the user
i Integer To run the for loop
c Integer To determine the output
n1 Integer To store the value of ’N+1’

Program 12:
Write a program to overload a function add( ) as follows:
i) void add (int a, int b) : to find the sum of two integers
ii) void add (double x, double y) : to find the sum of two real numbers
iii) void add (String s1, String s2) : to join two strings s1 and s2 with a space in between
Write the main function to invoke the above three functions.

import java.io.*;
public class p12
{
void add(int a,int b)
{
int s=a+b;
System.out.println("Sum of the two Integers= "+s);
}
void add(double x,double y)
{
double sum=x+y;
System.out.println("Sum of the two Real numbers= "+sum);
}
void add(String s1,String s2)
{
String st=s1+" "+s2;
System.out.println("The joined String: "+st);
}
public static void main()throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
p12 ob=new p12();
System.out.println("Enter two Integers");
int l=Integer.parseInt(in.readLine());
int m=Integer.parseInt(in.readLine());
ob.add(l,m);
System.out.println("Enter two Real numbers");
double c=Double.parseDouble(in.readLine());
double d=Double.parseDouble(in.readLine());
ob.add(c,d);
System.out.println("Enter two Strings");
String st1=in.readLine();
String st2=in.readLine();
ob.add(st1,st2);
}
}
VARIABLE DESCRIPTION
Variable name Data Type Description
a Integer To store the first number
b Integer To store the second number
x Double To store the first real number
y Double To store the second real number
s1 String To store the first string
s2 String To store the second string
s Integer To store the sum of the integers
sum Double To store the sum of the real numbers
st String To store the joined strings
l Integer To accept the first Integer
m Integer To accept the second Integer
c Double To accept the first real number
d Double To accept the second real number
st1 String To accept the first string
st2 String To accept the second string

Program 13:
Write a program to overload a function fibo( ) as follows:
a) void fibo(int n) : To print if the number ‘n’ is present in the Fibonacci series or not
b) void fibo( int start, int stop) : To print all the Fibonacci numbers from ‘start’ to ‘stop’
In the main function create a menu to display the above two options and invoke the functions
as per user’s choice.

import java.util.*;
public class p13
{
void fibo(int n)
{
int a=0,b=1,c=0;
while(c<n)
{
c=a+b;
a=b;
b=c;
}
if(c==n)
System.out.println(n+" is present in the Fibonacci series");
else
System.out.println(n+" is not present in the Fibonacci series");
}
void fibo(int start,int stop)
{
int a=0,b=1,c=0;
while(c<=stop)
{
c=a+b;
a=b;
b=c;
if(a>=start)
System.out.println(a);
}
}
public static void main()
{
Scanner sc=new Scanner(System.in);
int ch,n,stop;
System.out.println("Press 1 to search for a number\nPress 2 to print the numbers");
ch=sc.nextInt();
p13 ob=new p13();
switch(ch)
{
case 1:
System.out.println("Enter the number to be searched");
n=sc.nextInt();
ob.fibo(n);
break;
case 2:
System.out.println("Enter the start and stop");
n=sc.nextInt();
stop=sc.nextInt();
ob.fibo(n,stop);
break;
default:
System.out.println("Please enter a valid option");
}
}
}
VARIABLE DESCRIPTION
Variable name Data Type Description
ch Integer To store the choice made by the user
q Integer To store the first number entered
w Integer To store the second number entered
n Integer To store the number to be searched in the
first case and the value of ‘start’ in the
second case
a Integer To store the first number in the Fibonacci
series
b Integer To store the second number in the Fibonacci
series
c Integer To store the sum of the first and second
number in the Fibonacci series
start Integer To store the start of the range in the first
case
stop Integer To store the end of the range in the second
case
Program 14:
Write a program in java with the following specifications:
Class name: Cylinder
Instance variables: base, height, vol,ca,ta;
Methods/Functions:
void input():to input the base and height of the cylinder
void volume(): to calculate and print the volume of the cylinder
void csa(): to calculate and print the curved surface area of the cylinder
void tsa(): to calculate and print the total surface area of the cylinder
void display (): to print vol, ca and ta
Write the main function to call the above methods.

import java.util.*;
public class Cylinder
{
double base,height,vol,ca,ta;
void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the base of the Cylinder");
base=sc.nextDouble();
System.out.println("Enter the height of the Cylinder");
height=sc.nextDouble();
}
void volume()
{
vol=22.0/7.0*base*base*height;
}
void csa()
{
ca=2*22.0/7.0*base*height;
}
void tsa()
{
ta=2*22.0/7.0*base*(base+height);
}
void display()
{
System.out.println("Volume of cylinder= "+vol);
System.out.println("Curved surface area of cylinder= "+ca);
System.out.println("Total surface area of cylinder= "+ta);
}
public static void main()
{
Cylinder ob=new Cylinder();
ob.input();
ob.volume();
ob.csa();
ob.tsa();
ob.display();
}
}
VARIABLE DESCRIPTION
Variable name Data type Description
base Double To store the base of the cylinder
height Double To store the height of the cylinder
vol Double To store the volume of the cylinder
ca Double To store the curved surface area of the
cylinder
ta Double To store the total surface area of the
cylinder
Program 15:
A private Cab service company provides service within the city at the following rates:
AC CAR NON-AC CAR
Upto 5 KM Rs. 150/- Rs. 120/-
Beyond 5 KM Rs. 10/- PER KM Rs. 08/- PER KM
Design a class CabService with the following description:
Member variables/ data members:
String car_type- To store the type of car(AC or NON-AC)
Double km- To store the kilometre travelled
Double bill- To calculate and store the bill amount
Member methods:
CabService()- Default constructor to initialize data members. String data members to “ ” and
double data members to 0.0
void accept()- To accept car_type and km
void calculate()- To calculate the bill as per the rules given above
void display()- To Display the bill as per the following format:
CAR TYPE:
KILOMETER TRAVELLED:
TOTAL BILL:
Create an object of the class in the main method and invoke the member methods.

import java.util.*;
public class CabService
{
String car_type;
double km,bill;
Scanner sc=new Scanner(System.in);
double CabService()
{
String car_type="";
double km=0.0,bill=0.0;
return(km);
}
void accept()
{
System.out.println("Enter kilometers travelled");
km=sc.nextDouble();
System.out.println("Enter Car Type");
car_type=sc.next();
}
void calculate()
{
double km1;
if(car_type.equals("AC"))
{
if(km>5)
{
km1=km-5;
bill=150+(km1*10);
}
else
bill=150.0;
}
else
{
if(km>5)
{
km1=km-5;
bill=120+8*km1;
}
else
bill=120.0;
}
}
void display()
{
System.out.println("CAR TYPE: "+car_type);
System.out.println("KILOMETER TRAVELLED: "+km);
System.out.println("TOTAL BILL: "+bill);
}
public static void main()
{
CabService ob=new CabService();
double f=ob.CabService();
ob.accept();
ob.calculate();
ob.display();
}
}
VARIABLE DESCRIPTION
Variable name Data Type Description
f Double To store the return value
car_type String To store the car type
km Double To store the kilometres travelled
bill Double To store the total bill

Write a program using switch case to design a calculator .The calculator must be able to
perform the following tasks.
i) addition of 2 numbers
ii) subtraction of 2 number
iii) product of 2 numbers
vi) quotient of 2 numbers
v) remainder of 2 numbers
vi) square root of a number
vii) cube root of a number
viii)power of a base

import java.util.*;
public class p16
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int ch,a,b;double ans=0;
System.out.println("1-Addition\n2-Subtraction\n3-Multiplication\n4-Division\n5-
Remainder\n6-Square Root\n7-Cube Root\n8-Power of a Base");
ch=sc.nextInt();
switch(ch)
{
case 1:
System.out.println("Enter the two numbers");
a=sc.nextInt();
b=sc.nextInt();
ans=a+b;
break;
case 2:
System.out.println("Enter the two numbers");
a=sc.nextInt();
b=sc.nextInt();
ans=a-b;
break;
case 3:
System.out.println("Enter the two numbers");
a=sc.nextInt();
b=sc.nextInt();
ans=a*b;
break;
case 4:
System.out.println("Enter the two numbers");
a=sc.nextInt();
b=sc.nextInt();
ans=a/b;
break;
case 5:
System.out.println("Enter the two numbers");
a=sc.nextInt();
b=sc.nextInt();
ans=a%b;
break;
case 6:
System.out.println("Enter the number");
a=sc.nextInt();
ans=Math.sqrt(a);
break;
case 7:
System.out.println("Enter the number");
a=sc.nextInt();
ans=Math.cbrt(a);
break;
case 8:
System.out.println("Enter the base");
a=sc.nextInt();
System.out.println("Enter the power");
b=sc.nextInt();
ans=Math.pow(a,b);
break;
default:
System.out.println("Please enter a valid choice");
}
System.out.println(ans);
}
}

VARIABLE DESCRIPTION
Variable name Data Type Description
ch Integer To store the choice made by the user
a Integer To store the first number
b Integer To store the second number
ans Double To store the result to be printed

Program 17:
Write a program in java to implement "Movie Ticket Reservation System "with the following
specifications:
 The theater has four categories of seats - silver, gold ,platinum, business
 The cost of tickets are:
silver- Rs 200 per ticket
gold- Rs 300 per ticket
platinum-Rs 400 per ticket
business- Rs 500 per ticket
 The name of the movie playing in the theater can be any movie of your choice
 The movie is screened at the following timings : 8am, 11am, 2 pm, 8 pm
The program must ask the user their name ,category, timing of the movie and number of
tickets.
Calculate the cost of ticket user based on category and number of tickets. Add a GST of 28%
to the cost and calculate the amount to be paid. The output must display the name of the
person, name of the movie, category, timing and amount paid

import java.util.*;
public class p17
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter name: ");
String name=sc.nextLine();
String cat="";
System.out.println("Timings: 8am,11am,2pm,5pm,8pm\nEnter the required timing");
String t=sc.next();
System.out.println("Enter number of tickets");
int n=sc.nextInt();
System.out.println("1-Business\n2-Platinum\n3-Gold\n4-Silver\nSelect seat category");
int ch=sc.nextInt();
double amt=0.0;
switch(ch)
{
case 1:
amt=500.0;
amt=amt+28.0/100.0*amt;
amt=amt*n;
cat="Business";
break;
case 2:
amt=400.0;
amt=amt+28.0/100.0*amt;
amt=amt*n;
cat="Platinum";
break;
case 3:
amt=300.0;
amt=amt+28.0/100.0*amt;
amt=amt*n;
cat="Gold";
break;
case 4:
amt=200.0;
amt=amt+28.0/100.0*amt;
amt=amt*n;
cat="Silver";
break;
default:
System.out.println("Invalid seat");
}
System.out.println("Name: "+name);
System.out.println("Movie: Borsing Returns");
System.out.println("Category: "+cat);
System.out.println("Timing: "+t);
System.out.println("Amount Paid: "+amt);
}
}
VARIABLE DESCRIPTION
Variable name Data Type Description
name String To store the name of the person
cat String To store the category of the seats
t String To store the time
amt Double To store the total amount
n Integer To store the number of tickets
ch Integer To store the choice made by the user
Program 18:
Write a menu driven program to print the given patterns:
1) 0 2) ******
01 *****
010 ****
0101 ***
01010 **
010101 *

import java.util.*;
public class p18
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int ch,i,j;
System.out.println("Press 1 for the First pattern \nPress 2 for the Second pattern");
ch=sc.nextInt();
switch(ch)
{
case 1:
for(i=1;i<=6;i++)
{
for(j=1;j<=i;j++)
{
if(j%2!=0)
System.out.print("0");
else
System.out.print("1");
}
System.out.println();
}
break;
case 2:
for(i=1;i<=6;i++)
{
for(j=6;j>=i;j--)
{
System.out.print("*");
}
System.out.println();
}
break;
default:
System.out.println("Please enter a valid option");
}
}
}
VARIABLE DESCRIPTION
Variable name Data Type Description
ch Integer To store the choice made by the user
i Integer To run the first loop
j Integer To run the second loop

Program 19:
In a class the Mathematics teacher gave an assignment to her students to find the area of a
scalene triangle. However, she writes a program taking the length of the sides as input and
calculates its area. Further, she enters the answers obtained by all 40 students in an SDA.
The program compares the area calculated by the teacher and the answers stored in the
array to display the number of students who have given the correct answer. Write the
program in java to solve the given problem.

import java.util.*;
public class p19
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int a,b,c,i,co=0;
double ans[]=new double[40];
double area,s;
System.out.println("Enter the length of the sides of the triangle");
a=sc.nextInt();
b=sc.nextInt();
c=sc.nextInt();
s=(a+b+c)/2.0;
area=Math.sqrt(s*(s-a)*(s-b)*(s-c));
System.out.println("Enter the answers of the students");
for(i=0;i<40;i++)
{
ans[i]=sc.nextDouble();
if(ans[i]==area)
co++;
}
System.out.println("The number of students who gave the correct answer are: "+co);
}
}
VARIABLE DESCRIPTION
Variable name Data Type Description
a Integer To store the first side of the triangle
b Integer To store the second side of the triangle
c Integer To store the third side of the triangle
i Integer To run the loop
co Integer To count the number of correct answers
s Double To store the semi perimeter of the triangle
area Double To store the area of the triangle
ans[] Double To store the answers of the students
Program 20:
Write a program to accept any three letter word and print all the probable three letter
combinations. No letter combinations should repeat in the output.
Sample Input: TOP
Sample Output: TOP,TPO,OPT,PTO,POT

import java.util.*;
public class p20
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a three letter word");
String st=sc.next();
String com[]=new String[6];
int i;
com[0]=Character.toString(st.charAt(0))+Character.toString(st.charAt(1))
+Character.toString(st.charAt(2));
com[1]=Character.toString(st.charAt(0))+Character.toString(st.charAt(2))
+Character.toString(st.charAt(1));
com[2]=Character.toString(st.charAt(1))+Character.toString(st.charAt(0))
+Character.toString(st.charAt(2));
com[3]=Character.toString(st.charAt(1))+Character.toString(st.charAt(2))
+Character.toString(st.charAt(0));
com[4]=Character.toString(st.charAt(2))+Character.toString(st.charAt(0))
+Character.toString(st.charAt(1));
com[5]=Character.toString(st.charAt(2))+Character.toString(st.charAt(1))
+Character.toString(st.charAt(0));
for(i=0;i<6;i++)
System.out.print(com[i]+",");
}
}
VARIABLE DESCRIPTION
Variable name Data Type Description
st String To store the word entered
i Integer To run the loop
com[] String To store the different
combinations

You might also like