Option 1: Find Largest of Three Numbers in C Using if-else
#include <stdio.h>
int main() {
// Declare 3 integer type variables
int a, b, c;
printf("Enter three integers: ");
// Take input from the user
scanf("%d %d %d", &a, &b, &c);
// Use if-else to find the largest
if (a >= b && a >= c) {
printf("The largest number is: %d", a);
} else if (b >= a && b >= c) {
printf("The largest number is: %d", b);
} else {
printf("The largest number is: %d", c);
}
return 0;
}Output:
Enter three integers: 5 10 3
The largest number is: 10
Explanation:
Include Library: #include <stdio.h> allows the use of scanf() and printf() functions for input and output.
Declare Variables: int a, b, c; declares three integer variables to store user input.
Take Input: scanf("%d %d %d", &a, &b, &c); reads three integers entered by the user.
if (a >= b && a >= c): Checks if a is the largest number among a, b, and c.
else if (b >= a && b >= c): Checks if b is the largest.
else: If neither a nor b is the largest, then c is the largest.
Print Result: The largest number is printed based on the conditions above.
This C program to find the largest number among three numbers provides a simple yet essential way to learn conditional statements in C.
Time Complexity Analysis
Time Complexity : O(1)
Space Complexity Analysis
Space Complexity : O(1)
Option 2: C Program to Find Largest of Three Numbers Using Functions
#include <stdio.h>
// Function returns the largest number
int findLargest(int a, int b, int c) {
if (a >= b && a >= c)
// a is the largest
return a;
else if (b >= a && b >= c)
// b is the largest
return b;
else
// c is the largest
return c;
}
int main() {
int a, b, c;
printf("Enter three integers: ");
scanf("%d %d %d", &a, &b, &c);
printf("The largest number is: %d", findLargest(a, b, c));
return 0;
}Output:
Enter three integers: 5 12 8
The largest number is: 12Explanation:
This C program to find the largest of three numbers using function encapsulates the comparison logic in findLargest(), making the code modular and reusable.
Time Complexity Analysis
Time Complexity : O(1)
Space Complexity Analysis
Space Complexity : O(1)
Option 3: Greatest of Three Numbers in C Using Ternary Operator
#include <stdio.h>
int main() {
// Declare 3 integer type variables
int a, b, c, largest;
printf("Enter three integers: ");
scanf("%d %d %d", &a, &b, &c);
// Use the ternary operator to find the largest number
largest = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);
printf("The largest number is: %d", largest);
return 0;
}Output:
Enter three integers: 15 6 10
The largest number is: 15
Explanation:
This method finds the greatest of three numbers in C using ternary operator, providing a concise alternative to if-else statements for comparisons.
Time Complexity Analysis
Time Complexity : O(1)
Space Complexity Analysis
Space Complexity : O(1)
Option 4: Find Largest of Three Numbers in C Using Array and Loop
#include <stdio.h>
int main() {
// Declare an array and an integer
// type variable
int nums[3], largest;
printf("Enter three integers: ");
for (int i = 0; i < 3; i++) {
scanf("%d", &nums[i]);
}
largest = nums[0];
// Find the largest element of the array
for (int i = 1; i < 3; i++) {
if (nums[i] > largest) {
largest = nums[i];
}
}
printf("The largest number is: %d", largest);
return 0;
}Output:
Enter three integers: 4 9 3
The largest number is: 9
Explanation:
This C program to find the largest of three numbers stores values in an array and uses a loop to find the maximum, demonstrating the use of arrays and iterative comparison.
Time Complexity Analysis
Time Complexity : O(1)
Space Complexity Analysis
Space Complexity : O(1)
No comments:
Post a Comment