C Program to Add Two Numbers Using Function



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
 The sum of two numbers is: 25

For C# program :

C# PROGRAM TO ADD TWO INTEGERS USING FUNCTION


6 comments:

  1. Can you please write a prog on division using functions.

    ReplyDelete
    Replies
    1. HI pavan, for getting division of two numbers using function,
      in 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 */
      }

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

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

    ReplyDelete