Sei sulla pagina 1di 30

NAME: - Gaurav Kumar

Class: - 9C
ROLL NO: - 22
YEAR: - 2018
SUBJECT: - COMPUTER
1. .A library charges a fine for books returned late as given below:

First Five days 40 paise per day


Six to Ten Days 65 paise per day
Above Ten Days 80 paise per day

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

Variable Data type Description


s int To store the number of days
entered by the user
a int To store the number of days
and compare it to find the fine
b float To store the fine assuming
c int To store the fine in Rupees
p double To store the fine in Paisa

Sample 1

Sample Input: Enter the No. of days late

12

Sample Output: The fine assuming for 12 no. of days is 6 Rupees and 55 Paisa

Sample 2

Sample Input: Enter the No. of days late

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:

Annual Salary Rate of Income Tax


Up to Rs 2,50,000 No Tax
Rs 2,50,001 to Rs 5,00,000 10% of the amount exceeding Rs 2,50,000
Rs 5,00,001 to Rs 10,00,000 Rs 10,000+20% of the amount exceeding Rs 5,00,000
Above Rs 10,00,000 Rs 25,000+30% of the amount exceeding Rs 10,00,000

Write a program in java to compute the income tax to be paid by him.

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

Variable Data Type Description


a double To store the amount entered by
the user
e double To store the extra amount
exceeding the amount to get
the income tax
r double To store the rate of Income Tax

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.

Marks Obtained in Diff subjects Stream


Eng, Maths and Science>=80% Pure Science
Eng and Science>=80%, Maths>=60% Bio.Science
Eng, Maths and Science>=60% Commerce

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

Variable Data type Description


e float To store the marks in English
m float To store the marks in Maths
s float To store the marks in Science
p float To store the aggregate marks
in all the three subjects
c float To store the aggregate marks
in English and Science
Sample 1

Sample Input: Enter the marks obtained in English, Maths and Science

79

99

88

Sample Output: The stream is Pure Science

Sample 2

Sample Input: Enter the marks obtained in English, Maths and Science

81

67

82

Sample Output: The stream is Bio Science


4. A company announces revised Dearness Allowance(DA) and Special Allowance(SA) for their
employees as per the tariff given below: s:

Basic Dearness Allowance Special Allowance


UP to Rs. 10,000 15% 5%
Rs. 10,001-Rs. 20,000 20% 8%
Rs. 20,001-Rs. 30,000 25% 10%
Rs. 30,001 and above 30% 12%

Write a program to accept Basic Salary (BS) of an employee. Calculate and display gross salary.

Gross Salary=Basic + Dearness Allowance + Special Allowance.

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 Input: Enter the Basic Salary


30000
Sample Output:
_____________________________________________________________________________________
|Basic | Dearness Allowance | Special Allowance | Gross Salary |
------------------------------------------------------------------------------------------------------------------------------------------
|30000.0 | 7500.0 | 3000.0 | 40500.0 |
------------------------------------------------------------------------------------------------------------------------------------------

Sample 2

Sample Input: Enter the Basic Salary


90000
Sample Output:
_____________________________________________________________________________________
|Basic | Dearness Allowance | Special Allowance | Gross Salary |
------------------------------------------------------------------------------------------------------------------------------------------
|90000.0 | 27000.0 | 10800.0 | 127800.0 |
------------------------------------------------------------------------------------------------------------------------------------------
5. A courier company charges foe an ‘Ordinary’ mail and an ‘Express’ mail based on the weight of
the parcel as per the tariff given below:

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

Variable Data type Description


c double To store the charge of the
parcel
w long To store the weight of parcel
entered by the user
a char To store the choice entered by
the user
Sample 1

Sample Input: Enter the weight of the parcle

400

Enter '0' or 'o' for Ordinary mail

Enter 'E' or 'e' for Express mail

Sample Output: The charge on 400.0 gms in Ordinary mail : 160.0

Sample 2

Sample Input: Enter the weight of the parcle

700

Enter '0' or 'o' for Ordinary mail

Enter 'E' or 'e' for Express mail


e

Sample Output: The charge on 700.0 gms in Express mail : 455.0


6. Write a menu driven program to calculate:
1 .Area of a circle=πr2 π =22/7
2. Area of a square= side *side
3. Area of a rectangle=length*breath
Enter ‘c’ to calculate area of a circle, ‘s’ to calculate area of a square and ‘r’ to calculate area of
rectangle.

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 Input: Enter 'c' or 'C' to calculate the area of a circle

Enter 's' or 'S' to calculate the area of a square

Enter 'r' or 'R' to calculate the area of a rectangle

Enter your Choice

Enter the radius of the Circle

Sample Output: The area is: 154.0

Sample 2

Sample Input: Enter 'c' or 'C' to calculate the area of a circle

Enter 's' or 'S' to calculate the area of a square

Enter 'r' or 'R' to calculate the area of a rectangle

Enter your Choice

Enter the length and breadth of the rectangle

Sample Output: The area is: 72.0


7. Write a menu driven program that outputs the results of the following evaluations based on the
number entered by the user:
1. Volume of a sphere= π *r*r*r π=22/7
2. Volume of a cube=side*side*side
3. Volume of a cuboid=length*breath*height

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 Input: Enter 1 to find the volume of a sphere


Enter 2 to find the volume of a cube
Enter 3 to find the volume of a cuboid
Enter your choice
1
Enter the radius of the sphere
7
Sample Output: The Volume of the Sphere: 1078.0

Sample 2

Sample Input: Enter 1 to find the volume of a sphere

Enter 2 to find the volume of a cube

Enter 3 to find the volume of a cuboid

Enter your choice

Enter the length, Breadth and Height of the cuboid

Sample Output: The volume of the cuboid: 210.0


8. Write a menu driven program that outputs the results of the following evaluations based on the
number entered by the user:
1. Natural logarithm of the number
2. Absolute value of the number
3. Square root of the number
4. Random numbers between 0 and 1

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

Variable Data type Description


c int To store the choice entered
by the user
n Double To store the value entered
by the user
Sample 1

Sample Input: Enter 1 for Natural Logarithm


Enter 2 for Absolute value
Enter 3 for Square root
Enter 4 for Random numbers
Enter your Choice
3
Enter the number
4
Sample Output: The Square root is: 2.0

Sample 2

Sample Input: Enter 1 for Natural Logarithm


Enter 2 for Absolute value
Enter 3 for Square root
Enter 4 for Random numbers
Enter your Choice
1
Enter the number
3
Sample Output: The Natural Logarithm is: 1.0986122886681098
9. You want to buy an old car from ‘Sales and Purchase’, where you can get a car in it’s depreciated
price. The depreciated value of a car is calculate on its showroom price and the number of years
it has been used. The depreciated value of a car is calculate as per the tariff given below:

No. of years used Rate of deprecations


1 10%
2 20%
3 30%
4 50%
Above 4 years 60%

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

Variable Data type Description


n int To store the choice of the user
o double To store the original price
entered by the user
a double To store the amount after
depreciation
d double To store the depreciation in
price

Sample 1

Sample Input: Enter 1 for 'one year old'

Enter 2 for 'two year old'

Enter 3 for 'three year old'

Enter 4 for 'four year old'

Enter 5 for 'Above 4 year old'

Enter your Choice

Enter the Original price

30000

Sample Output:

Original Price: 30000.0 Depreciated Price: 15000.0 Amount Paid: 15000.0

Sample 2

Sample Input: Enter 1 for 'one year old'

Enter 2 for 'two year old'

Enter 3 for 'three year old'

Enter 4 for 'four year old'

Enter 5 for 'Above 4 year old'

Enter your Choice

Enter the Original price

8000

Sample Output:

Original Price: 8000.0 Depreciated Price: 1600.0 Amount Paid: 6400.0


10. An electronics shop has announced the following seasonal discounts on the purchase of certain
items.
Category Discounts on Laptops Discounts on Desktop PC
Up to Rs. 25,000 0.0% 5.0%
Rs. 25,001-Rs.57,000 5.0% 7.5%
Rs. 57,001-Rs. 1,00,000 7.5% 10.0%
More than Rs. 1,00,000 10.0% 15.0%

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

Variable Data Type Description


p double To store the price entered by
the user
d double To store the depreciation in
price
na double To store the amount after
depreciation
ch char To store the choice of the user

Sample 1

Sample Input: Enter your Price

55000

Enter 'L' or 'l' for Laptop or 'D' or 'd' for Desktop

Sample Output: The amount is: 52250.0

Sample 2

Sample Input: Enter your Price

70000

Enter 'L' or 'l' for Laptop or 'D' or 'd' for Desktop

Sample Output: The amount is: 63000.0


11. Print the multiplication tables in the pattern given below:

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

Variable Data type Description


a int To store the number of rows
b int To execute the no. of spaces
in each row
d int To execute the # sign
n int To store the no. of spaces in
each row

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 Input: Enter the range


7
Sample Output: 294 (9+99+8+88+7+77+6)

Sample 2

Sample Input: Enter the range


5
Sample Output: 211 (9+99+8+88+7)
15. Write a program to accept a number and display the new number after removing all zeros.

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

Variable Data type Description


a long To store the number entered
by the user
b long To store a copy of the number
entered by the user
s long To store the digits in Standard
Form
sum long To store the sum of the digits
extracted
c long To store each digits of the
number entered by the user

Sample 1

Sample Input: Enter a number

5070906080

Sample Output: 57968

Sample 2

Sample Input: Enter a number

34014235060

Sample Output; 34142356


16. Write a program to print the sum of negative numbers, sum of positive odd numbers and sum of
positive even numbers from a list of numbers entered by the user. The list terminates when the
user enters zero.

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

Sample Input: Enter your number

10

Sample Output: The factors of 10 are:

1
2
5
10

Sample 2

Sample Input: Enter your number

16

Sample Output: The factors of 16 are:

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

Variable Data type Description


a long To store the first number
b long To store the second number
f1 long To store the number of factors
of the first number
f2 long To store the number of factors
of the second number
i long To find the factors of the
number
d long To find the difference between
the two number

Sample 1

Sample Input: Enter two number


11
13
Sample Output: The number is Twin Prime number

Sample 2

Sample Input: Enter two number


13
17
Sample Output: The number is not Twin-Prime Number
19. Write a program in Java to accept a number and display the result in its Binary equivalent.

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

Variable Data type Description


u long To store the value entered by
the user
c long To store each digits of the
number entered by the user
sum long To store the sum of the digits
of the number
s long To store the number in
Standard form

Sample 1

Sample Input: Enter the number

564

Sample Output: 1000110100

Sample 2

Sample Input: Enter the number

9804

Sample Output: 10011001001100


20. Write a program in Java to enter a number containing three digits or more. Arrange the digits of
the entered number in ascending order and display the result.

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

Variable Data Type Description


a long To store the value entered by
the user
c long To store a copy of the value
entered by the user
i int To compare each digits by the
number between 1 to 9
d long To store each digits of the
number entered by the user

Sample 1

Sample Input: Enter a number


543241231
Sample Output: 1, 1, 2, 2, 3, 3, 4, 4, 5,

Sample 2

Sample Input: Enter a number


7395141514
Sample Output: 1, 1, 1, 3, 4, 4, 5, 5, 7, 9,

Potrebbero piacerti anche