Walk Lightly on this PLANET and yet leave such a FOOTPRINT that cannot be erased for thousands of Years..!!!
Visit Codstech for Cyber Security related Posts !

Visitors

Saturday, September 17, 2011

C-43 : Sum of 1 to 100 numbers

Refer  Find Sum , Sum of n even numbers using user defined fun... 


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
=================================================================
Back to Home   &  C

No comments: