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


20 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. #include
      int 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);

      }

      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
    Replies
    1. had anyone helped u in it Mr.Sharma?

      Delete
  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
    Replies
    1. #include
      #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;
      }

      Delete
  4. #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; }

    ReplyDelete
  5. can any one help mw with finding function prototype with flow charts?

    ReplyDelete
  6. Hey all you C lovers. Here are some programming exercises for beginners to practice and practice

    Basic 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 ;)

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

    ReplyDelete
  8. #include
    #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

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete
  10. pls someone solve it...
    que-write a c program to create 2 single dimensional array size by store data into the 2nd array.display the data both the erray?

    ReplyDelete
  11. add two numbers with arguments and without
    return values .... plz help

    ReplyDelete
    Replies
    1. Hi, We can also add two numbers using pointers in C.

      firstNumberPointer = &firstNumber;
      secondNumberPointer = &secondNumber;
      sum = *firstNumberPointer + *secondNumberPointer;










      Delete
  12. This blog awesome and i learn a lot about programming from here.The best thing about this blog is that you doing from beginning to experts level.

    Love from

    ReplyDelete
  13. Write function max(), this function find the maximum number between three numbers Write a program that uses this function to allow user enter numbers and print maximum number

    ReplyDelete