C program to add two numbers using functions

Beginners code hub
By -
0
#include "stdio.h"

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

    printf("Enter the first number: ");
    scanf("%d", &num1);

    printf("Enter the second number: ");
    scanf("%d", &num2);

    sum = num1 + num2;

    printf("The sum of %d and %d is: %d\n", num1, num2, sum);

    return 0;
}
    
1. Header File
The program begins with #include <stdio.h>, which is necessary for using standard input/output functions like printf and scanf.

2. Function Prototype
The line int addNumbers(int a, int b); declares a function named addNumbers that takes two integers as arguments and returns an integer value.

3. Main Function

We declare three variables: num1, num2, and sum.

We use printf and scanf to prompt the user for input and store the values of num1 and num2.

The function addNumbers is called, and the sum of the two numbers is stored in the variable sum.

Finally, the result is printed using printf.

4. AddNumbers Function
This function takes two integers (a and b) as input, adds them, and returns the result. It's defined separately to make the code modular and reusable.

Sample output
c program to add two numbers using functions



Tags:

Post a Comment

0Comments

Post a Comment (0)