Add typed values without using array

Asked

Viewed 709 times

2

Prepare a program that asks the user to enter 10 values, add this results and present on the screen.

I could only do it with one array whole.

#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 read values without using array???

1 answer

2


The use of array was just getting in the way, just use a normal variable instead of a array, after all it had no function in the algorithm.

#include <stdio.h>

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

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

Browser other questions tagged

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