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

Search this Site

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


3 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. You can get a huge list of c programson vitedu.in.

    ReplyDelete

Thanks for your comments.
-Sameer