C (ANSI)
#include <stdio.h>
// Function to find the maximum of two numbers
int max(int num1, int num2) {
return (num1 > num2) ? num1 : num2;
}
// Function to find the maximum of three numbers
int maxOfThree(int num1, int num2, int num3) {
return max(max(num1, num2), num3);
}
// Main function
int main() {
// Declare variables
int num1, num2, num3;
// Input three numbers from the user
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);
// Find and print the maximum of the three numbers
printf("Maximum number is: %d", maxOfThree(num1, num2, num3));
// Return 0 to indicate successful completion
return 0;
}
Explanation:
Include Header File:
#include <stdio.h>
includes the standard input-output library for using functions likeprintf
andscanf
.Function Declaration:
int max(int num1, int num2)
declares a function namedmax
that takes two integer parameters and returns an integer. It is used to find the maximum of two numbers.maxOfThree Function Declaration:
int maxOfThree(int num1, int num2, int num3)
declares a function namedmaxOfThree
that takes three integer parameters and returns an integer. It utilizes themax
function to find the maximum of three numbers.Main Function:
int main()
is the main function where the program execution starts.Variable Declaration:
int num1, num2, num3;
declares three integer variables to store user-input numbers.User Input:
printf("Enter three numbers: "); scanf("%d %d %d", &num1, &num2, &num3);
prompts the user to enter three numbers, andscanf
is used to read and store these values.Function Call:
maxOfThree(num1, num2, num3)
calls themaxOfThree
function with the user-input numbers as arguments.Print Result:
printf("Maximum number is: %d", maxOfThree(num1, num2, num3));
prints the result, which is the maximum of the three numbers.Return Statement:
return 0;
indicates the successful completion of the program.