Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Greatest of Two Numbers in C

Last Updated on May 12, 2023 by Prepbytes

This article discusses a C program that finds the greater of both of two provided numbers. Two numbers, num1 and num2, are given. The objective is to determine which of the two numbers is greater.

Approaches to Find Greatest of Two Numbers in C

You are given two integers and must find the greater of the two using C. There are two approaches to solving this problem.

  • Making use of if-else statements
  • The use of ternary operators

Let us now go over these approaches in greater detail.

Making Use of If-else Statement

If-else statements can be used to solve this problem. Let’s discuss the algorithm.

Algorithm to Find the Greatest of Two Numbers in C using If-else Statement

A and B are two numbers.

  • If A == B
    • Both are equal.
  • Otherwise, if A > B
    • Print A is greater.
  • Otherwise, B has to be the greater
    • Print B is greater.

Code Implementation of Finding the Greatest of Two Numbers in C using If-else Statement

#include<stdio.h>
int main ()
{
  int num1, num2;
  num1=12,num2=13;

  if (num1 == num2)
    printf("both are equal");
  else if (num1 > num2) 
    printf("%d is greater", num1);
  else
    printf("%d is greater", num2);

  return 0;
}

Output :

13 is greater 

Making Use of the Ternary Operator

A ternary operator is used when an operator is applied to three variables, or operands. When longer conditional statements such as if and else exist, programmers use the ternary operator to make decisions. Let’s look at how to use the ternary operator to find the greatest of two numbers in C.

Algorithm to Find the Greatest of Two Numbers in C using a Ternary Operator

For the pairs A and B:

  • If A == B
    • Print both are equal
    1. Else, use ternary operator (temp = A > B? A : B)
      • Print temp value is greater

Code Implementation of Finding the Greatest of Two Numbers in C using the Ternary Operator

#include <stdio.h>
int main ()
{
    int num1, num2, temp;
    
    num1=20,num2=30;
    if(num1 == num2)
        printf("Both are Equal\n");
    else{
        temp = num1 > num2? num1 : num2;
        printf("%d is largest",temp);
    }

  return 0;
}

Output:

30 is the largest

Conclusion
The article describes many methods for determining the greatest of two integers in the C programming language. Two approaches are presented: the first uses if-else expressions, while the second employs the ternary operator. The if-else approach entails creating a basic algorithm that compares two integers to decide which is bigger. The code is then implemented in C, with the bigger number as the output. The ternary operator, which is employed when an operator is applied to three operands, is utilized in the second technique. The procedure compares the two integers using the ternary operator, and the code is then created to produce the bigger number. Both techniques are contrasted and shown using code written in C. The page explains each method in depth, including their algorithms and code implementation, making it useful for novices in C programming.

Frequently Asked Questions (FAQs)

Q1. What is the C function for the greatest of two numbers?
Ans. The max() function is used to find the greatest difference between two numbers. Second, we must find the greatest possible difference between two numbers.

Q2. In C, what is Max ()?
Ans. The greater of two values is returned by the max() macro. The max() macro is only available in C programs.

Q3. How can I use a function to find a larger number in C?
Ans. To find the largest of those two numbers, we use the ternary operator inside the function biggest. The function biggest returns the greater of the two numbers to the calling method/function.

Leave a Reply

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