Mega Code Archive

 
Categories / C / Code Snippets
 

Compute the total of a list of numbers

#include <stdio.h> int main() { char line[100]; int total; int j; total = 0; while (1) { printf("Enter # to add \n"); printf(" or 0 to stop:"); fgets(line, sizeof(line), stdin); sscanf(line, "%d", &j); if (j == 0) break; total += j; printf("Total: %d\n", total); } printf("Final total %d\n", total); return (0); }