//Exercise 2
//Write a function called calculateSum that will take an integer number n as input, and calculate
// 1 + 2 + 3 + ..+ n
// And print the result on the screen.
//Hint: the function header will be
#include <stdio.h>
#include <stdlib.h>
void calculateSum(int n);
int main() {
int n=5;
calculateSum(n);
return 0;
}
void calculateSum(int n)
{
int sum=0;
int i;
for(int i=1;i<=n;i++)
{
sum=sum+i;
}
printf("the sum of 1 to %d is %d",n,sum);
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter