C Language Function Example # 01
* Lets find the sum of First N Numbers using Function.
* calculating the sum of numbers in c example.
C Language Code:
//Written by Latest Technology Guide
//Title : Example 10 : Calculate the Sum of N Numbers using Function : C Function Example .
#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
int add(int);
int n,sum;
printf("Enter Maximum Number for Sum : ");
scanf("%d",&n);
sum=add(n);
printf("\n Sum is : %d",sum);
getch();
}
int add(int n)
{
int i,sum=0;
for(i=1;i<=n;i++)
sum=sum+i;
return(sum);
}
We think there is no need of Output for this program.
--------------------------------------------------------------------------------
Explanation of C Language Example # 10 Calculate the Sum of N Numbers using Function : C Function Example .
* Enter Value of N.
* Program will calculate sum from Number 1 to N and will return it to the calling function i.e. void main().
* Here we have crated function add() with one parameter its n, and return type as integer.
* In add() function we have created temporary variable which will automatically dispose when you exist from the function.
Note: All programs are developed and tested using Turbo C++ 3.0 under Windows XP. We just want to provide guidelines to the users. If you are using any other Compiler or other operating system they you need to modify this program as per your requirements.
No comments:
Post a Comment