You are on page 1of 10

1. Write a program to input three numbers (positive or negative).

If they
are unequal then
display the greatest number, otherwise display they are equal. The
program also
displays whether the numbers entered by the user are ‘All positive’,
‘All Negative’ or
‘Mixed numbers’.
Sample Input: 56, -15, 12
Sample Output: The Greatest number is 56
Entered numbers are mixed numbers.

//Program to find the greatest number and check whether the given numbers are mixed, equal or
unequal numbers
import java.util.*;
class number
{
public static void main(String args[])
{
int a,b,c,gn;
Scanner sc=new Scanner(System.in);
System.out.println("Enter three numbers");
a=sc.nextInt();
b=sc.nextInt();
c=sc.nextInt();
if(a==b && a==c)
System.out.println("Entered numbers are equal");
else
{
if(a>b && a>c)
gn=a;
else if(b>a && b>c)
gn=b;
else
gn=c;
System.out.println("Greatest number is "+gn);
}
if(a>0 && b>0 && c>0)
{
System.out.println("All are positive numbers");
}
else if(a<0 && b<0 && c<0)
{
System.out.println("All are negative numbers");
}
else
{
System.out.println("Entered numbers are mixed numbers");
}
}//END OF MAIN
}//END OF CLASS

Variable Type Use


a int to store the first number
b int to store th second number
c int to store the third number
gn int to store the greatest out of the three numbers

2. An air-conditioned bus charges fare from the passengers based on the


distance
travelled as per the tariff given below:
Distance Travelled Fare
Up to 10 km Fixed charge Rs. 80
11 km to 20 km Rs.6/km
21 km to 30 km Rs.5/km
31 km and above Rs.4/km
Design a program to input distance travelledby the passenger. Calculate
and display the
fare to be paid.

//Program to calculate the fare of a bus


import java.util.*;
class fare

{
public static void main(String[] strings)
{
int d,f;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the distance travelled");
d=sc.nextInt();
if(d<=10)
{
f=80;
}
else if(d>10&&d<=20)
{
f=80+(d-10)*6;
}
else if(d>20&&d<=30)
{
f=80+10*6+(d-20)*5;

}
else
{
f=80+10*6+10*5+(d-30)*4;
}
System.out.println("Fare to be paid is Rs."+f);
}//END OF MAIN
}//END OF CLASS
Variable Type Use
d int To store the distance travelled
f int To store the fare to be paid

3. The standard form of quadratic equation is given by:


ax2+bx+c=0, where d=b2-4ac, is known as discriminant that determines the
nature of
the roots of the equation as:

Condition Nature
if d>=0 Roots are real
if d<0 Roots are imaginary
Write a program to determine the nature and the roots of a quadratic
equation, taking
a,b,c as input. If d=b2-4ac is greater than or equal to zero, then
display ‘Roots are
real’, otherwise display ‘Roots are imaginary’.
The roots are determined by the formula as:
r1=-b+ b2-4ac , r2= -b – b2-4ac
2a 2a

//Program to determine th nature of roots and also calculate the roots


import java.util.*;
class roots

{
public static void main(String[] strings)
{
int a,b,c,d;
double r1,r2;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the values of a,b,c as in a quadratic equation");
a=sc.nextInt();
b=sc.nextInt();
c=sc.nextInt();
d=b*b-4*a*c;
if(d>=0)
{
System.out.println("Roots are real");
r1=(-b+Math.pow(b*b-4*a*c,1/2))/2*a;
r2=(-b-Math.pow(b*b-4*a*c,1/2))/2*a;
System.out.println("Roots are "+r1+" and "+r2);
}
else
{
System.out.println("Roots are imaginary");
}
}//END OF MAIN
}//END OF CLASS
Variable Type Use
a int to store to value of a as in a quadratic
equation
b int to store to value of b as in a quadratic
equation
c int to store to value of c as in a quadratic
equation
d int to store to value of discriminant
r1 double to store the first root
r2 double to store the second root

4. Write a program using switch case to find the volume of a cube, a


sphere and a
cuboid. For an incorrect choice, an appropriate error message should be
displayed.
a) Volume of a cube
b) Volume of a sphere
c) Volume of a cuboid
//Program to calculate volume of a cube, sphere or cuboid
import java.util.*;
class volume
{
public static void main(String[] strings)
{
int ch,s,r,l,b,h;
double v;
Scanner sc=new Scanner(System.in);
System.out.println("1. Volume of cube");
System.out.println("2. Volume of sphere");
System.out.println("3. Volume of cuboid");
System.out.println("Enter the choice");
ch=sc.nextInt();
switch(ch)
{
case 1:
System.out.println("Enter the side");
s=sc.nextInt();
v=s*s*s;
System.out.println("Volume of cube is "+v);
break;
case 2:
System.out.println("Enter the radius");
r=sc.nextInt();
v=(4/3.0)*(22/7)*r*r*r;
System.out.println("Volume of sphere is "+v);
break;
case 3:
System.out.println("Enter the length,breadth and height");
l=sc.nextInt();
b=sc.nextInt();
h=sc.nextInt();
v=l*b*h;
System.out.println("Volume of cuboid is "+v);
break;
default:
System.out.println("Invalid choice");
break;
}
}//END OF MAIN
}//END OF CLASS
Variable Type Use
ch int to store the choice of the user
s int to store the side of a cube
r int to store the radius of a sphere
l int to store the length of cuboid
b int to store the breadth of cuboid
h int to store the height of cuboid
v double to store the volume of cube or sphere or cuboid

1. Write a program to print Fibonacci series of n terms where n is input by


user:
0 1 1 2 3 5 8 13 24 .....

import java.util.*;
public class FibonacciSeries
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n; // To hold number of terms
int a = 0,b = 1,c=0;
System.out.println("Enter number of terms to be printed ");
n = sc.nextInt();
System.out.print(a+" "+b+" ");
for(int i = 3; i <= n; i++)
{
c=a+b;
System.out.print(c + " ");
a=b;
b=c;
}
} //END OF MAIN
}//END OF CLASS

Variable Type Use


a int serves as a variable to print fibonacci series
b int serves as a variable to print fibonacci series
c int to store the values to be printed in fibonacci
series
n int to store the number of terms to be printed

You might also like