
A simple C program to find the maximum of three numbers with an explanation of each part of the code.
#include <stdio.h>
int main() {
int num1, num2, num3, max;
// Asking for user input
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);
// Finding the maximum number
if (num1 > num2) {
if (num1 > num3) {
max = num1;
} else {
max = num3;
}
} else {
if (num2 > num3) {
max = num2;
} else {
max = num3;
}
}
// Printing the maximum number
printf("The maximum number is: %d\n", max);
return 0;
}
#include <stdio.h>This line includes the standard input-output library which is necessary for using printf and scanf functions.
2. Main Function:
int main() {
This is the entry point of the C program where execution begins.
3. Variable Declaration:
int num1, num2, num3, max;
4. User Input:
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);printf is used to display a message asking the user to enter three numbers.scanf is used to readnum1, num2, and num3.5. Finding the Maximum Number:
if (num1 > num2) {
if (num1 > num3) {
max = num1;
} else {
max = num3;
}
} else {
if (num2 > num3) {
max = num2;
} else {
max = num3;
}
}
if statement checks if num1 is greater than num2.if statement checks if num1 is also greater than num3. If true, num1 is the maximum. Otherwise, num3 is the maximum.num1 is not greater than num2, the else block checks if num2 is greater than num3. If true, num2 is the maximum. Otherwise, num3 is the maximum.6. Printing the Maximum Number:
printf("The maximum number is: %d\n", max);
This line prints the maximum number stored in the variable max.
7. Return Statement:
return 0;
This statement indicates that the program executed successfully and returns 0 to the calling process.
C Program (Using Ternary Operator):
#include <stdio.h>
int main() {
int num1, num2, num3, max;
// Asking for user input
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);
// Finding the maximum number using ternary operator
max = (num1 > num2) ? ((num1 > num3) ? num1 : num3) : ((num2 > num3) ? num2 : num3);
// Printing the maximum number
printf("The maximum number is: %d\n", max);
return 0;
}
Explanation:
max = (num1 > num2) ? ((num1 > num3) ? num1 : num3) : ((num2 > num3) ? num2 : num3);
? : is a compact form of an if-else statement.(num1 > num2) ? ((num1 > num3) ? num1 : num3) : ((num2 > num3) ? num2 : num3) evaluates as follows: > If num1 is greater than num2, it further checks if num1 is greater than num3. If true, num1 is assigned to max. Otherwise, num3 is assigned to max.
> If num1 is not greater than num2, it checks if num2 is greater than num3. If true, num2 is assigned to max. Otherwise, num3 is assigned to max.
2. Printing the Maximum Number:
printf("The maximum number is: %d\n", max);This line prints the maximum number stored in the variable max.
#include <stdio.h>
// Function prototype
int findMax(int a, int b, int c);
int main() {
int num1, num2, num3, max;
// Asking for user input
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);
// Calling the function to find the maximum number
max = findMax(num1, num2, num3);
// Printing the maximum number
printf("The maximum number is: %d\n", max);
return 0;
}
// Function to find the maximum of three numbers
int findMax(int a, int b, int c) {
int max = a;
if (b > max) {
max = b;
}
if (c > max) {
max = c;
}
return max;
}
Explanation:
int findMax(int a, int b, int c);This declares the function findMax which takes three integer arguments and returns an integer.
2. Calling the Function:
max = findMax(num1, num2, num3);3. Printing the Maximum Number:
printf("The maximum number is: %d\n", max);
This line prints the maximum number stored in the variable max.
4. Function Definition:
int findMax(int a, int b, int c) {
int max = a;
if (b > max) {
max = b;
}
if (c > max) {
max = c;
}
return max;
}
findMax function which takes three integers a, b, and c.max with the value of a.b is greater than max. max to b.c is greater than max. max to c.max.This approach separates the logic of finding the maximum number into a function, making the code more modular and easier to understand.
C Program (Using fmax Function):
#include <stdio.h>
#include <math.h>
int main() {
int num1, num2, num3;
double max;
// Asking for user input
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);
// Finding the maximum number using fmax function
max = fmax(num1, fmax(num2, num3));
// Printing the maximum number
printf("The maximum number is: %.0f\n", max);
return 0;
}
Explanation:
fmax Function:max = fmax(num1, fmax(num2, num3));fmax function returns the maximum of two numbers.fmax functions to find the maximum of the three numbers. First, we find the maximum of num2 and num3, and then we compare the result with num1 to find the overall maximum2. Printing the Maximum Number:
printf("The maximum number is: %.0f\n", max);
This line prints the maximum number stored in the variable max. The format specifier %.0f is used to print the double value as an integer without any decimal places.
Using the fmax function simplifies the code by leveraging the built-in library function to find the maximum of two numbers and nesting it to handle three numbers.
C Program (Using `qsort` Function):
#include <stdio.h>
#include <stdlib.h>
// Comparator function for qsort
int compare(const void *a, const void *b) {
return (*(int *)b - *(int *)a); // Sort in descending order
}
int main() {
int numbers[3];
// Asking for user input
printf("Enter three numbers: ");
for (int i = 0; i < 3; i++) {
scanf("%d", &numbers[i]);
}
// Sorting the array in descending order
qsort(numbers, 3, sizeof(int), compare);
// The maximum number is the first element after sorting in descending order
int max = numbers[0];
// Printing the maximum number
printf("The maximum number is: %d\n", max);
return 0;
}
int compare(const void *a, const void *b) {
return (*(int *)b - *(int *)a); // Sort in descending order
}
compare function is used by qsort to determine the order of elements.*a from *b.2. User Input:
printf("Enter three numbers: ");
for (int i = 0; i < 3; i++) {
scanf("%d", &numbers[i]);
}printf is used to display a message asking the user to enter three numbers.for loop is used to read the input numbers and store them in the numbers array.3. Sorting the Array
qsort(numbers, 3, sizeof(int), compare);
`qsort` function sorts the numbers array.Using qsort to sort the array in descending order allows us to easily find the maximum number as the first element in the sorted array. This approach is flexible and can handle larger sets of numbers with minimal modification.