You're online now.

Hurray! you are online now.

Write a C program to find the maximum of three numbers

// Write a c program to find the maximum number given of three number from the user.
    #include <stdio.h>
    int main() {
        int n1, n2, n3, max;
        printf("Enter the three numbers : ");
        scanf("%d %d %d", &n1, &n2, &n3);
        if(n1 > n2 && n1 > n3){
            max = n1;
        }
        else if(n2 > n1 && n2 > n3){
            max = n2;
        }
        else{
            max = n3;
        }
        printf("\n Maximum number : %d", max);
    	return 0;
    }

The code prompts or message the user to input three integers, reads the input using scanf() funtion, and then find the maximum of the three number using a series of if-else statements. Finally, the code outputs the maximum value to the console using printf() funciton.

#include <stdio.h>
    

The above line includes the statndard input/output library in the code.

int main() {
    

This is the main function of the program. The main function is entry point of the main program in C.

int n1, n2, n3, max;
    

There are declares four integer variables named ‘n1’, ‘n2’, ‘n3’, and ‘max’.

printf("Enter the three numbers : ");
    

Display the prompt or message to the user to enter the three numbers.

scanf("%d %d %d", &n1, &n2, &n3);
    

Read the user input values for ‘n1’, ‘n2’, and ‘n3’ using the scanf() function.

if(n1 > n2 && n1 > n3){
        max = n1;
    }
    

These lines checks if ‘n1’ is greater than both ‘n2’ and ‘n3’. If it is, then ‘max’ is set to ‘n1’.

else if(n2 > n1 && n2 > n3){
        max = n2;
    }
    

These lines checks if ‘n2’ is greater than both ‘n1’ and ‘n3’. If it is, then ‘max’ is set to ‘n2’. This will be execute when the if condition is false.

else{
        max = n3;
    }
    

If neither of the above conditions are true, then ‘n3’ must be the largest/maximum number, so ‘max’ is set to ‘n3’.

printf("\n Maximum number : %d", max);
    

Prints the maximum value of the three numbers to the terminal suing the printf() function.

    return 0;
    }
    

Program is successfully executed and returns the value 0.

Algorithm

  1. Start
  2. Declare variables n1, n2, n3, and max as integers.
  3. Prompt the user to enter three numbers.
  4. Read the input values for n1, n2, and n2 using scanf() function.
  5. If n1 is greater than both n2 and n3, set max to n1.
  6. Else if n2 is greater than both n1 and n3, set max to n2.
  7. Else, set max to n3.
  8. Print the value of max to the terminal/console using printf() function.
  9. End

Pseudocode

// pseudocode to find the maximum of three numbers.
    1. Start
    2. Declare n1, n2, n3, and max as integers
    3. Display "Enter the three numbers : "
    4. Read input for n1, n2, and n3 using scanf()
    5. If n1 > n2 AND n1 > n3, then
           max = n1
       else if n2 > n1 AND n2 > n3, then
           max = n2
       else
           max = n3
    6. Display "Maximum number : ", max
    7. End
    
🖤 0
Buy me coffee ☕

Comments

Oops!

No comments here...