2
I believe the logic of this programme is right, but the result is wrong.
Make a program that receives two X and Y numbers, being X < Y. Calculate and show:
- the sum of the even numbers in that range of numbers, including the numbers entered;
- the multiplication of the odd numbers in that range, including those typed
Code:
#include <stdio.h>
void main()
{
int x = 0, y = 0, somaPares = 0, multiImpares = 0;
scanf("%d", &x);
scanf("%d", &y);
somaPares = (x+y);
multiImpares = (x*y);
printf("%d\n", somaPares);
printf("%d\n", multiImpares);
while (x<y)
{
x++;
if(x%2==0){
somaPares = somaPares + x;
}
else {
multiImpares = multiImpares*x;
}
}
printf("A soma de X e Y mais os números pares entre eles é: %d\n", &somaPares);
printf("A multiplicação de X e Y mais os números ímpares entre eles é: %d", &multiImpares);
}
The input I am putting is: 5 and 3, 8 being the sum and 15 the result, in the first two printfs
is coming right now in the last two printfs
is returning me respectively, 2752260 and 2752256.
There is a problem there, in the statement says, that x is less than y
– Leonardo
I don’t understand. Do you see any problem? What would it be?
– Maniero
In the question it says "being X < Y", so the code could not allow an X larger than a Y, Alexandre was also wrong to test with 5 and 3.. But this is a matter of detail and organization rsrs
– Leonardo
Yeah, I saw there’s some weird stuff in the code, but his question was about the wrong impression of the value. Someone might have understood that mine is wrong because I got a negative that I don’t know why.
– Maniero
I understand, with me it happened the same maybe just because I switched the
while
for, to have a more streamlined code.. His mistake was the same as mine yesterday rsrs– Leonardo