Program that sums/multiplies 2 numbers and what’s between them and prints

Asked

Viewed 6,996 times

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.

2 answers

4


Just pull the operator out & us printf() end. They are having the address of the variables printed and not their content printed. If you want the content, use the name of the simple variable. Maybe you got confused with the scanf() which requires the operator to pass by reference and the variable to receive the new value.

#include <stdio.h>

int main() {
    int x = 0, y = 0;
    scanf("%d", &x);
    scanf("%d", &y);
    int somaPares = x + y;
    int multiImpares = x * y;
    printf("%d\n", somaPares);
    printf("%d\n", multiImpares);
    while (x < y) {
        x++;
        if (x % 2 == 0) somaPares += x;
        else 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);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Maybe the algorithm is also wrong but then I can’t be sure. I answered what was asked and what is clear in the question.

  • There is a problem there, in the statement says, that x is less than y

  • I don’t understand. Do you see any problem? What would it be?

  • 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

  • 1

    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.

  • 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

0

The while can wrong.. And the & us printf With for :

#include <stdio.h>

main(void)
{
    int x = 0, y = 0, cont, pares = 0, impares = 1;
    while(x >= y)
    { // trate x como menor que y, como o pedido
        scanf("%d", &x);
            scanf("%d", &y);
    }

    printf("Soma = %i\n", (x + y));
    printf("Produto = %i\n", (x * y));

    for(cont = x; cont <= y; cont ++)
    {
        if(cont % 2 == 0)
            pares += cont;
        else
            impares *= cont;
    }
    printf("Resultado:\n");
    printf("Pares = %i\n", pares);
    printf("Impares = %i\n", impares);
}

Behold him spinning with values 3 for x and 9 for y because x must be less than y.

  • The while what?

  • I haven’t analyzed his code, because I prefer the for in those cases, I’ve just looked at while that is correct..

Browser other questions tagged

You are not signed in. Login or sign up in order to post.