Created
February 13, 2020 13:53
-
-
Save putheakhem/2ffd2658c201b7f0d7eefc7e20700293 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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