Program to find largest number among three numbers
Java program to display the big number where the user enters three numbers
import java.util.*;
public class BigNumberPrint
{
public static void main(String[] args)
{
int num1; // First Number
int num2; // Second Number
int
num3; // Third
Number
int big; // Store the largest number.
// Scanner Object sc.
Scanner sc = new Scanner(System.in);
// Input the First number from the user.
System.out.print("Enter first
number: ");
num1 = sc.nextInt();
// Input the Second number from the user.
System.out.print("Enter second
number: ");
num2 = sc.nextInt();
// Input the Third number from the user.
System.out.print("Enter third
number: ");
num3 = sc.nextInt();
// Find the largest number among three
number.
if (num1 > num2 && num1 >
num3)
{
big = num1;
}
else if (num2 > num3)
{
big = num2;
}
else
{
big = num3;
}
// Display a big number.
System.out.println("Big number:
" + big);
}
}
Output
Enter first number: 5
Enter second number: 8
Enter third number: 2
Largest number: 8
Program to find the largest number among three numbers
import
java.util.*;
public class
LargeNumberPrint
{
public static void main(String[] args)
{
int num1; // Input First Number
int num2; // Input Second Number
int num3; // Input Third Number
int large; // Store the largest number.
// Scanner Object sc.
Scanner sc = new Scanner(System.in);
// Input the First number from the user.
System.out.print("Enter first
number: ");
num1 = sc.nextInt();
// Input Second number from the user.
System.out.print("Enter second
number: ");
num2 = sc.nextInt();
// Input Third number from the user.
System.out.print("Enter third
number: ");
num3 = sc.nextInt();
// Find the largest number among three
numbers.
if(num1 >= num2)
{
if(num1 >= num3)
large = num1;
else
large = num3;
}
else
{
if(num2 >= num3)
large = num2;
else
large = num3;
}
// Print the large number.
System.out.println("Largest
number: " + large);
}
}
Output
Enter first number: 6
Enter second number: 9
Enter third number: 2
Largest number: 9
More Java Program
Celsius to Fahrenheit In Java – Fahrenheit To Celsius In Java
Magic number | Magic number in java
fibonacci Series in Java with recursive method
Recursive method
When a function called
by itself directly or indirectly is known as a recursive method or function. The function repeats itself several times and gives outputting the result and the end of
each repetition.
Fibonacci Series in Java with recursive method
- Class Name : RecursionFibonacciSeries
- Data Members/ instance variables: f1, f2, fib, number;
- Constructor: RecursionFibonacciSeries ()
- Member method: input(), fibo(int n), printFibseries()
Then it passes to as int
fibo(int n) where the Fibonacci series generate recursively.
After it passes to printFibseries()
method for printing of the series.
import java.util.*;
class
RecursionFibonacciSeries
{
// instance variables
int f1,f2,fib,number;
int i; // for iteration
RecursionFibonacciSeries() //Constructor
method
{
f1=0;
f2=1;
fib=0;
number=0;
}
// enter nth value for the series
void input()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number
: ");
number=sc.nextInt();
}
// Recursive method generating the 'nth'
term of Fibonacci Series
int fibo(int n)
{
if(n <= 1)
return f1;
else if(n == 2)
return f2;
else
// recursion call
return (fibo(n-1) + fibo(n-2));
}
void printFibseries() //Function generating
all the Fibonacci Series numbers upto 'n' terms
{
System.out.println("The Fibonacci
Series is:");
for(i=1; i<=number; i++)
{
fib=fibo(i);
// printing fibonacci series
System.out.print(fib + " ");
}
}
// main method
public static void main(String args[])
{
// object creation
RecursionFibonacciSeries ob=new
RecursionFibonacciSeries();
// calling method
ob.input();
ob.printFibseries();
} // end of main method
} // end of class
Output:
Enter the number : 15
The Fibonacci Series is:
0 1
1 2 3
5 8 13
21 34 55
89 144 233
377
More Java programs
Java Program to Find All Roots of a Quadratic Equation
Quadratic Equation
The form of the
quadratic equation is ax² + bx + c = 0 where a, b and c are real and a !=0, x
is an unknown variable. The nature of roots is determined by the discriminant.
The formula to find the
roots of the quadratic is
Java Program to Find all Roots of a Quadratic equation
discriminant = (b * b) - (4 * a * c). After check
1. if discriminant > 0 then
The roots are unequal and irrational
2. if discriminant = 0 then
The roots are equal and rational
3 otherwise
The roots are imaginary
import java.util.*;
class QuadraticEquation
{
public static void main(String args[])
{
double a, b, c;
double root, root1, root2;
double disc; // discriminant
Scanner sc=new Scanner(System.in);
System.out.println("Enter value of
a, b, c");
a= sc.nextInt();
b= sc.nextInt();
c= sc.nextInt();
// Calculate
disc = (b * b) - (4 * a * c);
if ( disc > 0 )
{
root1 = (- b + Math.sqrt(disc)) /
(2 * a);
root2 = (- b - Math.sqrt(disc)) /
(2 * a);
System.out.println("The roots are
unequal and irrational " + root1 + " and " + root2);
}
else if ( disc == 0 )
{
root = - b / (2 * a);
System.out.println("The roots
are equal and rational " + root + " and " + root );
}
else if ( disc < 0 )
{
System.out.println("The roots
are imaginary");
}
} // function is over
} // class is over
Output
Enter
value of a, b, c
20
30
20
The
roots are imaginary
Enter
value of a, b, c
2
7
3
The
roots are unequal and irrational -0.5 and -3.0
Java program to find the Roots of Quadratic equation with method
class
QuadraticEquationWithMethod
{
double a, b, c;
double disc; // discriminant
double root, root1, root2;
// quadratic equation method
void quadraticEquation(double x, double y,
double z)
{
a = x;
b = y;
c = z;
disc = (b * b) - (4 * a * c);
if ( disc > 0 )
{
root1 = (- b + Math.sqrt(disc)) /
(2 * a);
root2 = (- b - Math.sqrt(disc)) /
(2 * a);
System.out.println("The roots
are unequal and irrational " + root1 + " and " + root2);
}
else if ( disc == 0 )
{
root = - b / (2 * a);
root1 = root;
System.out.println("The roots
are equal and rational " + root + " and " + root1 );
}
else if ( disc < 0 )
{
System.out.println("The roots
are imaginary");
}
}
// main method
public static void main(String args[])
{
double a, b, c;
Scanner sc=new Scanner(System.in);
System.out.println("Enter value of
a, b, c");
a= sc.nextInt();
b= sc.nextInt();
c= sc.nextInt();
QuadraticEquationWithMethod ob=new
QuadraticEquationWithMethod();
// calling method
ob.quadraticEquation(a, b, c);
} // main method is over
} //
class is over
Output:
Enter value of a, b, c
1
1
1
The
roots are imaginary
Enter
value of a, b, c
1
6
11
The
roots are imaginary
Enter
value of a, b, c
1
7
12
The
roots are unequal and irrational -3.0 and -4.0
