Program to find largest number among three numbers

Program to find largest number among three numbers

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

Happy number in java

Celsius to Fahrenheit In Java – Fahrenheit To Celsius In Java

Magic number | Magic number in java

Program to print Floyd's triangle in Java

Program to print Floyd's triangle in Java

 

Program to print Floyd's triangle in Java

Program to print Floyd's triangle in Java

The Floyd’s triangle is a triangle of natural consecutive numbers and it is starting with a 1 to n. it is named after Robert Floyd an American computer scientist.

It looks like the following pattern 

1

2 3

4 5 6

7 8 9 10

11 12 113 14 15

import java.util.*;

public class FloydTriangle

{

   public static void main(String args[])

   {

       int rows;  // input number of rows

       int i, j;  // i and j used as loop

       int k = 1; // k generate the floyd's Triangle

       Scanner sc = new Scanner(System.in);

       System.out.println("Enter number of rows to generate Floyd's Triangle:");

       rows = sc.nextInt();

       // loop

       for(i=1; i<=rows; i++)

       {

           for(j=1; j<=i; j++)

           {

               // print the values of triangle

               System.out.print(k + " ");

               k++;

            }

            System.out.println();

        }

    }

}

Output:

Enter number of rows to generate Floyd's Triangle: 5

1

2 3

4 5 6

7 8 9 10

11 12 13 14 15

Here another output with 10 rows

Enter the number of rows to generate Floyd's Triangle: 10

1

2 3

4 5 6

7 8 9 10

11 12 13 14 15

16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31 32 33 34 35 36

37 38 39 40 41 42 43 44 45

46 47 48 49 50 51 52 53 54 55

 

We can also print with 5 rows Floyd’s triangle as

15

14 13

12 11 10

9   8    7   6

5   4   3   2    1

By just changing k=1 to k=15 and in the loop k++ to k—as below

for(i=1; i<=rows; i++)

       {

           for(j=1; j<=i; j++)

           {

               // print the values of triangle

               System.out.print(k + " ");

               k--;

            }

            System.out.println();

        }

 

More Java programs 

fibonacci Series in Java with recursive method

fibonacci Series in Java with recursive method

 

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

 A class RecursionFibonacciSeries has been defined to generate the Fibonacci series up to an nth number. Some of the members of the class are given below: 

  • Class Name : RecursionFibonacciSeries
  • Data Members/ instance variables: f1, f2, fib, number;
  • Constructor: RecursionFibonacciSeries ()
  • Member method: input(), fibo(int n), printFibseries()

 In the following java program, input is taken from the input() method.

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