Comp Project Class 9
Comp Project Class 9
2|Page
3|Page
Question 1
1.Program to reverse a number and print the number.
public class q1
{
public static void main(int n)
{
int rem,q,rvno=0;
q=n;
if(n>=1000 && n<10000)
{
while(q!=0)
{
rem=q%10;
rvno=rvno*10+rem;
q=q/10;
}
System.out.println("The reverse of "+n+"="+rvno);
}
else
System.out.println("Enter only a four digit number");
}
}
If n=1324 Output:
4|Page
The reverse of 1324=4231
5|Page
Question 2
2. Program to check if inputted letter is a vowel or not using
switch case statement.
import java.util.*;
public class q2
{
public static void main()
{
Scanner abc=new Scanner (System.in);
System.out.println("Enter your letter");
char A=abc.next().charAt(0);
switch (A)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'U':
case 'u':System.out.println(A+" is a vowel");
break;
default:System.out.println(A+" is a consonant");
break;
}
6|Page
}}
Input: Output:
A=i Enter your letter
i
i is a vowel
A=n Enter your letter
n
n is a consonant
7|Page
Question 3
3.Program to calculate addition,subtraction,multiplication and
division using switch case.
import java.util.*;
public class q3
{
public static void main()
{
Scanner abc=new Scanner (System.in);
long ans,n1,n2;
System.out.println("Enter number 1");
n1=abc.nextLong();
System.out.println("Enter number 2");
n2=abc.nextLong();
System.out.println("Enter operator");
System.out.println("Enter + for addition");
System.out.println("Enter - for subtraction");
System.out.println("Enter * for multiplication");
System.out.println("Enter / for division");
System.out.println("");
char op;
op=abc.next().charAt(0);
switch(op)
{
case '+':ans=n1+n2;
System.out.println(+ans);
break;
8|Page
case'-':ans=n1-n2;
System.out.println(+ans);
break;
case'*':ans=n1*n2;
System.out.println(+ans);
break;
case'/':ans=n1/n2;
System.out.println(+ans);
break;
default:System.out.println("invalid");
}
}
}
Input: Output:
Number 1=10 Enter number 1
Number 2=2 10
Operator=/ Enter number 2
2
Enter operator
Enter + for addition
Enter - for subtraction
Enter * for multiplication
Enter / for division
/
5
9|Page
Input: Output:
Number 1=10 Enter number 1
Number 2=2 10
Operator=+ Enter number 2
2
Enter operator
Enter + for addition
Enter - for subtraction
Enter * for multiplication
Enter / for division
+
12
Input: Output:
Number 1=10 Enter number 1
Number 2=2 10
Operator=- Enter number 2
2
Enter operator
Enter + for addition
Enter - for subtraction
Enter * for multiplication
Enter / for division
-
8
10 | P a g e
Question 4
4. Program to input six subject’s marks and to total and
calculate the average using for—loop.
import java.util.*;
public class q4
{
public static void main()
{
Scanner abc=new Scanner(System.in);
int sum=0;
System.out.println("Enter 6 subjects marks");
int marks;
for(int i=6;i>=1;i--)
{
marks=abc.nextInt();
sum=sum+marks;
}
int avg=sum/6;
System.out.println("The sum of 6 subjects is="+sum);
System.out.println("The average is="+avg);
}
}
11 | P a g e
Input: Output:
Marks= Enter 6 subjects marks
35 35
24 24
43 43
23 23
33 33
32 32
The sum of 6 subjects is=190
The average is=31
12 | P a g e
Question 5
5.Program to input four digit year and to check if it is a leap
year or not using if else ladder statements.
import java.util.*;
public class q5
{
public static void main()
{
Scanner abc=new Scanner(System.in);
int n;
n=abc.nextInt();
if(n<=999)
{
System.out.println("Enter only a four digit year");
}
else if(n%4==0)
System.out.println(n+" "+"This year is a leap year");
else
System.out.println(n+" " +"This year is not a leap year");
}
}
Input: Output:
n=2018 2018
2018 This year is not a leap
year
Input: Output:
13 | P a g e
n=2020 2020
2020 This year is a leap year
14 | P a g e
Question 6
6.Program to input a day number and using if else ladder
statements to print day name.
import java.util.*;
public class q6
{
public static void main()
{
Scanner abc=new Scanner (System.in);
int n=abc.nextInt();
if(n==1)
System.out.println("Monday");
else if(n==2)
System.out.println("Tuesday");
else if(n==3)
System.out.println("Wednesday");
else if(n==4)
System.out.println("Thursday");
else if(n==5)
System.out.println("Friday");
else if(n==6)
System.out.println("Saturday");
else if(n==7)
System.out.println("Sunday");
else
System.out.println("invalid");
}
15 | P a g e
}
Input: Output:
n=3 3
Wednesday
Input: Output:
n=7 7
Sunday
Input: Output:
n=32 32
invalid
16 | P a g e
Question 7
7. Program to check if a number is a prime number or not.
public class q7
{
public static void main(int n)
{
int a=0;
for(int i=2;i<n;i++)
{
if(n%i==0)
a=1;
}
if(a==0)
System.out.println(n+" is a prime number");
else
System.out.println(n+" is not a not prime number");
}
}
17 | P a g e
Input: Output:
7 is a prime number
Input: Output:
10 is not a not prime number
18 | P a g e
Question 8
8.Program to check if a number is a perfect number or not.
public class q8
{
public static void main(int n)
{
int sum=0,q;
for(q=1;q<n;q++)
{
if(n%q==0)
sum=sum+q;
}
if(sum==n)
System.out.println(n+" is a perfect number");
else
System.out.println(n+" is not a perfect number");
}
}
19 | P a g e
Input: Output:
121 is not a perfect number
Input: Output:
6 is a perfect number
20 | P a g e
Question 9
9.Program to check if a number is a palindrome number or not.
public class q9
{
public static void main(int n)
{
int a,rem,revno=0;
a=n;
while(n!=0)
{
rem=n%10;
revno=revno*10+rem;
n=n/10;
}
if(a==revno)
System.out.println(a+" is a palindrome number");
else
System.out.println(a+" is not palindrome number");
}
}
21 | P a g e
Input: Output:
121 is a palindrome number
Input: Output:
329 is not palindrome number
22 | P a g e
Question 10
9.Program to print divisors of a number.
public class q10
{
public static void main(int n)
{
int i,o;
for(i=1;i<=n;i++)
{
if(n%i==0)
System.out.println(+i);
}
}
}
Input: Output:
The factors of 46 =
1
2
23
46
23 | P a g e
Question 11
11.Program to print 1 to 10 with their squares.
public class q11
{
public static void main()
{
int s;
for(int i=1;i<=10;i++)
{
System.out.print(i+"/");
s=i*i;
System.out.println(""+s);
}
}
}
Output:
1/1
2/4
3/9
4/16
5/25
6/36
7/49
8/64
9/81
10/100
24 | P a g e
Question 12
12.Program to generate Fibonacci series till n.
public class q12
{
public static void main(int n)
{
int f=0,s=1,t;
for(int i=1;i<=n;i++)
{
t=f+s;
System.out.print(" "+t);
f=s;
s=t;
}
}
}
Input: Output:
1 2 3 5 8 13 21 34 55 89
25 | P a g e
Question 13
13.Program to find the sum of the series x+ x2/2+x3/3…..xn /n.
import java.util.*;
public class q13
{
public static void main()
{
Scanner c=new Scanner(System.in);
System.out.println("Enter x");
int x=c.nextInt();
System.out.println("Enter n");
int n=c.nextInt();
double t;
double s=0.0;
for(int i=1;i<=n;i++)
{
t=Math.pow(x,i)/i;
s=s+t;
}
System.out.println(+s);
}
}
26 | P a g e
Input: Output:
x=2 Enter x
n=5 2
Enter n
5
17.066666666666666
27 | P a g e
Question 14
14.Program to find the sum of all integers greater than 100 and
less than 200 which are divisible by 8.
public class q14
{
public static void main()
{
int sum=0,count=0;
System.out.println("The factors of 8 are ");
for(int i=100;i<=200;i++)
{
if(i%8==0)
{
count=count+1;
sum=sum+i;
System.out.println(+i);
}
}
System.out.println("The sum of all integers are "+sum);
System.out.println("Number of integers are "+count);
}
}
28 | P a g e
Output:
The factors of 8 are
104
112
120
128
136
144
152
160
168
176
184
192
200
The sum of all integers are 1976
Number of integers are 13
29 | P a g e
Question 15
14.Program to check if a number is a composite number or not.
public class q15
{
public static void main(int n)
{
int a=0;
for(int i=2;i<n;i++)
{
if(n%i==0)
a=1;
}
if(a==0)
System.out.println(n+" is not a composite number");
else
System.out.println(n+" is a composite number");
}
}
Input: Output:
2 is not a composite number
30 | P a g e
Input: Output:
4 is a composite number
31 | P a g e
Question 16
16.Program to calculate the percentage of a student and print
his/her division.
import java.util.*;
public class q16
{
public static void main()
{
Scanner a=new Scanner (System.in);
System.out.println("Enter percentage of student");
int n=a.nextInt();
if (n>100)
System.out.println("invlaid");
else if (n>=60)
System.out.println("first");
else if (n>=45)
System.out.println("second");
else if (n>=35)
System.out.println("third");
else if (n<35)
System.out.println("fail");
}
}
32 | P a g e
Input: Output:
Percentage=89 Enter percentage of student
89
first
Input: Output:
Percentage=45 Enter percentage of student
45
second
Input: Output:
Percentage=39 Enter percentage of student
39
third
Input: Output:
Percentage=17 Enter percentage of student
17
fail
33 | P a g e
Question 17
17.Program to compute area of Square,Circle,Rectangle using
switch case.
import java.util.*;
public class q17
{
public static void main()
{
System.out.println("Enter 1 to compute area of a Circle");
System.out.println("Enter 2 to compute area of a Square");
System.out.println("Enter 3 to compute area of a Rectangle");
System.out.println("Enter 4 to quit");
Scanner ab=new Scanner (System.in);
int p;
double pie=3.14,area,r,s,l,b;
p=ab.nextInt();
switch(p)
{
case 1:
System.out.println("Enter radius of a Circle");
r=ab.nextDouble();
area=pie*r*r;
System.out.println("The area of a Circle "+area);
break;
case 2:
System.out.println("Enter side of a square");
s=ab.nextDouble();
34 | P a g e
area=s*s;
System.out.println("The area of a Square "+area);
break;
case 3:
System.out.println("Enter Length of a Rectangle");
l=ab.nextDouble();
System.out.println("Enter Breath of a Rectangle");
b=ab.nextDouble();
area=l*b;
System.out.println("The area of a Rectangle "+area);
break;
case 4:System.out.println("You opted to exit");
}
}
}
Input: Output:
p=1 Enter 1 to compute area of a Circle
Enter 2 to compute area of a Square
r=10 Enter 3 to compute area of a Rectangle
Enter 4 to quit
1
Enter radius of a Circle
10
The area of a Circle 314.0
35 | P a g e
Input: Output:
p=2 Enter 1 to compute area of a Circle
Enter 2 to compute area of a Square
s=24 Enter 3 to compute area of a Rectangle
Enter 4 to quit
2
Enter side of a square
24
The area of a Square 576.0
Input: Output:
p=3 Enter 1 to compute area of a Circle
Enter 2 to compute area of a Square
Enter 3 to compute area of a Rectangle
l=22 Enter 4 to quit
b=4 3
Enter Length of a Rectangle
22
Enter Breath of a Rectangle
4
The area of a Rectangle 88.0
36 | P a g e
Input: Output:
p=4 Enter 1 to compute area of a Circle
Enter 2 to compute area of a Square
Enter 3 to compute area of a Rectangle
Enter 4 to quit
4
You opted to exit
37 | P a g e
Question 18
18.Program to calculate electricity bill.
import java.util.*;
public class q18
{
public static void main()
{
Scanner abc=new Scanner(System.in);
double u,a;
System.out.println("Enter consumer no ");
int c=abc.nextInt();
System.out.println("Enter no of units consumed");
u=abc.nextDouble();
int r=200;
if(u<=100)
{
a=r;
System.out.println("The amount="+a);
}
else if(u>100 && u<=300)
{
a=1*u+r;
System.out.println("The amount="+a);
}
else if(u>300 && u<=500)
{
a=1.55*u+r;
System.out.println("The amount="+a);
38 | P a g e
}
else if (u>500)
{
a=2.10*u+r;
System.out.println("The amount="+a);
}
System.out.println("The consumer no is "+c);
System.out.println("Number of units consumed is "+u);
}
}
Input: Output:
c=234541 Enter consumer no
u=99 234541
Enter no of units consumed
99
The amount=200.0
The consumer no is 234541
Number of units consumed is 99.0
Input: Output:
c=234541 Enter consumer no
u=245 234541
Enter no of units consumed
245
The amount=445.0
The consumer no is 234541
Number of units consumed is 245.0
39 | P a g e
Input: Output:
c=234541 Enter consumer no
u=356 234541
Enter no of units consumed
356
The amount=751.8000000000001
The consumer no is 234541
Number of units consumed is 356.0
Input: Output:
c=234541 Enter consumer no
u=600 234541
Enter no of units consumed
600
The amount=1460.0
The consumer no is 234541
Number of units consumed is 600.0
40 | P a g e
Question 19
Program to check if a number is a automorphic number or not.
public class q19
{
public static void main(int n)
{
int i,s,c=1;
i=n;
s=n*n;
while (n!=0)
{
c=c*10;
n=n/10;
}
if(s%c==i)
System.out.println(i+" is a automorphic number");
else
System.out.println(i+" is not a automorphic number");
}
}
41 | P a g e
Input: Output:
6 is a automorphic number
Input: Output:
7 is not a automorphic number
42 | P a g e
Question 20
43 | P a g e
Input: Output:
i is a vowel
Input: Output:
P is a consonant
44 | P a g e
Question 21
21.Program to check if a number is a special number or not.
import java.util.*;
public class q21
{
public static void main()
{
Scanner b=new Scanner(System.in);
System.out.println("Enter number");
int n=b.nextInt();
if(n>=1&&n<100)
{
int r=n, rem,sum=0,pro=1;
int a=n;
while(a!=0)
{
rem=a%10;
int d1=rem;
a=a/10;
sum=sum+d1;
pro=pro*d1;
}
int spn=pro+sum;
if(spn==n)
System.out.println(r+" is a special number");
else
System.out.println(r+" is not a special number");
}
45 | P a g e
else
System.out.println("Enter only a 2 digit number");
}
}
Input: Output:
n=59 59
59 is a special number
Input: Output:
n=23 23
23 is not a special number
46 | P a g e
Question 22
22.Menu driven program to find factorial of a number and
factors of a number using switch case.
import java.util.*;
public class q22
{
public static void main()
{
int p,n;
Scanner ab=new Scanner (System.in);
System.out.println("enter 1 to choose factors of a number");
System.out.println("enter 2 to choose factorial of a number");
System.out.println("enter 3 to opt out ");
p=ab.nextInt();
switch(p)
{
case 1:
System.out.println("Enter the value of n");
n=ab.nextInt();
for(int i=1;i<n;i++)
{
if(n%i==0)
System.out.print(" "+i);
}
break;
case 2:System.out.println("Enter the value of n");
n=ab.nextInt();
47 | P a g e
long fac=1;
int b=1;
while(b<=n)
{
fac=fac*b;
b++;
}
System.out.print(+fac);
break;
case 3:System.out.println("You opted to come out");
default:System.out.println("Invalid character");
}
}
}
Input: Output:
p=1 enter 1 to choose factors of a
n=100 number
enter 2 to choose factorial of a
number
enter 3 to opt out
1
Enter the value of n
100
1 2 4 5 10 20 25 50
48 | P a g e
Input: Output:
p=2 enter 1 to choose factors of a
n=10 number
enter 2 to choose factorial of a
number
enter 3 to opt out
2
Enter the value of n
10
3628800
Input: Output:
p=3 enter 1 to choose factors of a
number
enter 2 to choose factorial of a
number
enter 3 to opt out
3
You opted to come out
49 | P a g e
Question 23
23.Menu driven program and to print the series 0,3,7,15…..n
and to find the sum of series 1/2+2/4,…..19/20 using switch
case.
import java.util.*;
public class q23
{
public static void main()
{
Scanner abc=new Scanner (System.in);
System.out.println("Enter a to print the series0,3,7,15....n");
System.out.println("Enter b to find the sum
of1/2+3/4+...19/20");
System.out.println("Enter c quit");
char a1=abc.next().charAt(0);
switch(a1)
{
case 'a': int s=0, x=2;
System.out.println("Enter n");
Scanner br=new Scanner (System.in);
int n=br.nextInt();
int i;
for(i=3;i<=n;i=i*2+1)
{
System.out.print(i+" ");
50 | P a g e
}
break;
case 'b':float sum=0;
float a=1, b=2;
while(a <19)
{
sum += a / b;
a += 2;
b += 2;
}
System.out.println(+sum);
break;
case 'c':
System.out.println("You opted to come out");
break;
default:
System.out.println("Enter valid character");
}
}
}
Input: Output:
a1=a Enter a to print the series0,3,7,15....n
n=100 Enter b to find the sum
of1/2+3/4+...19/20
Enter c quit
a
Enter n
51 | P a g e
100
3 7 15 31 63
Input: Output:
a1=b Enter a to print the series0,3,7,15....n
Enter b to find the sum
of1/2+3/4+...19/20
Enter c quit
b
7.585515
Input: Output:
a1=c Enter a to print the series0,3,7,15....n
Enter b to find the sum
of1/2+3/4+...19/20
Enter c quit
c
You opted to come out
52 | P a g e
Question 24
24.Program to input salary of hundred employees and to
calculate the gross salary.
import java.util.*;
public class q24
{
public static void main()
{
Scanner abc=new Scanner (System.in);
char name;
double da,sa,s,gs;
for(int i=1;i<=100;i++)
{
System.out.println("Enter name");
name=abc.next().charAt(0);
Input: Output:
name=Rakesh Enter name
Basic salary=13005 Rakesh
Enter basic salary
12005
Basic salary 12005.0
DA=1440.6
SA=960.4
GS=14406.0
55 | P a g e
name=Alfred Enter name
Basic salary=50000 Alfred
Enter basic salary
50000
Basic salary 50000.0
DA=10000.0
SA=6000.0
GS=66000.0
Enter name
(loop goes on )
56 | P a g e
Question 25
Input: Output:
Present reading= 1000 Enter present reading
Previous reading= 100 1000
Enter previous reading
100
Units consumed=900.0
The charge is 1620.0
Input: Output:
Present reading= 200 Enter present reading
Previous reading= 110 200
Enter previous reading
110
58 | P a g e
Units consumed=90.0
The charge is 112.5
Question 26
26.Program to calculate the pay of security agency.
import java.util.*;
public class q26
{
public static void main()
{
Scanner abc=new Scanner (System.in);
int t,r;
double c;
System.out.println("Enter time in hours");
t=abc.nextInt();
System.out.println("Enter cost for one hour");
r=abc.nextInt();
if(t<=48)
{
c=r*t;
System.out.println("The pay is "+c);
}
if(t<=48+8&&t>48)
{
59 | P a g e
c=r*1.25*t;
System.out.println("The pay is "+c);
}
if(t>48+8)
{
c=r*1.5*t;
System.out.println("The pay is "+c);
}
}
}
Input: Output:
Time=52 Enter time in hours
Cost of one hour=20 52
Enter cost for one hour
20
The pay is 1300.0
Input: Output:
Time=90 Enter time in hours
Cost of one hour=20 90
Enter cost for one hour
20
The pay is 2700.0
60 | P a g e
Question 27
61 | P a g e
16.442417876241407
Question 28
28.Program to check if a number is a sunny number or not.
public class q28
{
public static void main(int n)
{
int n1=n+1;
double x=Math.sqrt(n1);
if((int)x==x)
Input: Output:
62 | P a g e
99 is sunny number
Input: Output:
66 is not a sunny number
63 | P a g e
Question 29
29.Program to conduct operations on to numbers using custom
operators using if else ladder statements.
import java.util.*;
class q29
{
public static void main()
{
Scanner abc=new Scanner (System.in);
char op;
int m,n,r;
System.out.println("Enter operator");
System.out.println("Enter + for addition");
System.out.println("Enter - for subtraction");
System.out.println("Enter for multiplication");
System.out.println("Enter / for division");
op=abc.next().charAt(0);
System.out.println("Enter m (number1)");
m=abc.nextInt();
System.out.println("Enter n (number 2)");
n=abc.nextInt();
if(m>n)
64 | P a g e
{
if(m>0&&n>0)
{
if(op=='+')
{r=m+n;
System.out.println("The result is "+r);}
else if(op=='-')
{r=m-n;
System.out.println("The result is "+r);}
else if(op=='*')
{r=m*n;
System.out.println("The result is "+r);}
else if(op=='/')
{r=m/n;
System.out.println("The result is "+r);
}
else
System.out.println("Invalid operator");
}
else
System.out.println("Enter only positive number");
}
else
System.out.println("Enter m greater than n");
}
}
65 | P a g e
Input: Output:
Op= + Enter operator
m=30 Enter + for addition
n=20 Enter - for subtraction
Enter for multiplication
Enter / for division
+
Enter m (number1)
30
Enter n (number 2)
20
The result is 50
Input: Output:
Op= - Enter operator
m=30 Enter + for addition
n= 20 Enter - for subtraction
Enter for multiplication
Enter / for division
-
Enter m (number1)
66 | P a g e
30
Enter n (number 2)
20
The result is 10
Input: Output:
Op= * Enter operator
m=30 Enter + for addition
n= 20 Enter - for subtraction
Enter for multiplication
Enter / for division
*
Enter m (number1)
30
Enter n (number 2)
20
The result is 60
Input: Output:
Op= / Enter operator
m=30 Enter + for addition
n= 20 Enter - for subtraction
Enter for multiplication
Enter / for division
/
67 | P a g e
Enter m (number1)
30
Enter n (number 2)
10
The result is 3
Input: Output:
Op= q Enter operator
m=30 Enter + for addition
n= 20 Enter - for subtraction
Enter for multiplication
Enter / for division
q
Enter m (number1)
30
Enter n (number 2)
20
Invalid operator
68 | P a g e
Question 30
30.Program to amount for given principle.
import java.util.*;
class q30
{
public static void main()
{
Scanner abc=new Scanner (System.in);
double m,d,a;
System.out.println("Enter sum of money");
m=abc.nextDouble();
System.out.println("Enter no of days");
d=abc.nextDouble();
if(d<=180)
{
a=(m*5.5*d/100)+m;
System.out.println("The amount is "+a);
}
else if(d>180&&d<=364)
{
a=(m*7.5*d/100)+m;
69 | P a g e
System.out.println("The amount is "+a);
}
else if(d==365)
{
a=(m*8.0*d/100)+m;
System.out.println("The amount is "+a);
}
else if(d>365)
{
a=(m*8.5*d/100)+m;
System.out.println("The amount is "+a);
}
}
}
Input: Output:
Sum of money=200 Enter sum of money
Number of days=150 200
Enter no of days
150
The amount is 3500.0
Input: Output:
Sum of money=400 Enter sum of money
Number of days=246 400
Enter no of days
70 | P a g e
246
The amount is 7780.0
Input: Output:
Sum of money=500 Enter sum of money
Number of days=365 500
Enter no of days
365
The amount is 15100.0
Input: Output:
Sum of money=500 Enter sum of money
Number of days=500 500
Enter no of days
500
The amount is 21750.0
71 | P a g e