Refer Find Sum , Sum of n even numbers using user defined fun...
1st Method :
2nd Method :
3rd Method :
Output :
The sum of the numbers from 1 to 100 is 5050
=================================================================
1st Method :
#include<stdio.h> #include<conio.h> void main() { clrscr(); int i,sum=0; for(i=1;i<101;i++) sum=sum+i; printf("The sum of the numbers from 1 to %d is %d\n",i-1,sum); getch(); } |
2nd Method :
#include<stdio.h> #include<conio.h> void main() { clrscr(); int i,sum=0; for(i=1;i<101;sum+=i++) ; printf("The sum of the numbers from 1 to %d is %d\n",i-1,sum); getch(); } |
3rd Method :
#include<stdio.h> #include<conio.h> void main() { clrscr(); int i,sum=0; for(i=1;i<101 && (sum+=i++);) ; printf("The sum of the numbers from 1 to %d is %d\n",i-1,sum); getch(); } |
Output :
The sum of the numbers from 1 to 100 is 5050
=================================================================
No comments:
Post a Comment