Add N numbers of Positive integers in C

/*     Reads a positive number N. Then read N integers and
* print them out together with their sum.
*     Author: maSnun (masnun@gmail.com)
*/

#include <stdio.h>

int main(void) {
int n; /* The number of numbers to be read */
int sum; /* The sum of numbers already read */
int current; /* The number just read */
int lcv; /* Loop control variable, it counts the number
of numbers already read */

printf(“How many numbers to add ? (N) > “);
scanf(“%d”,&n); /* We should check that n is really positive*/
sum = 0;
for (lcv=0; lcv < n; lcv++) {
printf(“nEnter an integer > “);
scanf(“%d”,&current);
/* printf(“nThe number was %dn”, current); */
sum = sum + current;
}
printf(“The sum is %dn”, sum);
return 0;
}

Well, another typical program in C. I don’t think it would need more details. There’s already enough inline comments J

Tags: . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">