Write a c program to add two numbers / integers using function..
Note: The same program can be used for adding floating numbers, just we have
to change the function return type,and all variables to float type and
for large numbers declare as long..
PROGRAM:
#include<stdio.h>
#include<conio.h>
int sum(int,int); /*declaring prototype of function*/
void main()
{
int a,b,c;
printf("\n Enter the two numbers : ");
scanf("%d %d",&a,&b); /* taking two numbers as input*/
c = sum(a,b); /* calling function,
*the value returned by the function is stored in c */
printf("\n The sum of two numbers is : %d ",c);
getch();
}
int sum ( int num1,int num2)
{
int result; /* defining variable, its scope lies within function */
result = num1 + num2 ; /*adding two numbers*/
return (result) ; /* returning result */
}
Sample Output: ( using GNU GCC Compiler with Code Blocks IDE )
Enter the two numbers: 10 15
Can you please write a prog on division using functions.
ReplyDeleteHI pavan, for getting division of two numbers using function,
Deletein the above program just change the following
c = division(a,b);
int division( int num1,int num2)
{
int result; /* defining variable, its scope lies within function */
result = num1 / num2 ; /*division of two numbers*/
return (result) ; /* returning result */
}
You can get a huge list of c programson vitedu.in.
ReplyDelete