FOR FREE MATERIALS

largest number between two or three numbers

Back to Programming

Description

The program is written here to design a menu-driven program to find the largest number between two or three numbers. According to the choice of the user, the inputs are taken, and then the largest number is found and printed.

For example, if the two numbers are 5 and 8, then the largest number is 8.

If the three numbers are 5, 2, and 4 then the largest number is 5.

Algorithm

INPUT: Two or Three numbers according to the user choice.

OUTPUT: The largest number among two or three numbers.

PROCESS:
Step 1: [function to find the largest between two numbers]
               
               Read a, b [two numbers]
               If a>b then
                              Print “a is largest”
               Else
                              Print “b is largest”
               [End of ‘if’]

Step 2: [Function to find the largest between three numbers]

               Read a, b, c [three numbers]
               If a>b and a>c then
                              Print “a is largest"
               else if b>a and b>c then
                              Print “b is largest"
               else
                              Print “c is largest"
               [End of ‘if’]

Step 3: [Main function]
             Print “1. Find the largest between two numbers 
                        2. Find the largest between three numbers
                       Enter your choice”
             Read ch [choice]
             If ch=1 then
                        Call the function to find the largest number between two numbers
             Else If ch=2 then
                        Call the function to find the largest number between three numbers
             Else
                        Print “Wrong Choice”
             [End of ‘if’]

[End of ‘main’ function]

Step 4: Stop.

Code

TIME COMPLEXITY:

               if(a>b)------------------------------------------- O(1)

                       printf("%d is largest",a);

               else---------------------------------------------- O(1)

                       printf("%d is largest",b);

               if(a>b&&a>c)---------------------------------- O(1)

                        printf("%d is largest",a);

               else if(b>a&&b>c)---------------------------- O(1)

                        printf("%d is largest",b);

               else----------------------------------------------- O(1)

                        printf("%d is largest",c);

Here, O(1) means constant time. Each ‘if’ statement and ‘else’ statement takes a constant time to execute. Therefore, the time complexity of this program is O(1).

 

SPACE COMPLEXITY:

The space complexity of this program is O(1) as it requires a constant number of spaces for any given input.