Summation and Factorial
Summation of Natural Number From 1 to 10
#include<stdio.h>
int main(){
int i = 0, n = 10, sum = 0;
for(i=1; i<=n; i++){
sum = sum + i;
}
printf("%d\n",sum);
return 0;
}
#include<stdio.h>
int main(){
int n = 5, i, sum, f;
//scanf("%d", &n);
//printf("%d", n);
//1 + 2 + 3 + 4 ............... + N;
for( i=1, sum=0; i<=n; i++){
//sum = sum + i;
sum += i;
}
printf("%d\n", sum);
//1 + 3 + 5 ............... + N;
for( i=1, sum=0; i<=n; i+=2){
sum += i;
}
printf("%d\n", sum);
//2^3 + 4^3 + 6^3 + 8^3 + ...... + N^3
for( i=2, sum=0; i<=n; i+=2){
sum += i*i*i;
}
printf("%d\n", sum);
return 0;
}
#include<stdio.h>
int main(){
int c;
int sum = 0;
c = 2;
while(c <= 6){
sum += c;
c += 2;
}
printf("%d\n", sum);
return 0;
}
Factorial (n)
#include<stdio.h>
int main(){
int i=0, n=5, f=1;
for(i=1; i<=n; i++){
f *= i;
}
printf("Factorial (%d) = %d", n, f);
return 0;
}
// Factorial(n) = n! = 1*2*3* ... *(n-1)*n
// Factorial(5) = 5! = 1 * 2 * 3 * 4 * 5 = 120
Last updated