ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

find the sum of first N natural numbers

Updated on March 22, 2010

sum of first N natural numbers

#include<stdio.h>

#include<conio.h>

void main()

{

int s=0,i=1,n;

clrscr();

printf("Enter a value ");

scanf("%d",&n);

while(i <= n)

{

s=s+i;

i=i+1;

}

printf("Sum of first %d natural numbers is %d",n,s)

getch();

}

-----------------------------------------------------------------------------------

NB: // ---> this is a comment. compiler will not execute the statements after this '//'.

#include<stdio.h> --------> header file

void main() --------------> main function

int s=0 -------> variable declaration & value assigning

clrscr() -----> built-in function for clearing the screen

printf() -----> built-in function for print a message and print the output.

scanf() ------> built-in funtion for accepting a value

getch() ------> built-in function for accepting a character.

for running the program you should press Ctrl + F9 keys together.

------------------------------------------------------------------------------------

thanking you.

hope u get the result....... :-)

Comments

    0 of 8192 characters used
    Post Comment

    • profile image

      Ishan 6 years ago

      In the last printf statment u missed a semi-colon.

    • profile image

      shijo 6 years ago

      give an output also

    • profile image

      kirti shinde 4 years ago

      it's a very easy. it is understand lauguage.

    • profile image

      jewel 2 years ago

      nmknkl

    • profile image

      jewel mathew pm 2 years ago

      S=n(n=1)/2

    Click to Rate This Article