I want to calculate greatest of given three integer numbers in C programming without using any if
/else
condition or switch
case. For example in this way.
#include <stdio.h>
#include <stdlib.h>
int main() {
int num1, num2, max;
scanf("%d %d", &num1, &num2);
max = (num1 num2 abs(num1 - num2)) /2;
printf("%d\n", max);
return 0;
}
Here is my code to calculate the max of given two integer numbers.
CodePudding user response:
You can call the max function twice:
#include <stdio.h>
#include <stdlib.h>
int main() {
int num1, num2, num3, max;
scanf("%d %d %d", &num1, &num2, &num3);
max = (num1 num2 abs(num1-num2))/2;
max = (max num3 abs(max-num3))/2;
printf("%d\n", max);
return 0;
}
CodePudding user response:
You can use ternary expression as well:
#include <stdio.h>
#define max2(a,b) (((a) > (b)) ? (a) : (b))
#define max3(a,b,c) max2(max2((a), (b)), (c))
int main() {
printf("%d\n", max3(10, -10, 20));
return 0;
}
CodePudding user response:
Your question makes no sense: the meaning of abs(x)
is (more or less):
int abs(int x)
{
if (x>0)
return x;
else return -x;
}
So the simple usage of abs()
implies an if
-clause.
CodePudding user response:
Instead of if
/else
statements, you can use the ternary operator:
#include <stdio.h>
int main() {
int num1, num2, num3, max;
if (scanf("%d%d%d", &num1, &num2, &num3) == 3) {
max = (num1 < num2) ? num1 : num2;
max = (max < num3) ? max : num3;
printf("%d\n", max);
}
return 0;
}
If the ternary operator is not allowed, you can use comparison operators, which may generate branchless code anyway:
#include <stdio.h>
int main() {
int num1, num2, num3, max;
if (scanf("%d%d%d", &num1, &num2, &num3) == 3) {
max = (num1 >= num2) * num1 (num1 < num2) * num2;
max = (max >= num2) * max (max < num2) * num2;
printf("%d\n", max);
}
return 0;
}
Note that you cannot use num1 num2
nor num1 - num2
, not even -num1
nor num2
because there operations may cause arithmetic overflow with undefined behavior.
Also note that you can abuse the rules by replacing if
statements with while
or for
statements:
#include <stdio.h>
int main() {
int num1, num2, num3;
if (scanf("%d%d%d", &num1, &num2, &num3) == 3) {
for (max = num1; max < num2; max = num2)
continue;
while (max < num3)
max = num3;
printf("%d\n", max);
}
return 0;
}
CodePudding user response:
without using any if/else condition or switch case
#define m(a,b,c)a>b?a>c?a:c:b>c?b:c
35 bytes, I win.
CodePudding user response:
You may use something similar to this:
#include <stdio.h>
int a,b,c, max_num;
int maxof3(int num1, int num2, int num3)
{
return(max(max(num1, num2), num3));
}
int main()
{
scanf("%d %d %d", &a, &b, &c);
max_num = maxof3(a, b, c);
printf("%d\n", max_num);
return 0;
}
CodePudding user response:
Since you asked different ways, and not efficient ways you can sort them using the qsort
library function and then return the last element.
#include <stdio.h>
#include <stdlib.h>
int cmp (const void * a, const void * b) { return ( *(int*)a - *(int*)b ); }
int main (void) {
int arr[3];
scanf("%d %d %d", &arr[0], &arr[1], &arr[2]); //ignoring valid input check, assuming valid input
qsort(arr, 3, sizeof(int), cmp);
printf("max: %d",arr[2]);
return 0 ;
}