C Program to Find Largest of Two Numbers

C Program to Find Largest of Two Numbers

C program to find the largest or greatest of two numbers; Through this tutorial, we will learn how to find the largest of two numbers in the c program using if statement, condition operator, switch case, and ternary operator.

Programs and Algorithm to Find Largest of Two Numbers in C

  • Algorithm to Find Largest of Two Numbers
  • C Program to Find Largest of Two Numbers using Else If Statement
  • C Program to Find Largest of Two Numbers using Conditional Operator
  • C Program to Find Largest of Two Numbers using Switch Case
  • C Program to Find Largest of Two Numbers using Ternary Operator

Algorithm to Find Largest of Two Numbers

Use the following algorithm to write a c program to find largest of two number; as follows:

  1. Start program
  2. Read the two integer values in program.
  3. Check if num1 is greater than num2.
  4. If true, then print ‘num1’ as the greatest number.
  5. If false, then print ‘num2’ as the greatest number.
  6. End program

C Program to Find Largest of Two Numbers using Else If Statement

/* C Program to Find Largest of Two numbers */ 
   
#include <stdio.h>  
   
int main() {  
    int a, b;  
    printf("Please Enter Two different values :- ");  
    scanf("%d %d", &a, &b);  
    
    if(a > b) 
    {
        printf("%d is Largest\n", a);          
    } 
    else if (b > a)
    { 
        printf("%d is Largest\n", b);  
    } 
    else 
    {
	printf("Both are Equal\n");
    }
   
    return 0;  
}

The output of the above c program; as follows:

Please Enter Two different values :- 10 25
25 is Largest

C Program to Find Largest of Two Numbers using Conditional Operator

/* C Program to Find Largest of Two numbers */ 
   
#include <stdio.h>  
   
int main() {  
    int a, b, largest;
    printf("Please Enter Two Different Values :- ");  
    scanf("%d %d", &a, &b);  
    
    if(a == b)
    {
        printf("Both are Equal\n");
    }
    else { 
        largest = (a > b) ? a : b;
        printf("%d is Largest\n", largest);
    }
    return 0;  
}

The output of the above c program; as follows:

Please Enter Two Different Values :- 100 2
100 is Largest

C Program to Find Largest of Two Numbers using Switch Case

/* C Program to Find Largest of Two numbers */ 
   
#include <stdio.h>  
   
int main() {  
    int a, b, largest;
    printf("Please Enter Two Values :- ");  
    scanf("%d %d", &a, &b);  
   
    switch(a > b) 
    {     
        case 1: printf("%d is Largest", a);  
                break;  
        case 2: printf("%d is Largest", b);  
                break;  
    }  
    return 0;  
}

The output of the above c program; as follows:

Please Enter Two Values :- 63 36
63 is Largest

C Program to Find Largest of Two Numbers using Ternary Operator

#include <stdio.h>
int main()
{
    int num1, num2;
    // Ask user to enter the two numbers
    printf("Please Enter Two different values :- ");
    // Read two numbers from the user
    scanf("%d %d", &num1, &num2);
    (num1 >= num2)?((num1 ==num2)?printf("Both numbers are equal"):printf("%d is Largest\n", num1)):printf("%d is Largest\n", num2);
    return 0;
}

The output of the above c program; as follows:

Please Enter Two different values :- 40 30
40 is Largest

Recommended C Programs

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *