Skip to content

Instantly share code, notes, and snippets.

@putheakhem
Created February 13, 2020 13:53
Show Gist options
  • Save putheakhem/2ffd2658c201b7f0d7eefc7e20700293 to your computer and use it in GitHub Desktop.
Save putheakhem/2ffd2658c201b7f0d7eefc7e20700293 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
// Write a function that can calculate the summation from 1 to n positive number where n is passed as parameter.
// we need function sum()
int sum(int n) {
int sum1toN = 0;
for(int i = 0; i < n; i++) {
sum1toN += i; // sum1toN = sum1toN + i;
}
return sum1toN;
}
int main() {
// we need to ask user to input value of n
int n;
printf("Enter value of n: ");
scanf("%d", &n);
// we need to print the result from sum() function
printf("the summation from 1 to %d is : %d ", n, sum(n));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment