Computer for SEE and NEB

It is a complete SEE and NEB solution for computer science. It includes Computer Fundamentals, Database (SQL), Programming in C QBASIC, CSS, JavaScript, and PHP for beginners.

Breaking

Post Top Ad

Your Ad Spot

Tuesday, September 27, 2022

C program to find the sum of two numbers using functions

C program to find the sum of two numbers using functions

 

C program to find the sum of two numbers using functions



Introduction:


If you're just starting out with programming, learning how to add two numbers using a C program can be a great place to begin. Adding two numbers may seem like a trivial task, but it is an essential part of any programming language. In this tutorial, we'll show you how to write a C program to find the sum of two numbers using functions.
The C program to find the sum of two numbers using functions is a simple program that takes two input numbers from the user and returns the sum of those numbers. This program uses functions to perform the addition, making it easier to understand and modify.

Writing a C Program to Find the Sum of Two Numbers using Functions


Step 1: Include the Header Files


Before writing any C program, you need to include the required header files. In our program, we need to include the stdio.h and conio.h header files.

Step 2: Declare the Function Prototype


The function prototype declares the function's name, return type, and input parameter types. In our program, we need to declare the add function prototype before the main function.

Step 3: Define the add Function

The add function takes two integer parameters and returns the sum of those numbers. In our program, we define the add function after the main function.

Step 4: Write the Main Function


The main function is the entry point of the program. It takes no input parameters and returns an integer value. In our program, we declare two integer variables, num1 and num2, to store the input values.

Step 5: Call the add Function

After declaring the variables, we call the add function with the input values as arguments. The return value of the add function is stored in a variable called sum.

Step 6: Display the Result

Finally, we use the printf function to display the result of the addition operation.

Programming coding:


#include<stdio.h>
int sum(int x, int y) /*Function definition*/
{
int s;
s = x + y;
return s;
}

int main()
{
int a, b, s;
printf("Enter any two numbers:\n" );
scanf("%d %d", &a, &b);
s = sum(a , b); /*Function call*/
printf("Sum of %d and %d is %d\n", a, b, s);
}

Output:





FAQs


Q: Why do we need to use functions to add two numbers in a C program? 
Ans: Functions make the program more modular, easier to understand, and easier to modify.

Q: What are header files in C programming? 
Ans: Header files are files that contain function prototypes and other definitions used in a C program.

Q: What is the return type of the add function in our program? 
Ans: The return type of the add function is integer.

Conclusion

In conclusion, writing a C program to find the sum of two numbers using functions is a simple yet essential task for any programmer. By following the steps outlined in this tutorial, you can learn how to create a program that adds two numbers and returns their sum. Remember to always include the required header files, declare the function prototype, define the function, write the main function, call the function, and display the result. With practice, you'll become proficient in writing C programs in no time.



No comments:

Post a Comment

Post Top Ad

Your Ad Spot

Pages