Add typed values without using array

2

Create a program that asks the user to type 10 values, add these results and present them on the screen.

I've only been able to do with an integer array.

#include <stdio.h>
#include <stdlib.h>

int main()
{
  int i, soma;
  soma = 0;
  int num[10];
  printf("Digite 10 numeros para somar:\n");
  for(i= 0; i<10; i++) {
    printf("Digite a %d nota: ", i+1);
    scanf("%d", &num[i]);
    soma +=num[i];
  }
  printf("\nSoma = %d : ", soma);

  system("pause");
   return 0;
}

How do I add the 10 values read without using array ???

    
asked by anonymous 22.07.2015 / 04:29

1 answer

2

Using the array was only disrupting, just use a normal variable instead of an array , after all it had no function in the algorithm.

#include <stdio.h>
#include <stdlib.h>

int main() {
  int i, soma = 0, num;
  printf("Digite 10 numeros para somar:\n");
  for(i = 0; i < 10; i++) {
    num = 0;
    printf("Digite a %d nota: ", i + 1);
    scanf("%d", &num);
    soma += num;
  }
  printf("\nSoma = %d : ", soma);
  return 0;
}

See working on ideone .

    
22.07.2015 / 04:37