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 */

}

great explanation

Delete#include

Deleteint div(int,int);

int main(void)

{

int a,b,result;

printf("\nenter the two integers:\n");

scanf("%d%d",&a,&b);

result=div(a,b);

printf("\nthe division of %d and %d is %d",a,b,result);

return 0;

}

int div(int p ,int q)

{

int c1;

if(q==0)

printf("\n UNDEFINED\n");

else

c1=p/q;

return (c1);

}

hi! can you please draw a flowchart of this program, i'll be very grateful, can't find any flowchart of functions anywhere

ReplyDeletehad anyone helped u in it Mr.Sharma?

Deletehmmmm

ReplyDeleteaddition of three num. using function. one should be in main nd others should be in function.. will u help me with this query?

ReplyDelete#include

Delete#include

int total (int, int);

int main()

{

int a, b, c, d, e;

d = 450;

printf("1st number is: %d\n",d);

printf("Now Enter other the two numbers:\n\n");

scanf("%d %d", &a, &b);

c = total(a, b);

e = c + d;

printf("Total of three Numbers is: %d\n",e);

getch();

return 0;

}

int total(int num1, int num2)

{

int result;

result = num1 + num2;

return result;

}

#include #include int sum(int x,int y); //function declaration void main() { int a,b,c; clrscr(); printf("Enter the two numbers:"); scanf("%d%d",&a,&b); c=sum(a,b); //function call printf(" \n the sum of two number is : %d ",c); getch(); } int sum(int x,int y) //function definition { int c; c=x / y; return c; }

ReplyDeletecan any one help mw with finding function prototype with flow charts?

ReplyDeleteHey all you C lovers. Here are some programming exercises for beginners to practice and practice

ReplyDeleteBasic programming exercises in C

If else programming exercise to practice in C

Exercises on switch case in C

Exercises on conditional operator

Practice, Practice and Practice....

Happy coding ;)

I will like to know how to add to no. Which are declare in function n one function in main in c++

ReplyDelete#include

ReplyDelete#include

int sum(int,int);

void main()

{

int a,b,c;

printf("\n Enter the two numbers : ");

scanf("%d %d",&a,&b);

c = sum(a,b);

printf("\n The sum of two numbers is : %d ",c);

getch();

}

int sum ( int num1,int num2)

{

int result;

return (result) ;

}

Sample Output: ( using GNU GCC Compiler with Code Blocks IDE )

Enter the two numbers: 10 15

The sum of two numbers is: 25