This blog is under construction

Saturday 6 July 2013

C program to add two numbers using functions

Write a C program to add two numbers using function(call by value).


  #include <stdio.h>

  /* function to add two numbers */
  int addTwoNumbers(int num1, int num2) {
        int sum;
        sum = num1 + num2;
        return (sum);
  }

  int main() {
        int num1, num2, sum;

        /* get two numbers from user */
        printf("Enter your first input:");
        scanf("%d", &num1);
        printf("Enter your second input:");
        scanf("%d", &num2);

        /* calling a function to add two numbers */
        sum = addTwoNumbers(num1, num2);

        /* printing the result */
        printf("Sum of %d and %d is %d\n", num1, num2, sum);
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter your first input:100
  Enter your second input:200
  Sum of 100 and 200 is 300



No comments:

Post a Comment