I'm writing a program to check a number and I have a problem; This program should print the 2nd big number of the input. The code is:
printf("Please enter number\n");
unsigned long long N;
scanf("%llu", &N);
int d1 = N % 10, d2 = -1;
while (N != 0) {
if (N % 10 > d1) {
d2 = d1;
d1 = N % 10;
}
if (N % 10 < d1 && N % 10 > d2)
d2 = N % 10;
N /= 10;
}
if (d2 != -1)
printf("2nd big digit is %d\n", d2);
else
printf("There's no 2nd big digit!\n");
When getting an input of 123 I should get d2 = 2 in the end but this if: if (N % 10 < d1 && N % 10 > d2)
is not working, but when using this code:
printf("Please enter number\n");
unsigned long long N;
scanf("%llu", &N);
int d1 = N % 10, d2 = -1, s;
while (N != 0) {
if (N % 10 > d1) {
d2 = d1;
d1 = N % 10;
}
s = N % 10;
if (s < d1 && s > d2)
d2 = N % 10;
N /= 10;
}
if (d2 != -1)
printf("2nd big digit is %d\n", d2);
else
printf("There's no 2nd big digit!\n");
It's working as intended, but s is N % 10 so I don't understand why the second if is working and the first one isn't, if you can explain I would really appreciate that.
Sorry for my bad English :(