Program that adds / multiplies 2 numbers and what is between them and prints

2

I think the logic of this program is right, but the result is going wrong.

Make a program that receives two numbers X and Y, where X < Y. Calculate and show:

  • the sum of the even numbers of this range of numbers, including the numbers entered;
  • multiplication of odd numbers in this range, including digits

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 that I am putting is 5 and 3, being 8 the sum and 15 the result, in the first two printfs is coming correct, now in the last two printfs are returning me respectively, 2752260 and 2752256.

    
asked by anonymous 17.09.2015 / 21:03

2 answers

4

Just remove the & operator from the printf() end. They are having to print the address of the variables and not their contents. If you want the content, use the simple variable name. You may have been confused with the scanf() that requires the operator to pass by reference and the variable receive the new value.

#include <stdio.h>

int 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);
    return 0;
}

See running on ideone .

Maybe the algorithm is wrong too, but then I can not be sure. I answered what was asked and what is clear in the question.

    
17.09.2015 / 21:06
0

The while can be wrong .. And the & in 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);
}

See it running with values 3 for x and 9 for y since x must be less than y.

    
17.09.2015 / 21:10