Reputation: 9
when I give maximum values to a and b the code works perfectly but when the maximum is given input to c or d, the code doesn't works. Can anyone help me with this one?
I've already visited websites and done my research but I couldn't find anything.
#include<stdio.h>
void main(void) {
int a, b, c, d;
scanf("%d %d %d %d", &a, &b, &c, &d);
printf("the enter no.: is %d %d %d %d\n", a, b, c, d);
if(a > b) {
if(a > c) {
if(a > d) {
printf("%d is greater",a);
}
}
} else if(b > a) {
if(b > c) {
if(b > d) {
printf("%d is greater", b);
}
}
} else if(c > a) {
if(c > b) {
if(c > d) {
printf("%d is greater", c);
}
}
} else if(d > a) {
if(d > c) {
if(d > b) {
printf("%d is greater", d);
}
}
}
}
The output should be same for all the variables but still a and b is giving outputs as expected and c and d doesn't.
Upvotes: 0
Views: 102
Reputation: 32596
your way to do is wrong, for instance with the input 2 1 4 3 if(a>b)
is true but if(a>c)
is false so you do nothing more and you do not detect the max number
out of your problem I also encourage you to check 4 valid int was enter checking scanf("%d %d %d %d",&a,&b,&c,&d)
returns 4
a proposal :
#include<stdio.h>
int main()
{
int a,b,c,d;
if (scanf("%d %d %d %d",&a,&b,&c,&d) != 4)
fprintf(stderr, "error while entering the 4 values\n");
else {
printf("the enter no.: is %d %d %d %d\n",a,b,c,d);
int max = a;
if (b > max)
max = b;
if (c > max)
max = c;
if (d > max)
max = d;
printf("the greater is %d\n", max);
}
return 0;
}
compilation and execution :
pi@raspberrypi:/tmp $ gcc -pedantic -Wall -Wextra m.c
pi@raspberrypi:/tmp $ ./a.out
1 2 4 3
the enter no.: is 1 2 4 3
the greater is 4
Of course there is an other way to do without memorizing all the numbers
Upvotes: 4
Reputation: 753
#include <stdio.h>
int main() {
int a,b,c,d;
scanf("%d %d %d %d",&a,&b,&c,&d);
printf("the enter no.: is %d %d %d %d\n",a,b,c,d);
if(a > b && a > c && a > d)
printf("%d is greater\n", a);
else if(b > a && b > c && b > d)
printf("%d is greater\n", b);
else if(c > a && c > b && c > d)
printf("%d is greater\n", c);
else
printf("%d is greater\n", d);
return 0;
}
Upvotes: 2
Reputation: 63
It is the order of your program which is causing the problem. If you put c and d as maximum but if b>a or a>b it will enter the first two if-statements and it will not reach the two last statements. Try to put c or d as maximum and choose two numbers such that a=b. This should work.
Upvotes: 0
Reputation: 339
Your conditions depends on one another, but they shouldn't be.
Meaning, the second high level condition (else if(b>a)) would be checked only if a is not greater then b.
For example, given the input of:
a = 1
b = 2
c = 3
d = 4
The second high level condition would be true (b is greater than a), and so, no other condition would be checked at all (the condition if d is the greatest would not be checked, and the line won't be printed).
To solve this issue, you can ether change the conditions to make every one of them in a single if (with && (and) between them), or you can change your code to always use "if"s, instead of your "else if".
Upvotes: 1