Uploaded by Aditya Sir

Comp Sc Programs ICSE 2021

advertisement
ANKURAM EDUCATION / PROGRAMMERS POINT
Computer Application / Computer Science (For ICSE/ISC/CBSE Board)
Add: 19,COD Colony Near Navdeep Hosiptal, Saket Road, Agra
Behind Kundkund Dharmshala, Opp. Collectorate Office, Agra
PEC Coaching centre, Sector- 4, Avas vikas Colony, Sikandra, Agra
Mob: 8273651685
Ankuram Education, Punchwati (Parasnath) Near Shankar Green, Fatehabad Road,
7017954184
1. A cloth showroom offers discounts in its regular stock clearance sale as per
follows:
Amount of purchase
Discount in %
Mill cloth
Handloom Item
Less than Rs 1000
2%
5%
Rs 1000 to Rs 5000
20%
25%
Above 5000
40%
50%
Write a program to accept the amount of purchase and type of purchase
where ‘M’ indicates Mill cloth and ‘H’ indicates Handloom item either
calculate discount and the discounted price for Mill cloth or Handloom item
depending on the type.
import java.util.*;
class customer
{
Public static void main()
{
Scanner sc=new Scanner(System.in);
double amt,dis=0.0,net=0.0;
System.out.println("Enter the amount");
amt=sc.nextDouble();
System.out.println("Enter your choice");
System.out.println("Choose M for mill cloth");
System.out.println("Choose H for handloom Item");
char ch=br.read().charAt();
switch(ch)
{
case 'M':
if(amt<1000)
{
dis=amt*2/100;
}
else
if(amt>=1000 && amt<=5000)
{
dis=amt*20/100;
}
else
{
dis=amt*40/100;
}
break;
case 'H':
if(amt<1000)
{
dis=amt*5/100;
}
else
if(amt>=1000 && amt<=5000)
{
dis=amt*25/100;
}
else
{
dis=amt*50/100;
}
break;
default:
System.out.println("Wrong choice");
}
net=amt-dis;
System.out.println("Discount price: = "+dis);
System.out.println("Net amount: ="+net);
}}
2. Display the output using nested loop
a.
123454321
1234 4321
123
321
12
21
1
1
a. class pat
{
void main()
{
int i,j,k,c=-1,x;
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
System.out.print(j);
}
for(k=1;k<=c;k++)
{
System.out.print(" ");
}
c=c+2;
if(i==5)
{
for(x=i-1;x>=1;x--)
{
System.out.print(x);
}
}
else
{
for(x=i;x>=1;x--)
{
b.
1
121
12321
1234321
123454321
System.out.print(x);
}
}
System.out.println();
}
}
}
b. class pattern1
{
void main()
{
int i,j,k,x,c=4;
for(i=1;i<=5;i++)
{
for(k=1;k<=c;k++)
{
System.out.print(" ");
}
c=c-1;
for(j=1;j<=i;j++)
{
System.out.print(j);
}
for(x=i-1;x>=1;x--)
{
System.out.print(x);
}
System.out.println();
}
}
}
3. Write a program to input a string and display the string in following
patterns according to user’s choice (1 or 2)
Example: Input: program
For choice 1:
p
pr
pro
prog
progr
progra
program
import java.util.*;
class format
{
void main()
{
String str;
int i,len,choice,k,c=6;
Scanner sc=new Scanner(System.in);
System.out.println("Enter any string");
str=sc.next();
len=str.length();
System.out.println("Ente your choice");
choice=sc.nextInt();
switch(choice)
{
case 1:
for(i=1;i<=len;i++)
{
System.out.println(str.substring(0,i));
}
break;
For choice 2:
m
am
ram
gram
ogram
rogram
program
case 2:
for(i=len-1;i>=0;i--)
{
for(k=1;k<=c;k++)
{
System.out.print(" ");
}
c--;
System.out.println(str.substring(i,len));
}
break;
default:
System.out.println("Wrong Choice");
}
}
}
4. Define overloaded method to compute the volume of a cube, cuboid, sphere
and cylinder.
a. Volume of a cube = side * side
b. Volume of cuboid = length * breadth * height
c. Volume of sphere = (4/3) * 3.14 * (radius)2
d. Volume of cylinder = 3.14 * (radius)2 * height
class compute
{
void compute(int side)
{
double vol_cube;
vol_cube=side*side;
System.out.println("Volume of cube = "+vol_cube);
}
void compute(int length,int breadth,int height)
{
double vol_cuboid;
vol_cuboid=length*breadth*height;
System.out.println("Volume of cuboid = "+vol_cuboid);
}
void compute(double radius)
{
double vol_sphere=(4/3)*3.14*radius*radius;
System.out.println("Volume of Sphere = "+vol_sphere);
}
void compute(double radius,double h)
{
double vol_cylinder=3.14*radius*radius*h;
System.out.println("Volume of cylinder = "+vol_cylinder);
}
}
5. Design a class happy to check if a given number is happy number.
A happy number is a number in which the eventual sum of the square of the
digits of the number is equal to 1.
Example: 28 = (2)2 + (8)2 = 4 + 64 = 68
68 = (6)2 + (8)2 = 36 + 64 =100
100 = (1)2 + (0)2 +(0)2 = 1 + 0 + 0 = 1
Hence, 28 is a happy number
import java.util.*;
class Happy
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int num,d;
System.out.println("Enter any number");
num=sc.nextInt();
while(num>9)
{
int sum=0;
while(num!=0)
{
d=num%10;
sum=sum+d;
num=num/10;
}
num=sum;
}
if(num==1)
System.out.println("Happy number");
else
System.out.println("Not");
}
}
6. Write a program that encodes a word into Piglatin. To translate a word into a
Piglatin word, convert the word into uppercase and then place the first vowel of
the original word as the start of the new word along with the remaining
alphabets. The alphabets present before the vowel being shifted towards the
end followed by “AY”.
Sample Input (1) London
(2) Olympics
Sample output (1) ONDONLAY
(2) OLYMPICSAY
import java.io.*;
class pigLatin
{
void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String word,rev="";
char ch;
int len,i;
System.out.println("Enter the word");
word=br.readLine();
word=word.toUpperCase();
len=word.length();
for(i=0;i<=len-1;i++)
{
ch=word.charAt(i);
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
{
rev=word.substring(i)+word.substring(0,i)+"AY";
break;
}
}
System.out.println("PigLatin Word="+rev);
}
}
7. WAP to input any string and print the string in the given format.
Input:
RAJ PRATAP SINGH
Output:
R.P. SINGH
import java.util.*;
class ShortName
{
void main()
{
Scanner sc=new Scanner(System.in);
String str;
int len,last,i;
char ch;
System.out.println("Enter the full name");
str=sc.nextLine();
str=str.trim();
len=str.length();
last=str.lastIndexOf(' ');
System.out.print(str.charAt(0)+".");
for(i=0;i<last;i++)
{
ch=str.charAt(i);
if(ch==' ')
{
System.out.print(str.charAt(i+1)+".");
}
}
System.out.print(str.substring(last+1));
}
8. WAP to input any string in uppercase and print the frequency of each
character.
Input: SCIENCE
Output: Character
Frequency
C
2
E
2
I
1
N
1
S
1
import java.util.*;
class freqchar
{
void main(String args[])
{
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
char ch;
int freq=0;
int len=str.length();
for(int i=0;i<len-1;i++)
{
ch=str.charAt(i);
for(int j=0;j<=len-1;i++)
{
if(ch==str.charAt(j))
{
freq++;
}
System.out.println(freq);
}
}
}
}
09. Write a program using a method Palin(), to check whether a string is a
Palindrome or not.
A Palindrome is a string that reads the same from left to right and vice versa.
Ex- MADAM, ARORA, MOM
import java.io.*;
class palindrome
{
void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str,rev="";
int len,i;
char ch;
System.out.println("Enter the word");
str=br.readLine();
len=str.length();
for(i=0;i<len;i++)
{
ch=str.charAt(i);
rev=rev+ch;
}
if(rev.equalsIgnoreCase(str))
{
System.out.println("Palindrome");
}
else
{
System.out.println("Not Palindrome");
}
}
}
10. Wap to calculate and print the value of the given series.
S= 1/X - 2/X2 +
3/X3 +
4/X4 +………… 10/X10
(value of X to be passed as parameter)
class series1
{
void main(int x)
{
int i,j,fact=1,sign=-1;
double sum=0.0;
for(i=1;i<=10;i++)
{
for(j=1;j<=i;j++)
{
fact=fact*j;
}
sign=sign*-1;
sum=sum+(i/Math.pow(x,i))*sign;
fact=1;
}
System.out.println("Sum="+sum);
}
}
11. Wap to calculate and print the value of the given series.
S= X/2! + X2/4! +
X3/6!
+
X4/8!
+………… X10/18!
(value of X to be passed as parameter)
class series
{
void main(int x)
{
int i,j,fact=1,sign=-1,k=2;
double sum=0.0;
for(i=1;i<=10;i++)
{
for(j=1;j<=k;j++)
{
fact=fact*j;
}
sum=sum+Math.pow(x,i)/fact;
fact=1;
k=k+2;
}
System.out.println("Sum="+sum);
}
}
12. Write a program to input and store the weight of 10 people. Sort and display
them in descending order using the selection sort technique.
import java.util.*;
class sorting
{
void main()
{
Scanner sc=new Scanner(System.in);
int a[]=new int[10];
int i,j,k,temp,small,pos;
System.out.println("Enter the array elements");
for(i=0;i<10;i++)
{
a[i]=sc.nextInt();
}
for(i=0;i<10;i++)
{
small=a[i];
pos=i;
for(j=i+1;j<10;j++)
{
if(small<a[j])
{
small=a[j];
pos=j;
}
}
temp=a[i];
a[i]=a[pos];
a[pos]=temp;
}
System.out.println("After Sorting Array Element are:");
for(k=0;k<10;k++)
{
System.out.print(a[k]+ " "); } } }
13. Write a program to perform binary search on a list of integer given below ,to
search for an element input by the user, if it is found display the element
along with its position, otherwise display the message “Search element not
found”.
5,7,11,15,20,30,45,89,97
import java.util.*;
class searching
{
void main()
{
int a[]={5,7,11,13,15,17,19,21,23,25};
int item,flag=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the element to be search");
item=sc.nextInt();
int low=0,high=a.length-1,mid;
while(low<high)
{
mid=(low+high)/2;
if(item>a[mid])
{
low=mid+1;
}
else
if(item<a[mid])
{
high=mid-1;
}
else
if(item==a[mid])
{
System.out.println(item+" Fount at "+(mid+1)+" Position ");
flag=1;
break;
}
}
if(flag==0)
{
System.out.println("Search element is not found");
}
}
}
14. Define a class and store the given city names in a single dimensional array.
Sort these names in alphabetical order using the Bubble Sort technique only.
INPUT : Delhi, Bangalore, Agra, Mumbai, Kolkata
OUTPUT : Agra, Bangalore, Delhi, Kolkata, Mumbai
class bubble
{
void main()
{
int i,j,k;
String temp;
String city[]={"Delhi","Bangalore","Agra","Mumbai","Kolkata"};
for(i=0;i<city.length-1;i++)
{
for(j=0;j<city.length-1-i;j++)
{
if(city[j].compareTo(city[j+1])>0)
{
temp=city[j];
city[j]=city[j+1];
city[j+1]=temp;
}
}
}
System.out.println("After sorting array elements are: ");
for(k=0;k<city.length;k++)
{
System.out.print(city[k]+ " ");
}
}
}
15. Write a program to initialize an array of 5 names and initialize another array
with their respective telephone numbers. Search for a name input by the user, in
the list. If found, display “Search Successful” and print the name along with the
telephone number, otherwise display “Search unsuccessful. Name not enlisted”.
(Using Linear Search)
import java.io.*;
class linear
{
void main()throws IOException
{
String name[]={"AMAN","KARAN","VARUN","KAPIL","SUMIT"};
int phno[]={9897,9411,9997,8273,9058};
int flag=0,i;
String item;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the name to be search");
item=br.readLine();
for(i=0;i<name.length;i++)
{
if(item.equalsIgnoreCase(name[i]))
{
System.out.println("Search successful");
System.out.println(item +" found at " +(i+1)+ " position");
flag=1;
break;
}
}
if(flag==0)
{
System.out.println("Search element is not found");
}
}
16. Use arrays in java and WAP to store rollno, name and marks of six subjects
for 10 students. Calculate the percentage marks obtained by each. Assume the
maximum marks in each subject as 100.
Calculate the grade as per the given criteria.
Percent
Grade
80 to 100
A
60 to 79
B
40 to 59
C
Less than 40
D
import java.io.*;
class Student
{
void main()throws IOException
{
int s[],avg,roll[],m1[],m2[],m3[],m4[],m5[],i,a=0,b=0,c=0,d=0;
String name[];
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
name=new String[10];
roll=new int[10];
m1=new int[10];
m2=new int[10];
m3=new int[10];
m4=new int[10];
m5=new int[10];
s=new int[10];
//avg=new int[10];
for(i=0;i<10;i++)
{
System.out.println("Enter the name");
name[i]=br.readLine();
System.out.println("Enter the Roll no");
roll[i]=Integer.parseInt(br.readLine());
System.out.println("Enter the marks of three subjects");
m1[i]=Integer.parseInt(br.readLine());
m2[i]=Integer.parseInt(br.readLine());
m3[i]=Integer.parseInt(br.readLine());
m4[i]=Integer.parseInt(br.readLine());
m5[i]=Integer.parseInt(br.readLine());
s[i]=m1[i]+m2[i]+m3[i]+m4[i]+m5[i];
}
for(i=0;i<10;i++)
{
avg=s[i]/5;
if(avg>=80 && avg<=100)
{
a++;
}
else
if(avg>=60 && avg<=79)
{
b++;
}
else
if(avg>=40 && avg<=59)
{
c++;
}
else
{
d++;
}
}
System.out.println("Total no of grade A="+a);
System.out.println("Total no of grade B="+b);
System.out.println("Total no of grade C="+c);
System.out.println("Total no of grade D="+d);
}
}
17. Define a class Fibbonacci to generate and print first n terms of the fibbonacci
series
Fibbonacci series is 0, 1, 1, 2, 3, 5, 8,13………………
Each term is the sum of previous 2 terms.
import java.util.*;
class Fibbonacci
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int first=0,second=1,third,n;
System.out.println(“Enter the number of terms”);
n=sc.nextInt();
System.out.println("Fibbonacci series are: ");
System.out.print(first+" "+second+" ");
for(i=1;i<=n-2;i++)
{
third=first+second;
System.out.print(third+" ");
first=second;
second=third;
}
}
}
18. Specify a class TelCall which calculates monthly phone bill of a consumer.
The class description is given below:
Instance Variables :
phno
phone number
name
name of the consumer
n
number of calls made
amt
bill amount
Methods :
Parameterized constructor
void compute( )
to calculate the phone bill amount based on the slabs
given below
void dispdata( )
to display the details
Number of calls
1 -100
101 – 200
201 – 300
Above 300
Rate
Rs. 500 rental charge only
Rs. 1.00/-per call + rental charge
Rs. 1.20/-per call + rental charge
Rs. 1.50/-per call + rental charge
In the main method, create an object of type TelCall and display the phone
bill for a consumer who makes 625 calls in month.
class TellCall
{
int n;
long phno;
String name;
double amt;
public TellCall(long ph,int calls,String nm)
{
phno=ph;
n=calls;
name=nm;
amt=0.0;
}
void compute()
{
if(n<=100)
{
amt=500;
}
else
if(n>100 && n<=200)
{
amt=500+ (n-100)*1;
}
else
if(n>200 && n<=300)
{
amt=500+100*1+(n-200)*1.2;
}
else
{
amt=500+100*1+100*1.2+(n-300)*1.5;
}
}
void dispdata()
{
System.out.println("Phone Number is "+phno);
System.out.println("Name is "+name);
System.out.println(" Number of Calls "+n);
System.out.println("Telephone bill "+amt);
}
public static void main()
{
TellCall obj=new TellCall(9897,625,"XYZ");
obj.compute();
obj.dispdata();
}}
19. Using a switch statement, Write a menu driven program to convert the
temperature form Fahrenheit to Celsius and vice versa. For an incorrect choice
an appropriate error message should be displayed.
(HINT : C = 5/9 * (F – 32) and F = 1.8 * ( C + 32 )
import java.util.*;
class convert
{
int F,C;
double Fa=0.0,Ca=0.0;
void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter your choice");
System.out.println("1: for Fahrenheit to Celsius");
System.out.println("2: for Celsius to Fahrenheit");
int choice=sc.nextInt();
switch(choice)
{
case 1:
System.out.println("Enter the Temperature in Fahrenheit ");
F=sc.nextInt();
Ca=5*(F-32)/9;
System.out.println("Temperature in celsius: = "+Ca);
break;
case 2:
System.out.println("Enter the temperature in Celsius");
C=sc.nextInt();
Fa=1.8*(C+32);
System.out.println("Temperature in Fahrenheit : ="+Fa);
break;
default:
System.out.println("Wrong choice");
}
}}
20. Write a menu driven program to accept a number from the user and check
whether it is prime number Or a perfect number.
import java.io.*;
class Number
{
int n,i,ctr=0,sum=0,j;
void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter your choice");
System.out.println("1: for Check Prime No.");
System.out.println("2: for Check Perfect No.");
int choice=Integer.parseInt(br.readLine());
switch(choice)
{
case 1:
for(i=1;i<=n;i++)
{
if(n%i==0)
{
ctr++;
}
}
if(ctr==2)
{
System.out.println(n+" is prime");
}
else
{
System.out.println(n+" is not prime");
}
break;
case 2:
for(j=1;j<n;j++)
{
if(n%j==0)
{
sum=sum+j;
}
}
if(sum==n)
{
System.out.println(n+ "is Perfect No.");
}
else
{
System.out.println(n+ "is not perfect No.");
}
break;
default:
System.out.println("Wrong choice");
}
}
}
21. Write a Program in Java to input a number and check whether it is a Disarium
Number or not.
Note: A number will be called DISARIUM if sum of its digits powered with
their respective position is equal to the original number.
For example 135 is a DISARIUM
(Workings 11+32+53 = 135, some other DISARIUM are 89, 175, 518 etc)
import java.util.*;
class Disarium
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int num,d,sum=0,num2,num1,ctr=0;
System.out.println("Enter any number");
num=sc.nextInt();
num2=num1=num;
while(num2!=0)
{
ctr++;
num2=num2/10;
}
while(num1!=0)
{
d=num1%10;
sum=sum+(int)Math.pow(d,ctr);
num1=num1/10;
ctr--;
}
if(sum==num)
System.out.println(num+" is Disarium ");
else
System.out.println(num+" is not Disarium ");
}
}
22. Design a program which inputs a date in eight digit number format i.e.
14121999. Test the validity of the date and print the date in full from. If date is
invalid then print a message as “Invalid date”.
Example :
1. Input :
1412199
Output : 14 December, 1999
Valid date
import java.io.*;
class Date_DDMMYY
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int l, y, d, m;
String dd, mm, yy;
int maxdays[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
String month[]={ "", "January", "February", "March", "April", "May", "June",
"July", "August","September", "October", "November", "December" };
System.out.print("Enter any date in 8 digits (ddmmyyyy) format: ");
String date = br.readLine();
l = date.length();
if(l==8)
{
dd = date.substring(0,2);
mm = date.substring(2,4);
yy = date.substring(4);
d = Integer.parseInt(dd);
m = Integer.parseInt(mm);
y = Integer.parseInt(yy);
if((y%400==0) || ((y%100!=0)&&(y%4==0)))
{
maxdays[2]=29;
}
if(m<0 || m>12 || d<0 || d>maxdays[m] || y<0 || y>9999)
{
System.out.println("The day, month or year are outside acceptable limit");
}
else
{
System.out.println(dd+" "+month[m]+", "+yy);
System.out.println("Valid Date");
}
}
else
System.out.println("Invalid Date");
}
}
23. Write a Java program to check whether two strings are anagram or not?
Two strings are called anagrams if they contain same set of characters but in
different order.
"keep ? peek", "Mother In Law - Hitler Woman".
import java.util.Scanner;
public class Anagram
{
public static void main(String[] input)
{
String str1, str2;
int len, len1, len2, i, j, found=0, not_found=0;
Scanner scan = new Scanner(System.in);
System.out.print("Enter First String : ");
str1 = scan.nextLine();
System.out.print("Enter Second String : ");
str2 = scan.nextLine();
len1 = str1.length();
len2 = str2.length();
if(len1 == len2)
{
len = len1;
for(i=0; i<len; i++)
{
found = 0;
for(j=0; j<len; j++)
{
if(str1.charAt(i) == str2.charAt(j))
{
found = 1;
break;
}
}
if(found == 0)
{
not_found = 1;
break;
}
}
if(not_found == 1)
{
System.out.print("Strings are not Anagram to Each Other..!!");
}
else
{
System.out.print("Strings are Anagram");
}
}
else
{
System.out.print("Both Strings Must have the same number of Character to
be an Anagram");
}
}
}
24. Write a Program in Java to input a number and check whether it is a Unique
Number or not.
Note: A Unique number is a positive integer (without leading zeros) with no
duplicate digits. For example 7, 135, 214 are all unique numbers whereas 33, 3121,
300 are not.
class unique
{
public static void main(int n)
{
int flag=0,n1,d;
for(int i=0;i<=9;i++)
{
int f=0;
n1=n;
while(n1!=0)
{
d=n1%10;
if(d==i)
f++;
n1=n1/10;
}
if(f>0&&f!=1)
flag=1;
}
if(flag==0)
System.out.println("Unique number");
else
System.out.println("Not");
}
}
25. Write a program to declare a square matrix A[ ] [ ] of order (M x M) where
‘M’ is the number of rows and the number of columns. Display appropriate error
message for an invalid input. Perform the following tasks:
(a) Create a mirror image matrix.
(b) Display the mirror image matrix.
Example 1
INPUT : M = 3
4
16 12
8
2
14
4
1
3
MIRROR IMAGE MATRIX
12
14
3
16
2
1
4
8
6
import java.util.*;
class MirrorImage
{
public static void main(String args[])throws IOException
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the size of the square matrix : ");
int m=sc.nextInt();
int A[][]=new int[m][m];
System.out.println("Enter the elements of the Matrix : ");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
A[i][j]=Integer.parseInt(br.readLine());
}
}
// creating the Image Matrix
int B[][]=new int[m][m];
for(int i=0;i<m;i++)
{
int k=0;
for(int j=m-1;j>=0;j--)
{
B[i][k]=A[i][j];
k++;
}
}
System.out.println("The Mirror Image:");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(B[i][j]+"t");
}
System.out.println();
}
}
}
ANKURAM EDUCATION / PROGRAMMERS POINT
Computer Application / Computer Science (For ICSE/ISC/CBSE Board)
Add: 19,COD Colony Near Navdeep Hosiptal, Saket Road, Agra
Behind Kundkund Dharmshala, Opp. Collectorate Office, Agra
PEC Coaching centre, Sector- 4, Avas vikas Colony, Sikandra, Agra
Mob: 8273651685
Ankuram Education, Punchwati (Parasnath) Near Shankar Green, Fatehabad Road,
7017954184
26. Write a Menu driven program to find the area of an Equilateral triangle. an
Isosceles triangle and a Scalene triangle as per the User‟s choice.
1. Equilateral triangle = √
* S2 , where s= side of an equilateral
triangle.
2. Isosceles triangle = ¼ * b* 4a2 – b2
3. Scalene triangle = s * (s-m) * (s-n) * (s-p), where s=(m+n+P)/2
Where m,n and p are three sides of triangle.
import java.util.*;
class p26
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int s,a,b,m,n,p,choice;
double area,area1,area2,s1;
System.out.println("Enter your choice");
System.out.println("1. Area of Equilateral triangle");
System.out.println("2. Area of Isosceles triangle");
System.out.println("3. Area of Scalene triangle");
choice=sc.nextInt();
switch(choice)
{
case 1:
System.out.println("Enter side of an Equilateral triangle");
s=sc.nextInt();
area=(Math.sqrt(3)*s*s)/4.0;
System.out.println("Area of triangle="+area);
break;
case 2:
System.out.println("Enter side and base of Isosceles triangle");
a=sc.nextInt();
b=sc.nextInt();
area1=1/4*b*Math.sqrt(4*a*a-b*b);
System.out.println("Area of triangle="+area1);
break;
case 3:
System.out.println("Enter the sides of scalene triangle");
m=sc.nextInt();
n=sc.nextInt();
p=sc.nextInt();
s1=(m+n+p)/2.0;
area2=Math.sqrt(s1*(s1-m)*(s1-n)*(s1-p));
System.out.println("Area of triangle="+area2);
break;
default:
System.out.println("Wrong choice");
}
}
}
27. Write a program to accept a number and check whether the number is perfect
or not. A number is said to be perfect if the sum of the factors(including 1 and
excluding the number itself) is the same as the original number)
import java.util.*;
class p27
{
void main()
{
Scanner sc=new Scanner(System.in);
int n,sum=0;
System.out.println("Enter any number");
n=sc.nextInt();
for(int i=1;i<n;i++)
{
if(n%i==0)
sum=sum+i;
}
if(sum==n)
System.out.println("Perfext number");
else
System.out.println("not");
}
}
28.WAP to accept a two numbers and find the Greatest common Divisor(G.C.D)
of those numbers.
Sample input : 25, 45
The greatest Divisor : 5
import java.util.*;
class p28
{
void main()
{
Scanner sc=new Scanner(System.in);
int n1,n2,sum=0,gcd=0,n;
System.out.println("Enter any two number");
n1=sc.nextInt();
n2=sc.nextInt();
n=Math.min(n1,n2);
for(int i=1;i<n;i++)
{
if(n1%i==0 && n2%i==0)
gcd=i;
}
System.out.println("Greatest common Divisor :"+gcd);
}
}
29. Create a class Employee as given:
Class name
:
Employee
Data members/Variable : name (String)
basic ,DA,HRA,Total,pf,netsalary(double type)
Member functions/methods :
(i) Employee( )
:
default constructor to initialize data members.
(ii) void accept( )
:
accept the name and basic salary of employee.
(iii) void calculation ( ) :
to calculate allowance and salaries as per the
given criteria.
Da = 45% of basic
HRA = 15% of basic
pf=8.33% of basic
Total=basic+DA+HRA
Netsalary=Total – pf
(iv) void print( ) : to display the name and other data members with
messages.
Write a main function to create an object of the class and print the details the
employee by invoking the methods.
import java.util.*;
class Employee
{
String name;
//string variable to store name of employee
double basic,DA,HRA,Total,pf,netsalary;
//variable for other calculation
//Default constructor to initialize the data members
Employee( )
{
name="";
basic=0.0;
DA=0.0;
HRA=0.0;
Total=0.0;
pf=0.0;
netsalary=0.0;
}
//Function definition for input the name and basic salary of employee
void accept()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the name and basic salry");
name=sc.nextLine();
basic=sc.nextDouble();
}
//Function definition for calculation of other allowances
void calculation()
{
DA=(basic*45.0)/100.0;
HRA=(basic*15.0)/100.0;
pf=(basic*8.33)/100.0;
Total = basic+DA+HRA;
netsalary=Total-pf;
}
//Function definition for display the details of employee
void print()
{
System.out.println("Name of Employee "+name);
System.out.println("Basic salary of Employee "+basic);
System.out.println("Net Salary of Employee "+netsalary);
}
public static void main(String args[])
{
Employee obj=new Employee(); //create the object of class
obj.accept();
//invoke accept function
obj.calculation();
//invoke calculation function
obj.print();
//invoke print function
} //end of the main() function
}//end of the class
(Variable description table)
Variable name
Data type
Description/Purpose
name
String
To store name of employee
DA
double
To find and store Dearness allowance
HRA
double
To find and store House rent allowance
pf
double
To find and store provident fund
Total
double
To find and store gross salary
Netsalary
double
To find and store net salary
30. Write a program to input a sentence and print longest word and smallest word
of the given sentence.
INPUT : Str = “ Honesty is the best policy”
Output: The longest word : Honesty
The smallest word : is
class StringDemo
{
public static void main(String str)
{
String Word="",Sword=str,Lword="";
int len;
char ch;
str=str.trim();
str=str+" ";
len=str.length();
for(int i=0;i<len;i++)
{
ch=str.charAt(i);
if(ch!=' ')
Word=Word+ch;
else
{
if(Word.length()>Lword.length())
Lword=Word;
else if(Word.length()<Sword.length())
Sword=Word;
Word="";
}
}
System.out.println("Longest word"+Lword);
System.out.println("Smallest word"+Sword);
}
}
31. Using a java program, create a 3x3 matrix and store the first nine natural
numbers in it row-wise.
Now write a program to print the following output:
(a)
Sum of the numbers in the left diagonal
(b)
Sum of the numbers in the right diagonal
1
2
3
4
5
6
class p31
{
public static void main(int A[][])
{
int Lsum=0,Rsum=0;
7
8
9
//Find the sum of left diagonal
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(i==j)
Lsum=Lsum+A[i][j];
}
}
//Find the sum of right diagonal
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if((i+j)==2)
Rsum=Rsum+A[i][j];
}
}
System.out.println("Sum of left diagonal ="+Lsum);
System.out.println("Sum of Right diagonal ="+Rsum);
}
}
32. WAP to create a Double Dimensional array as
char ch[] = new ch[3][3]
The program stores nine different letters in the double dimensional array.
Display the letters of the left diagonal.
Sample output : S,H,T
class P32
{
public static void main(int A[][])
{
//Display the left diagonal elements
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(i==j)
System.out.print(A[i][j]);
else
System.out.print(" ");
}
System.out.println();
}
}
}
S
P
K
A
H
L
H
O
T
33. Design a class Area to find the area of following geometrical figures to
illustrate function overloading and invoke them.
 Area of circle = 2
 Area of square = a2
 Area of rectangle = length * breadth
