Program to Sum of Two Numbers in C using a Function

sum-of-two-numbers-in-c-using-functions

Program Description:

Write a program to Sum of Two Numbers in C using a Function. This program is to understand how functions work in c language.

The program accepts two numbers from the user and calculates the sum of those numbers. The program should create a function, which should accept two numbers as input and return the integer number ( which is the sum of given numbers).

Example Input and Output:

Input:

Enter two numbers : 10 20

Output:

Sum of 10 and 20 is : 30

Pre-Requisites:

Sum of two Numbers in C using Function Program Explanation:

  1. Accept two numbers from the user as input, And store them in variables number1 and number2 respectively.
  2. We are going to create a function called sum.
    • The sum function takes two variables as input number1 and number2 and calculate the sum of number1 and number2 and returns the result.
    • Here number1 and number2 are formal arguments of the sum function.
    • As the sum function is defined after the function call, So we need to declare the function using the function declaration. We have added the sum function declaration at the start of the program using int <strong>sum</strong>(int, int);
  3. Call the sum function by passing the number1 and number2 from the main function. As the sum function returns an integer which is the sum of the given numbers, Assign the function call to an integer variable (i.e result). So that the return value of the sum function will be stored in result variable
    • result = <strong>sum</strong>(number1, number2);
  4. Print the result on the console using printf function.

Sum of two Numbers in C using function Program:

Here is the sum of two numbers program using functions in the C programming language

The sum function prototype details.

function_name: sum

argument_list: (int, int)

return_type: int

Sum of two Numbers in C Program Output:

We are using the GCC compiler to compile and run the program.

Run the program and provide two integer values.

Let’s try a few more examples

sum-of-two-numbers-in-c-using-function-program-output

As we can see from the above output, We are getting the expected results.

Related Programs:

  1. C Program to Calculate the Power of Number
  2. C Program to Check Number is Power of 2
  3. C Program to find power of number without Loops
  4. C Program to Find all Factors of a Number
  5. C Program to find all Prime Factors of number 
  6. C Program to Calculate the GCD or HCF of Two Number
  7. C Program to Calculate the LCM of two Numbers
  8. C Program to Check Palindrome Number
  9. C Program to Check 3-Digit and N-Digit Armstrong Number
  10. C Program to Generate Armstrong Numbers upto N ( User-Provided Number)
  11. C Program to Generate Armstrong Numbers between two Intervals

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

2 Responses

  1. […] Add Two Numbers using Functions in C […]

  2. […] Add Two Numbers using Functions in C […]

Leave a Reply