Difference between revisions of "C programming/sumsum.c"
From Teknologisk videncenter
m (Created page with "<source lang=c> #include <stdio.h> int add(int a, int b) { int c; c = a + b; return( c); } /* totalsum returner summen af alle tal op til tallet p. ...") |
(No difference)
|
Revision as of 11:37, 16 November 2011
#include <stdio.h>
int add(int a, int b) {
int c;
c = a + b;
return( c);
}
/* totalsum returner summen af alle tal op til tallet p.
fx. overføres 5 som p til totalsum returneres:
1 + 2 + 3 + 4 + 5 = 15
*/
int totalsum( int p) {
int totalsum = 0;
int count;
for( count = 1; count <= p; count++) {
totalsum = add(totalsum, count);
}
return(totalsum);
}
main() {
int tal1;
int tal2;
int res;
tal1=42;
tal2=16;
res = add(tal1, tal2);
printf("Sum er %i\n", res);
res = totalsum(5);
printf("Totalsum af 5 er %i\n",res);
res = totalsum(5000);
printf("Totalsum af 5000 er %i\n",res);
}