class overloading
{
double calculate(double r)
{
double area=3.14*r*r;
return area;
}
double calculate(int a)
{
double area=a*a;
return area;
}
double calculate(int length,int breadth)
{
double area;
area=length*breadth;
return area;
}
public static void main()
{
overloading obj=new overloading();
double P,Q,R;
P=obj.calculate(3.4);
Q=obj.calculate(3);
R=obj.calculate(4,5);
System.out.println("Area of circle"+P);
System.out.println("Area of square"+Q);
System.out.println("Area of rectangle"+R);
}
}
34. WAP to enter a no and check whether it is a Special no or not.
(It is a no which is equivalent to the sum of factorial of its digit)
Ex. 145 = 1! + 4! + 5!
1 + 24 + 120 = 145
import java.util.*;
class p34
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int num,d,sum=0,num1;
System.out.println("Enter any number");
num=sc.nextInt();
num1=num;
while(num1!=0)
{
d=num1%10;
int fact=1;
for(int i=1;i<=d;i++)
{
fact=fact*i;
}
sum=sum+fact;
num1=num1/10;
}
if(sum==num)
System.out.println(num+" is Speical number ");
else
System.out.println(num+" is not Special number ");
}
}
35.WAP to enter two numbers and check whether they are Amicable no or Not.
(If two numbers are such that the sum of the perfect divisors of one number
is equal to the other number and the sum of the perfect divisors of the other
number is equal to the first number, then the number is called Amicable
Numbers)
class p35
{
public static void main(int A,int B)
{
int sum1=0,sum2=0;
for(int i=1;i<A;i++)
{
if(A%i==0)
sum1=sum1+i;
}
for(int j=1;j<B;j++)
{
if(B%j==0)
sum2=sum2+j;
}
if(A==sum2 && B==sum1)
System.out.println("They are Amicable number");
else
System.out.println("They are not Amicable number");
}
}
ANKURAM EDUCATION/PROGRAMMERS POINT
Computer Application / Computer Science (For ICSE/ISC/CBSE Board)
Add: 19,COD Colony Near Navdeep Hosiptal, Saket Road, Agra
Behind Kundkund Dharmshala, Opp. Collectorate Office, Agra
PEC Coaching centre, Sector- 4, Avas vikas Colony, Sikandra, Agra
Mob: 8273651685
Ankuram Education, Punchwati (Parasnath) Near Shankar Green, Fatehabad Road,
7017954184
36. Write a menu driven program to calculate the sum of following series:
1 + 2 + 1 + 2 + 3 + 1 + 2 + 3 + 4 ……n
1*2
1*2*3
1 * 2 * 3 * 3 …….n
class p36
{
public static void main(int n)
{
double sum=0.0;
for(int i=1;i<=n;i++)
{
int p=1,s=0;
for(int j=1;j<=i;j++)
{
s=s+j;
p=p*j;
}
sum=s/p;
}
System.out.println("Sum = "+sum);
}
}
37. Define a class Cabservice having the following specifications:
class name
: Cabservice
Instance variable/data members :
String taxino : to store the taxi number
String name
: to store name of the passenger
int d
: to store the distance travelled (in km)
Methods/Member functions:
Cabservice
: default constructor to initialize the data members
Input( )
: to store taxino, name, d
Calculate( )
: to calculate the bill for a customer as per the tariff given
below:
Distance Travelled (km)
Rate/km
Up to 1 km
Rs. 25
More than 1 km and upto 5 km Rs. 30
More than 5 km and upto 10 km Rs. 35
More than 10 km and upto 20
Rs. 40
km
More than 20 km
Rs. 45
Display( )
: to display the details in the following format:
Taxino
Name
Distance (km)
Bill amount
*****
****
****
****
Write a main method to create an object of a class and call the above
member methods.
import java.util.*;
class Cabservice
{
String taxino,name;
int d,amt;
Cabservice()
{
taxino="";
name="";
d=0;
amt=0;
}
void Input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter taxi number");
taxino=sc.nextLine();
System.out.println("Enter the name");
name=sc.nextLine();
System.out.println("Enter the distance travelled");
d=sc.nextInt();
}
void Calculate()
{
if(d==1)
amt=d*25;
else if(d>1 && d<=5)
amt=d*30;
else if(d>5 && d<=10)
amt=d*35;
else if(d>10 && d<=20)
amt=d*40;
else
amt=d*45;
}
void Display()
{
System.out.println("Taxi no.\t Name \t Distance(km)\t Bill amount");
System.out.println(taxino+"\t"+name+"\t"+d+"\t"+amt);
}
public static void main()
{
Cabservice obj=new Cabservice();
obj.Input();
obj.Calculate();
obj.Display();
}
}
38. Write a program to accept a string. Convert the string to uppercase. Count
and print the numbers of double letter sequences that exists in the string.
Ex. INPUT : “SHE WAS FEEDING THE LITTLE RABBIT WITH AN APPLE”
OUTPUT: 4
class p38
{
public static void main(String str)
{
int ctr=0,len;
char ch1,ch2;
str=str.toUpperCase();
len=str.length();
for(int i=0;i<len-1;i++)
{
ch1=str.charAt(i);
ch2=str.charAt(i+1);
if(ch1==ch2)
{
System.out.println(ch1+","+ch2+" is a duplicate sequence");
ctr++;
}
}
System.out.println("No. of double letter sequences : "+ctr);
}
}
39. Write a program to initialize 5 real number in an array. Calculate and print
the sum of integer part and product of decimal part.
class p39
{
public static void main()
{
double num[]={3.4,5.7,7.0,6.9,8.5};
int sum=0,I;
double pro=1.0,D;
for(int i=0;i<num.length;i++)
{
if(num[i]>0)
{
I=(int)Math.floor(num[i]);
D=num[i]-I;
sum=sum+I;
pro=pro*D;
}
else
{
I=(int)Math.ceil(num[i]);
D=num[i]-I;
sum=sum+I;
pro=pro*D;
}
}
System.out.println("Sum of Integer part : "+sum);
System.out.println("Product of Decimal part : "+pro);
}
}
40. Write a program to input any number with base 10, and convert it into its
binary equivalent.
Ex. Input any number : 6
Binary equivalent : 110
class p40
{
public static void main(int n)
{
int sum=0,ctr=0,d;
while(n!=0)
{
d=n%2;
sum=sum+(int)Math.pow(10,ctr)*d;
n=n/2;
ctr++;
}
System.out.println("Binary equivalent : "+sum);
}
}
41. Write a program in java to accept a Binary number(base 2) and convert it into
its Decimal equivalent(base 10).
class p41
{
public static void main(int n)
{
int c=0,s=0,d;
while(n!=0)
{
d=n%10;
s=s+(int)Math.pow(2,c)*d;
n=n/10;
c=c+1;
}
System.out.println("The decimal equivalent : "+s);
}
}
42. WAP to store 5 different names along with corresponding Telephone
numbers. Enter a name from the console and search whether the name is
present or not. If the name is present then display the name along with the
phone number otherwise, display an appropriate message using the „Linear
Search‟.
import java.util.*;
class p42
{
public static void main()
{
String name[]={"aman","karan","varun","tarun","vijay"};
int phno[]={9897,7878,9997,9152,8867};
String search_name;
int flag=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the name to be searched");
search_name=sc.nextLine();
for(int i=0;i<name.length;i++)
{
if(name[i].equalsIgnoreCase(search_name))
{
flag=1;
System.out.println(name[i]+"\t"+phno[i]);
break;
}
}
if(flag==0)
System.out.println("Sorry not found");
}
}
43. Write a program to accept a String in lowercase and replace „e‟ with „*‟ in the
given string. Display the new string.
class p43
{
public static void main(String str)
{
str=str.toLowerCase();
int len;
char ch;
len=str.length();
for(int i=0;i<len;i++)
{
ch=str.charAt(i);
if(ch=='e')
ch='*';
System.out.print(ch);
}
}
}
44. Write a program that output the result of the following evaluations based on
the number entered by the user.
(i) Natural logarithm of the number
(ii) Absolute value of the number
(iii) Square root of the number
class p44
{
public static void main(int n)
{
System.out.println("Natural logarithm of the number "+Math.log(n));
System.out.println("Absolute of the number "+Math.abs(n));
System.out.println("Square root of the number "+Math.sqrt(n));
}
}
45. Write a program to accept a string and display:
i. The number of lower case characters
ii. The number of upper case characters
iii. The number of special characters
iv. the number of digits present in the string.
class p45
{
public static void main(String str)
{
int lc=0,up=0,dg=0,sp=0;
char ch;
for(int i=0;i<str.length();i++)
{
ch=str.charAt(i);
if(ch>='a' && ch<='A')
lc++;
else if(ch>='A' && ch<='Z')
up++;
else if(ch>='0' && ch<='9')
dg++;
else
sp++;
}
System.out.println("No. of lowercase : "+lc);
System.out.println("No. of uppercase : "+up);
System.out.println("No. of digits : "+dg);
System.out.println("No. of special characters : "+sp);
}
}
46. WAP to input a sentence. Create a new sentence by replacing each consonant
with the previous letter. If the previous letter is a vowel then replace it with the
next letter.(i.e. : if the letter is B then replace it with C as the previous letter is A)
and the other characters remain same.
Input: THE CAPITAL OF INDIA
Output: SGE BAQISAK OG IMCIA
class p46
{
public static void main(String str)
{
int len;
char ch,chr;
str=str.toUpperCase();
String word="";
len=str.length();
for(int i=0;i<len;i++)
{
ch=str.charAt(i);
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
word=word+ch;
if((ch!='A')&&(ch!='E')&&(ch!='I')&&(ch!='O')&&(ch!='U'))
{
chr=(char)(((int)ch)-1);
if(chr=='A'||chr=='E'||chr=='I'||chr=='O'||chr=='U')
{
chr=(char)(((int)ch)+1);
}
word=word+chr;
}
if(ch==' ')
word=word+" ";
}
System.out.println(word);
}
}
47. Write a program to accept a word in lower case and display the new word
after removing all the repeated letters.
Sample Input : applications
Sample Output : aplictons
class p47
{
public static void main(String str)
{
String s="";
char ch1,ch2;
for(int i=0;i<str.length();i++)
{
ch1=str.charAt(i);
int flag=0;
for(int j=0;j<s.length();j++)
{
ch2=s.charAt(j);
if(ch1==ch2)
flag=1;
}
if(flag==0)
s=s+ch1;
}
System.out.println("The new string after removing duplicate letters :"+s);
}
}
48. The basic salary of employees is undergoing a revision . Define a class called
Grade_Revision with the following specifications:
Instance variable/ data members:
String name
: to store name of the employee
int basic
: to store basic salary
int expn
: to consider the length of service as an experience
double inc
: to store increment
double nbas
: to store new basic salary (basic+ increment)
Methods:
Grade_Revision ( ) : default constructor to initialize all data members
void accept( )
: to input name , basic and experience
void increment( )
: to calculate increment with the following specifications:
Experience
Increment
Less than 3 years
Rs. 1000 + 10% of basic
3 years or more but less than 5 years
Rs. 3,000 + 12% of basic
5 years or more but less than 10 years Rs. 5,000 + 15% of basic
10 years or more
Rs. 8,000 + 20% of basic
void display( )
: to print all the details of an employee.
Write the main method to create an object of a class and call all the above
member methods.
import java.util.*;
class Grade_Revision
{
String name;
int basic,expn;
double inc,nbas;
Grade_Revision()
{
name="";
basic=0;
expn=0;
inc=0.0;
nbas=0.0;
}
void accept()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the name of employee");
name=sc.nextLine();
System.out.println("Enter the basic salary");
basic=sc.nextInt();
System.out.println("Enter the experience");
expn=sc.nextInt();
}
void increment()
{
if(expn<3)
inc=1000+(basic*3.0)/100;
else if(expn>=3 && expn<5)
inc=3000+(basic*12.0)/100;
else if(expn>=5 && expn<10)
inc=5000+(basic*15.0)/100;
else
inc=8000+(basic*20.0)/100;
}
void display()
{
nbas=basic+inc;
System.out.println("Name="+name);
System.out.println("Basic salary"+basic);
System.out.println("Net salary ="+nbas);
}
public static void main()
{
Grade_Revision obj=new Grade_Revision();
obj.accept();
obj.increment();
obj.display();
}
}
49. Write a program in java to create 3*3 Square matrix and store numbers in it.
The programmer wants to check whether the matrix is Symmetric or not.
(A square matrix is said to be Symmetric, if the element of the ith row and
jth column is equal to the element of the jth row and ith column.)
A Symmetric matrix:
1
2
3
2
4
5
3
5
6
class p49
{
public static void main(int A[][])
{
int flag = 0;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(A[i][j] != A[j][i])
{
flag = 1;
break;
}
}
}
if(flag == 1)
System.out.println("The given Matrix is Not Symmetric");
else
System.out.println("The given Matrix is Symmetric");
}
}
50. Write a program in java to create 3*3 Square matrix and convert it into
transpose matrix.
Input:
1
2
3
1
4
7
4
5
6
2
5
8
7
8
9
3
6
8
Output:
class p50
{
public static void main(int A[][])
{
int B[][]=new int[3][3];
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
B[i][j] = A[j][i];
}
}
System.out.println("Transpose matrix are :");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(B[i][j]);
}
System.out.println();
}
}
}
Download