Java Programs
Java Programs
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
{
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
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
{
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
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