Make a new start with a new Thought!

NOTE :                // indicates comments.This wont get printed in the output.This is used for for documentation purpose. PROGRAM: #incl...

Sum of 'n' natural numbers

NOTE :
              
// indicates comments.This wont get printed in the output.This is used for for documentation purpose.

PROGRAM:

#include<stdio.h>
#include<conio.h>
int sum(int n);                        //Function Declaration 
void main()
{
 int n,r;
 clrscr();
 printf("Enter a value\t:\n");
 scanf("%d",&n);  
 r=sum(n);                              //Function Calling 
 printf("The sum of %d natural nummbers is %d",n,r);
 getch();
}
int sum(int n)                     //Function Defination
{
 if(n!=0)
 {
    return n+sum(n-1);
 }
 else
 {
    return n;
 }
}

OUTPUT :



0 comments: