5

Is there any library function in c for finding out the largest number between two numbers?

RottieRango
  • 105
  • 1
  • 1
  • 5

3 Answers3

13

You can do

#define new_max(x,y) (((x) >= (y)) ? (x) : (y))
#define new_min(x,y) (((x) <= (y)) ? (x) : (y))

valter

0

You can easily write your own function using comparison operators such as >, <, >=, <=, and ==.

Here is an example from this site:

#include <stdio.h>

int main()
{
  int a, b;
  scanf("%d %d", &a, &b);//get two int values from user

  if (a < b)//is a less than b?
    printf("%d is less than %d\n", a, b);

  if (a == b)//is a equal to b?
    printf("%d is equal to %d\n", a, b);

  if (a > b)//is a greater than b?
    printf("%d is greater than %d\n", a, b);

  return 0;
}

You can use this knowledge to do a simple function that follows this pseudo code:

//get a and b int variable
//is a greater than or equal to b?
    //return a
//else
    //return b

Some other useful resources:

http://c.comsci.us/tutorial/icomp.html

http://www.cyberciti.biz/faq/if-else-statement-in-c-program/

-1

Here is a example of using a IF statement to determine the largest of 3 numbers. Like Ahmed said.

/* C program to find largest number using if statement only */

#include <stdio.h>
int main(){
      float a, b, c;
      printf("Enter three numbers: ");
      scanf("%f %f %f", &a, &b, &c);
      if(a>=b && a>=c)
         printf("Largest number = %.2f", a);
      if(b>=a && b>=c)
         printf("Largest number = %.2f", b);
      if(c>=a && c>=b)
         printf("Largest number = %.2f", c);
      return 0;
}
Steve Church
  • 344
  • 1
  • 3
  • 12