This blog is under construction

Monday 10 June 2013

C program to find the greatest of two numbers without using if statement

Write a C program to find the greatest of two numbers without using if statement.



  /* C program to find the greates of two nos without if statement */
  #include <stdio.h>
  int main(){
        int num1, num2, max;
        printf("Enter num1 value:");
        scanf("%d", &num1);
        printf("Enter num2 value:");
        scanf("%d", &num2);
        printf("\nMethod 1:\n");
        max = num1 > num2 ? num1 : num2;
        printf("%d is the Maximum\n", max);
        printf("\nMethod 2:\n");
        max = ((num1 + num2) + abs(num1 - num2))/2;
        printf("%d is the Maximum\n", max);
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter num1 value:100
  Enter num2 value:98

  Method 1:
  100 is the Maximum

  Method 2:
  100 is the Maximum



No comments:

Post a Comment