Reputation:
So i am trying to write this program but i get 10 no matter what the input is. My function seems correct to me so is it a scanf issue?
Write a program to input 10 integers and decide how many of them satisfy the following rule: abcd = (ab + cd)2 e.g. 3025=(30+25) Use a function that receives an integer parameter returns 1 if it satisfies the above rule, returns 0 otherwise.
int check(int n);
int main(void)
{
int n, i, j = 0;
printf("Input 10 integers: ");
for (i = 0; i < 10; i++)
{
scanf("%d", &n);
if (check(n) == 1)
{
j++;
}
}
printf("%d\n", j);
}
int check(int x)
{
if (((x / 100) + (x % 100)) * ((x / 100) + (x % 100)))
{
return 1;
}
else
{
return 0;
}
}
Upvotes: 0
Views: 51
Reputation: 11047
The issue I think is the check
function,
if (((x / 100) + (x % 100)) * ((x / 100) + (x % 100))) // <---- anything not zero will be true
{
return 1;
}
The expression inside if
will convert any integer which is not zero to true. The expression as it is written is if (x * x)
which only false if x == 0
.
Upvotes: 1