find the sum of first N natural numbers

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....... :-)

More by this Author


Comments 5 comments

Ishan 6 years ago

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


shijo 6 years ago

give an output also


kirti shinde 4 years ago

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


jewel 2 years ago

nmknkl


jewel mathew pm 2 years ago

S=n(n=1)/2

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working