Problem with a variable in C

3

Good I have the following code, in which I have to calculate and visualize the sum and the product of the even numbers between 20 and 400 both inclusive. The product always shows me 0.

#include <stdio.h>

int main()
{
    int n=20;
    int sumaP=0, producto=1;

    while(n<=400)
    {
        printf(" %d ",n);
        if(n%2==0)
        {
            sumaP+=n;
            producto*=n;            
        }
        n++;
    }
    printf("\n\nLa suma total de numeros pares es: %d", sumaP);
    printf("\nEl producto total de numeros pares es: %d", producto);    

}
    
asked by 04.10.2017 в 19:57
source

2 answers

2

The problem is that the operation of showing the product of all the even numbers up to 400 is a long mistake so the int producto only takes up a number of numbers, so when it is not able to take more shows only a 0. What I have done has been to change the type of product data such that: unsigned long long int producto = 1; and then display it with %llu

Code:

int main()
{
    int sumaP=0;
    unsigned long long int producto = 1;
    int n=20;

    do{
        printf(" %d ",n);
        if(n%2==0)
        {
            sumaP = sumaP + n; 
            producto*=n;    
        }        
        n++;
    }while(n<=83); 
    printf("\n\nLa suma total de numeros pares es: %d", sumaP);
    printf("\nEl producto total de numeros pares es: %llu", producto);
}

But even so until the number 400 is much the result of the product. Doing tests, product only shows me up to the number of the rank 83 with a result of: 9223372036854775808.

If you wanted to go from 20 to 84 multiplying all the even numbers between them including 20 and 84, 0 would come out.

I hope I have helped you and clarified the doubts.

    
answered by 04.10.2017 / 21:13
source
2

The justification that @MarioGuiber has given you about the reason of 0 is not correct.

All even numbers have the common characteristic that their least significant bit is always 0. Multiplying a sequence of pairs produces a effect 0 . That is, the least significant bits are all marked as 0:

2  * 4 =   8 -> 0000 0010 * 0000 0100 = 0000 0000 1000 <- 3 bits a 0
8  * 6 =  48 -> 0000 1000 * 0000 0110 = 0000 0011 0000 <- 4 bits a 0
48 * 8 = 384 -> 0011 0000 * 0000 1000 = 0001 1000 0000 <- 7 bits a 0
...

On the other hand we have that the type int that you are using is a type composed of 32 bits (of which 1 is used to indicate the sign). Any number that you try to store that occupies more than 32 bits will see truncated all the bits that exceed 32.

The joint effect of these two characteristics causes that, from the moment in which the first 32 bits of the sequence are 0 (situation that occurs when multiplied by 28), the product will be stuck at number 0.

In fact, you can see how bits are being lost with a very simple algorithm:

int producto = 1;
for( int i = 2; i<30; i+=2 )
{
  int resultado = producto * i;
  printf("%d * %d = %d\n",producto,i,resultado);
  if( resultado / i != producto )
    puts("\tresultado incorrecto\n");
  producto = resultado;
}

From i=20 the results start to be strange.

If you do as you say @MarioGuiber and replace the type int with unsigned long long what you get is to increase the range of possible values since now you will use 64 bits instead of 31 (remember that int spends a bit in the sign). As the growth of the product is exponential a short time later you have the same overflow problem again.

    
answered by 05.10.2017 в 08:01