Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Class: - 9C
ROLL NO: - 22
YEAR: - 2018
SUBJECT: - COMPUTER
1. .A library charges a fine for books returned late as given below:
Design a program in java to calculate the fine assuming that a book is returned N days late.
import java.util.*;
public class Lib_charge
{
public static void main(String args[])
{
int s; float b;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the No. of days late");
s=sc.nextInt();
int a=s;
if(a>5)
{
b=5*40;
a=a-5;
if(a!=0)
{
if(a<10)
{
b=b+(a*65);
}
else if(a>10)
{
b=b+(10*65);
a=a-10;
if(a!=0)
{
b=b+(a*80);
}
}
}
}
else
{
b=a*40;
}
b=b/100;
int c=(int)b;
double p=(b-c)*100;
System.out.println("The fine assuming for " + s+ " no. of days is "+c+" Rupees"+" and "+(int)p+" Paisa
");
}
}
Variable Description
Sample 1
12
Sample Output: The fine assuming for 12 no. of days is 6 Rupees and 55 Paisa
Sample 2
24
Sample Output: The fine assuming for 24 no. of days is 15 Rupees and 69 Paisa
2. Mr. A.P.Singh is a software engineer. He repays annual income tax as per the given table:
import java.util.*;
class Income_Tax
{
public static void main()
{
double a,e,r;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Amount");
a=sc.nextDouble();
if(a<=250000)
{
r=0;
}
else if(a>250000&&a<=500000)
{
e=a-250000;
r=10.0/100*e;
}
else if(a>500000&&a<=1000000)
{
e=a-500000;
r=10000+(20.0/100*e);
}
else
{
e=a-1000000;
r=25000+(30/100*e);
}
System.out.println("Income Tax to be paid: "+"Rupees "+r);
}
}
Variable Description
Sample 1
Sample Input: Enter the Amount
600000
Sample Output: Income Tax to be paid: Rupees 30000.0
Sample 2
Sample Input: Enter the Amount
2000000
Sample Output: Income Tax to be paid: Rupees 325000.0
3. St. Xavier School displays a notice on the school notice board regarding admission in Std. XI for
choosing different streams, according to marks obtained in English, Maths and Science in ICSE
Examinations.
Print the appropriate stream allotted to the candidate. Write a program in Java to accept marks
in English, Maths and Science from the console.
import java.util.*;
public class Stream
{
public static void main(String args[])
{
float e,m,s,p,c;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the marks obtained in English, Maths and Science");
e=sc.nextInt();
m=sc.nextInt();
s=sc.nextInt();
p=(e+m+s)/3;
c=(e+s)/2;
if(p>=80)
System.out.println("The stream is Pure Science");
else if(c>=80&&m>=60)
System.out.println("The stream is Bio Science");
else if(p>=60&&p<80)
System.out.println("The stream is Commerce");
else
System.out.println("no Stream");
}
}
Variable Description
Sample Input: Enter the marks obtained in English, Maths and Science
79
99
88
Sample 2
Sample Input: Enter the marks obtained in English, Maths and Science
81
67
82
Write a program to accept Basic Salary (BS) of an employee. Calculate and display gross salary.
import java.util.*;
class salary
{
public static void main()
{
Scanner sc=new Scanner(System.in);
double b,d=0.0,s=0.0,g=0.0;
System.out.println("Enter the Basic Salary");
b=sc.nextDouble();
if(b<=10000)
{
d=15.0/100*b;
s=5.0/100*b;
}
else if(b>10000&&b<=20000)
{
d=20.0/100*b;
s=8.0/100*b;
}
else if(b>20000&&b<=30000)
{
d=25.0/100*b;
s=10.0/100*b;
}
else
{
d=30.0/100*b;
s=12.0/100*b;
}
g=b+d+s;
System.out.println("____________________________________________________________________
_____________________________________");
System.out.println("|"+"Basic"+"\t"+"\t"+"|"+"\t"+"Dearness Allowance"+"\t"+"|"+"\t"+"Special
Allowance"+"\t"+"|"+"\t"+"Gross Salary"+"\t"+"|");
System.out.println("---------------------------------------------------------------------------------------------------------");
System.out.println("|"+ b +"\t"+"|"+"\t"+ d +"\t"+"\t"+"\t"+"|"+"\t"+ s +"\t"+"\t"+"\t"+"|"+"\t"+ g
+"\t"+"\t"+"|");
System.out.println("---------------------------------------------------------------------------------------------------------");
}
}
Variable Description
Variable Data type Description
b double To store the basic salary entered
by the user
d double To store the Dearness Allowance
s double To store the Special Allowance
g double To store the Gross Salary
Sample 1
Sample 2
Weight of parcel (in gms) Charge on Ordinary mail Charge on Express mail
Up to 100 gms Rs. 50 Rs. 80
101 to 500 gms Rs. 40/100 gms Rs. 70/100 gms
501 and above Rs. 35/100 gms Rs. 65/100 gms
Write a program to accept the weight of a parcel and type of mail (‘O’ for ordinary and ‘E’ for express).
Calculate and print the charges under each category.
import java.util.*;
class charge
{
public static void main()
{
double c;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the weight of the parcle");
double w=sc.nextLong();
System.out.println("Enter '0' or 'o' for Ordinary mail");
System.out.println("Enter 'E' or 'e' for Express mail");
char a=sc.next().charAt(0);
switch(a)
{
case 'O':
case 'o':
if(w<=100)
c=50;
else if(w>100&&w<=500)
c=w*(40.0/100);
else
c=w*(35.0/100);
System.out.println("The charge on "+ w +" gms in Ordinary mail : "+ c);
break;
case 'E':
case 'e':
if(w<=100)
c=80;
else if(w>100&&w<=500)
c=w*(70.0/100);
else
c=w*(65.0/100);
System.out.println("The charge on "+ w +" gms in Express mail : " + c);
break;
default:
System.out.println("Wrong Choice");
}
}
}
Variable Description
400
Sample 2
700
import java.util.*;
class area
{
public static void main()
{
Scanner sc=new Scanner(System.in);
double a,r,s,l,b;
char c;
System.out.println("Enter 'c' or 'C' to calculate the area of a circle");
System.out.println("Enter 's' or 'S' to calculate the area of a square");
System.out.println("Enter 'r' or 'R' to calculate the area of a rectangle");
System.out.println("Enter your Choice");
c=sc.next().charAt(0);
switch(c)
{
case 'c':
case 'C':
System.out.println("Enter the radius of the Circle");
r=sc.nextDouble();
a=22.0/7*r*r;
System.out.println("The area is: "+ a);
break;
case 's':
case 'S':
System.out.println("Enter the sides of the square");
s=sc.nextDouble();
a=s*s;
System.out.println("The area is: "+ a);
break;
case 'r':
case 'R':
System.out.println("Enter the length and breadth of the rectangle");
l=sc.nextDouble();
b=sc.nextDouble();
a=l*b;
System.out.println("The area is: "+a);
break;
default:
System.out.println("Wrong Choice");
}
}
}
Variable Description
Variable Data Type Description
r double To store the radius of circle
s double To store the side of square
l double To store the length of rectangle
b double To store the breadth of rectangle
a double To calculate the area according to
the choice
c char To store the choice of the user
Sample 1
Sample 2
import java.util.*;
class Volume
{
public static void main()
{
double v,r,s,l,b,h;
int c;
Scanner sc=new Scanner(System.in);
System.out.println("Enter 1 to find the volume of a sphere");
System.out.println("Enter 2 to find the volume of a cube");
System.out.println("Enter 3 to find the volume of a cuboid");
System.out.println("Enter your choice");
c=sc.nextInt();
switch(c)
{
case 1:
System.out.println("Enter the radius of the sphere");
r=sc.nextDouble();
v=22.0/7*r*r*r;
System.out.println("The Volume of the Sphere: "+v);
break;
case 2:
System.out.println("Enter the side of the cube");
s=sc.nextDouble();
v=s*s*s;
System.out.println("The volume of the Cube: "+v);
break;
case 3:
System.out.println("Enter the length, Breadth and Height of the cuboid");
l=sc.nextDouble();
b=sc.nextDouble();
h=sc.nextDouble();
v=l*b*h;
System.out.println("The volume of the cuboid: "+v);
break;
default:
System.out.println("Wrong Choice");
}
}
}
Variable Description
Variable Data Type Description
v double To calculate the volume
according to the choice
r double To store the radius of the
sphere
s double To store the side of the cube
l double To store the length of the
cuboid
b double To store the breadth of the
cuboid
h double To store the height of the
cuboid
c int To store the choice of the user
Sample 1
Sample 2
import java.util.*;
class driven_program
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter 1 for Natural Logarithm");
System.out.println("Enter 2 for Absolute value");
System.out.println("Enter 3 for Square root");
System.out.println("Enter 4 for Random numbers");
System.out.println("Enter your Choice");
int c=sc.nextInt();
double n;
switch(c)
{
case 1:
System.out.println("Enter the number");
n=sc.nextDouble();
n=Math.log(n);
System.out.println("The Natural Logarithm is: " +n);
break;
case 2:
System.out.println("Enter the number");
n=sc.nextDouble();
n=Math.abs(n);
System.out.println("The Absolute value is: " +n);
break;
case 3:
System.out.println("Enter the number");
n=sc.nextDouble();
n=Math.sqrt(n);
System.out.println("The Square root is: " +n);
break;
case 4:
System.out.println("Enter the number");
n=sc.nextDouble();
n=Math.random();
System.out.println("The Random number is: " +n);
break;
default:
System.out.println("Wrong Input");
}
}
}
Variable Description
Sample 2
Write a menu driven program to input showroom price and the number of years used (‘1’ for one year
old, ‘2’ for two years old and so on). Calculate the depreciated value. Display the original price of the car,
depreciated value and the amount paid for the car.
import java.util.*;
class Sale_Purchase2
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter 1 for 'one year old'");
System.out.println("Enter 2 for 'two year old'");
System.out.println("Enter 3 for 'three year old'");
System.out.println("Enter 4 for 'four year old'");
System.out.println("Enter 5 for 'Above 4 year old'");
System.out.println("Enter your Choice");
int n=sc.nextInt();
double o,a=0,d=0;
System.out.println("Enter the Original price");
o=sc.nextDouble();
switch(n)
{
case 1:
a=o-(o*0.10);
d=o*0.10;
break;
case 2:
a=o-(o*0.20);
d=o*0.20;
break;
case 3:
a=o-(o*0.30);
d=o*0.30;
break;
case 4:
a=o-(o*0.50);
d=o*0.50;
break;
case 5:
a=o-(o*0.60);
d=o*0.60;
break;
default:
System.out.println("Wrong Input");
}
System.out.println("Original Price: "+o+"\t"+"Depreciated Price: " + d+"\t"+"Amount Paid: "+ a);
}
}
Variable Description
Sample 1
30000
Sample Output:
Sample 2
8000
Sample Output:
import java.util.*;
class seasonal_discounts
{
public static void main()
{
double p,d=0.0,na=0.0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter your Price");
p=sc.nextDouble();
System.out.println("Enter 'L' or 'l' for Laptop or 'D' or 'd' for Desktop");
char ch=sc.next().charAt(0);
switch(ch)
{
case 'L':
case 'l':
if(p<=25000)
{
d=0*p;
na=p-d;
}
else if(p>25000&&p<57000)
{
d=(5.0/100)*p;
na=p-d;
}
else if(p>57000&&p<100000)
{
d=(7.5/100)*p;
na=p-d;
}
else
{
d=(10.0/100)*p;
na=p-d;
}
System.out.println("The amount is: "+na);
break;
case 'D':
case 'd':
if(p<=25000)
{
d=(5.0/100)*p;
na=p-d;
}
else if(p>25000&&p<57000)
{
d=(7.5/100)*p;
na=p-d;
}
else if(p>57000&&p<100000)
{
d=(10.0/100)*p;
na=p-d;
}
else
{
d=(15.0/100)*p;
na=p-d;
}
System.out.println("The amount is: "+na);
break;
default:
System.out.println("WRONG INPUT");
}
}
}
Variable Description
Sample 1
55000
Sample 2
70000
class pattern6
{
public static void main()
{
int n,e,g;
System.out.println("********************************************************************
*******************************");
for(n=1;n<=12;n++)
{
System.out.print("\t"+n);
}
System.out.println();
System.out.println("********************************************************************
*******************************");
for (g=1;g<=5;g++)
{
System.out.print(g);
for(e=1;e<=12;e++)
{
System.out.print("\t"+(e*g));
}
System.out.println();
}
System.out.println("********************************************************************
*******************************");
}
}
Variable Description
Variable Data type Description
n int To print the horizontal line of
the table
g int To print the vertical line of the
table
e int To print the multiplication
table
Sample 1
Sample Output:
**************************************************************************
1 2 3 4 5 6 7 8 9 10 11 12
**************************************************************************
1 1 2 3 4 5 6 7 8 9 10 11 12
2 2 4 6 8 10 12 14 16 18 20 22 24
3 3 6 9 12 15 18 21 24 27 30 33 36
4 4 8 12 16 20 24 28 32 36 40 44 48
5 5 10 15 20 25 30 35 40 45 50 55 60
**************************************************************************
12. Write a program to print the following pattern:
#
# #
# # #
# # # #
class pattern10
{
public static void main()
{
int a,b,d,n=3;
for(a=1;a<=4;a++)
{
for(b=1;b<=n;b++)
{
System.out.print(" ");
}
n--;
for(d=1;d<=a;d++)
{
System.out.print(" "+"#");
}
System.out.println();
}
}
}
Variable Description
Sample 1
Sample Output: #
##
###
####
13. Write a program to input a number and print whether the number is a ‘Special number’ or not. A
number is said to be special number if the sum of the factorial of the digits of the number is same
as the original number.
import java.util.*;
public class special_number
{
public static void main(String args[])
{
long a,b,c,i=1,sum=0;
Scanner ob=new Scanner(System.in);
System.out.println("Enter the number:");
a=ob.nextLong();
long d=a;
while(a>0)
{
b=a%10;
a=a/10;
for(c=1;c<=b;c++)
{
i=i*c;
}
sum=sum+i;
i=1;
}if(sum==d)
System.out.println("The number is special number");
else
System.out.println("The number is not a special number");
}
}
Variable Description
Variable Data type Description
a long To store the value entered by
the user
b long To store the each of the digits
of the number
c long To find the factorial
i long To store the factorial of each
of the digits
sum long To store the sum of the
factorial of each of the digits
d long To sore a copy of the value
entered by the user
Sample 1
Sample Input: Enter the number:
145
Sample Output: The number is special number
Sample 2
Sample Input: Enter the number:
210
Sample Output: The number is not a special number
14. Write a program in Java to find the sum of the given series:
a) S = 9 + 99 + 8 + 88 + 7 + ……………………………..to n.
import java.util.*;
class sum_series_project
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the range");
int n=sc.nextInt();
int a,b=9,sum=0,c;
for(a=1;a<=(int)(n+1)/2;a++)
{
sum=sum+b;
b--;
}
b=9;
for(c=1;c<=(int)(n/2);c++)
{
sum=sum+(b*11);
b--;
}
System.out.println(sum);
}
}
Variable Description
Variable Data Type Description
n int To store the range of
number entered by the user
a int To store the number of
times the single digits while
be executed
b int To find the sum of the
pattern
sum int To store the sum of number
c int To store the number of
times the double digits while
be executed
Sample 1
Sample 2
import java.util.*;
class removing_zero
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
long a=sc.nextLong();
long b=a,s=0,sum=0;
while(b>0)
{
long c=b%10;
b=b/10;
if(c!=0)
{
sum=sum+c*((long)Math.pow(10,s));
s++;
}
}
System.out.println(sum);
}
}
Variable Description
Sample 1
5070906080
Sample 2
34014235060
import java.util.*;
class sum_of_number
{
public static void main()
{
Scanner sc=new Scanner(System.in);
long r,k=1,a,sum1=0,sum2=0,sum3=0;
System.out.println("Enter the Range of number");
r=sc.nextLong();
if(r>0)
{
System.out.println("Enter the numbers");
do
{
a=sc.nextLong();
if(a>=0)
{
if(a%2==0)
sum1=sum1+a;
else
sum2=sum2+a;
}
else
sum3=sum3+a;
k++;
}
while(k<=r);
System.out.println("The sum of Negative Number: "+sum3);
System.out.println("The sum of positive odd number: "+sum2);
System.out.println("The sum of positive even number: "+sum1);
}
else
System.out.println("The range is not real number");
}
}
Variable Description
Variable Data Type Description
r long To store the range
k long To store the
a long To store each number entered by the user
sum1 long To store the sum of all positive even number
sum2 long To store the sum of all positive odd number
sum3 long To store the sum of all negative number
Sample 1
Sample Input: Enter the Range of number
2
Enter the numbers
4
-9
Sample Output: The sum of Negative Number: -9
The sum of positive odd number: 0
The sum of positive even number: 4
Sample 2
Sample Input: Enter the Range of number
0
Sample Output: The range is not real number
17. Write a program to find the factors of a number including 1 and the number itself.
import java.util.*;
public class factors
{
public static void main(String args[])
{
long a,n;
Scanner ob=new Scanner(System.in);
System.out.println("Enter your number");
n=ob.nextLong();
System.out.println("The factors of "+n+" are:");
for(a=1;a<=n;a++)
{
if(n%a==0)
System.out.println(a);
}
}
}
Variable Description
Variable Data type Description
a long To compare it with the
number to get its factor
n long To store the value entered by
the user
Sample 1
10
1
2
5
10
Sample 2
16
16
18. Write a program to accept two numbers and check whether they are twin prime or not.
import java.util.*;
class Twin_prime
{
public static void main()
{
long a,b,f1=0,f2=0,i,d;
Scanner sc=new Scanner(System.in);
System.out.println("Enter two number");
a=sc.nextLong();
b=sc.nextLong();
for(i=1;i<=a;i++)
{
if(a%i==0)
f1++;
}
for(i=1;i<=b;i++)
{
if(b%i==0)
f2++;
}
d=Math.abs(a-b);
if(f1==2&&f2==2&&d==2)
System.out.println("The number is Twin Prime number");
else
System.out.println("The number is not Twin-Prime Number");
}
}
Variable Description
Sample 1
Sample 2
import java.util.*;
public class dec_bin
{
public static void main(String args[])
{
long s=0,c,sum=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number");
long u=sc.nextLong();
while(u>0)
{
c=u%2;
u=u/2;
sum=sum+c*((long)Math.pow(10,s));
s++;
}
System.out.println(sum);
}
}
Variable Description
Sample 1
564
Sample 2
9804
import java.util.*;
class no_ascending2
{
public static void main()
{
long a;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
a=sc.nextLong();
long c=a;
for(int i=0;i<=9;i++)
{
while(a>0)
{
long d=a%10;
if(d==i)
System.out.print(i+", ");
a=a/10;
}
a=c;
}
}
}
Variable Description
Sample 1
Sample 2