What is wrong with the logic of this code that adds numbers tracks?

1

My algorithm needs to sum all values between a and b , for example if I type a=3 e b=6 the program needs to speak 3 7 12 18 (3+4= 7/ 7+5=12 / 12+6=18) only it only makes the first two then it starts to err.

#include <stdio.h>  

int main ()  
{  

int a,b,i,n,x,c;  

  printf("Digite a: ");  
  scanf("%d", &a);  
  printf("Digite b: ");  
  scanf("%d", &b);  

  c=0;  
  for(i=1;i<=b;i++)  
  {  
    c=a;  
    a=c+(a+1);  

    printf("%d\n ",c);  

  }


    return 0;  
}
    
asked by anonymous 13.05.2018 / 03:47

2 answers

4

The code is a bit confusing and tries to do too many things. Just start the loop with the first data entered and end with the second. The term is correct, but must start with a .

#include <stdio.h>  

int main() {
    int a, b;
    printf("Digite a: ");
    scanf("%d", &a);
    printf("Digite b: ");
    scanf("%d", &b);
    int soma = 0;
    for (int i = a; i <= b; i++) {
        soma += i;
        printf("%d ", soma);
    }
}

See working on ideone .

    
13.05.2018 / 04:26
0
#include <stdio.h>

int main()
{
    int a, b, soma=0, i;

    printf("Digite a: ");
    scanf("%d", &a);
    printf("Digite b: ");
    scanf("%d", &b);

    for(i=0; i<=b; i++)
    {
        if(i >= a)
        {
            soma = soma + i;
        }
    }
    printf("%i\n", soma);
}
    
13.05.2018 / 04:13