Enter two integers between 1 to 10

3
4
Both numbers are less than 5.
The sum of two numbers is:-1081825676
The product of two numbers is:-1081825680

why are the sum and product so weird?

my code:
#include <stdio.h>
int main()
{
int x;
int y;
int z;
int p;
int count=1;

printf("Enter two integers between 1 to 10\n");
scanf("%i%i", &x, &y);

z=x+y;
p=x*y;

while (count <=4)
{
if((x<10) && (y>10)){
printf("You did not enter a number between 1 and 10.\n");
printf("Please try again.\n");
}
else
{
if (x>=5 && y>=5)
{
printf("Both numbers are greater than 5.\n");
}
if (x<=5 && y<=5)
{
printf("Both numbers are less than 5.\n");
}
if (x>=5 && y<=5)
{
printf("The value of the first %i is greater than or equal to 5.\n");
}
if (x<=5 && y>=5)
{
printf("The value of the second %i is greater than equal to 5.\n");
}

printf("The sum of two numbers is:%i\n",&z);
printf("The product of two numbers is:%i\n",&p);
count+=1;
}
printf("Enter two integers between 1 to 10.\n");
scanf("%i%i", &x, &y);
}
}

User Icon for jen jen answered
16 years ago

#include <stdio.h>

int main()
{
int x;
int y;
int z;
int p;
int count=1;

printf("Enter two integers between 1 to 10\n");
scanf("%i%i", &x, &y);

z=x+y;
p=x*y;

while (count <=4)
{
if((x<10) && (y>10)){
printf("You did not enter a number between 1 and 10.\n");
printf("Please try again.\n");
}
else
{
if (x>=5 && y>=5)
{
printf("Both numbers are greater than 5.\n");
}
if (x<=5 && y<=5)
{
printf("Both numbers are less than 5.\n");
}
if (x>=5 && y<=5)
{
printf("The value of the first %i is greater than or equal to 5.\n");
}
if (x<=5 && y>=5)
{
printf("The value of the second %i is greater than equal to 5.\n");
}

printf("The sum of two numbers is:%i\n",&z);
printf("The product of two numbers is:%i\n",&p);
count+=1;
}
printf("Enter two integers between 1 to 10.\n");
scanf("%i%i", &x, &y);
}
}

User Icon for jen jen answered
16 years ago

it's not working

User Icon for Explain Bot Explain Bot answered
11 months ago

The reason why the sum and product values are so weird in your code is because of the incorrect usage of the `printf` function. In the lines where you are printing the sum and product, you are using the `&` symbol before the variables `z` and `p`.

The `&` symbol is used to get the address of a variable, not its value. So when you use `&z` and `&p` in the `printf` function, it is printing the memory address of the variables instead of their values.

To fix this issue, you need to remove the `&` symbol from these lines:

```c
printf("The sum of two numbers is:%i\n", z);
printf("The product of two numbers is:%i\n", p);
```

With this change, the program will correctly print the sum and product of the entered numbers.

Here is the corrected code:

```c
#include <stdio.h>

int main() {
int x;
int y;
int z;
int p;
int count=1;

printf("Enter two integers between 1 and 10:\n");
scanf("%i%i", &x, &y);

z = x + y;
p = x * y;

while (count <= 4) {
if (x < 1 || x > 10 || y < 1 || y > 10) {
printf("You did not enter numbers between 1 and 10.\n");
printf("Please try again.\n");
} else {
if (x >= 5 && y >= 5) {
printf("Both numbers are greater than or equal to 5.\n");
} else if (x <= 5 && y <= 5) {
printf("Both numbers are less than or equal to 5.\n");
} else if (x >= 5 && y <= 5) {
printf("The value of the first number is greater than or equal to 5.\n");
} else if (x <= 5 && y >= 5) {
printf("The value of the second number is greater than or equal to 5.\n");
}

printf("The sum of two numbers is: %i\n", z);
printf("The product of two numbers is: %i\n", p);
count += 1;
}

printf("Enter two integers between 1 and 10:\n");
scanf("%i%i", &x, &y);
}

return 0;
}
```

This updated code will correctly calculate and print the sum and product of the entered numbers.