Blog Moved

This website completely moved to new domain. For latest content, visit www.programmingposts.com

Search This Blog

21 Jul 2012

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


25 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
  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. 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
  13. #include
    void addition();
    void subtraction();
    void multiplication();
    void division();
    Int a,b,choice;
    int main ()
    Printf("value of a");
    Scanf("%d",&a);
    Printf("value of b");
    Scanf("%d",&b);
    printf("please type /n 1 for add/n 2/ for sub/n 3 for mul/n 4 for div/n");
    printf("enter your choice");
    scanf("%d",&choice);
    if(choice==1)
    {
    void addition()
    }
    if(choice==2)
    {
    void subtraction()
    if(choice==2)
    {
    void multiplication()
    }
    if(choice==4)
    {
    void division()
    }
    return 0;
    }
    void addition();
    {
    printf("sum of %d and %d is %d",a,b,(a+b));
    }
    void subtraction();
    {
    printf("sub of %d and %d is %d",a,b,(a-b));
    }
    void muptiplication();
    {
    printf("mul of %d and %d is %d",a,b,(a*b));
    }
    void division();
    {
    printf("div of %d and %d is %d",a,b,(a/b));
    }

    ReplyDelete
  14. please tell me how to print the result in main if function is void type

    ReplyDelete
    Replies
    1. Hi, In this case, you can use a global variable.

      #include
      int result; /*global variable*/
      int main()
      {
      int a=10,b=20;
      sum(a,b);
      printf("\n The sum of two numbers is : %d ",result);
      return 0;
      }

      sum(int num1,int num2)
      {
      result = num1 + num2 ; /*adding two numbers and storing result in global variable*/
      }

      Delete
  15. I write same in code::block but it's not working

    ReplyDelete
  16. Everything worked....plz elaborate use of scanf and printf .....why %d is used....

    ReplyDelete
    Replies
    1. %d is used as a format specifier, here is a samll example, go through this link http://cprogramsblog.blogspot.com/2012/07/c-program-for-showing-printf-format.html

      Delete