In this tutorial, we will discuss the Cpp program to add two numbers using the function.
In this topic, we will learn a simple concept of how to add two number(integer , floating point)using the function in the C++ programming language
already we will know the same concept using the operator.
if you known click here C++ program to sum of two numbers
This program has following procedures for completion
Program 1
#include <iostream> #include <conio.h> using namespace std; int add(int x, int y);//function declaration or prototype int main() { int num1; //variable for store first numbers int num2; //variable for store second numbers int sum; //variable for store result of product //read numbers cout << "Enter the first number" << endl; cin>>num1; //stored first number- get input from user cout << "Enter the second number" << endl; cin>>num2; //stored second number- get input from user sum=add(num1, num2);//calling function and stored returning the result by function; //print sum of numbers cout<<"sum of two numbers "<<sum<<endl; getch(); return 0; } //function definition int add(int x, int y) { return (x+y); //return the product result to main }
When the above code is compiled and executed, it produces the following results
Enter the first number 23 Enter the second number 43 sum of two numbers 66
In the above program, deeply describe how to find sum of two integer using function in C++ language.
Program 2
#include <iostream> #include <conio.h> using namespace std; float add(float x, float y);//function declaration or prototype int main() { float num1; //variable for store first number float num2; //variable for store second number float sum; //variable for store result of sum //read numbers cout << "Enter the first numbers" << endl; cin>>num1; //stored first number- get input from user cout << "Enter the second numbers" << endl; cin>>num2; //stored second number- get input from user sum=add(num1, num2);//calling function and stored returning the result by function; //print sum of numbers cout<<"Sum of two numbers "<<sum<<endl; getch(); return 0; } //function definition float add(float x, float y) { return (x+y); //return the addition result to main }
When the above code is compiled and executed, it produces the following results
Enter the first numbers 34.78 Enter the second number 45.34 Sum of two numbers 80.12
In the above program, deeply describe how to find sum of two floating point values using function in C++ language.
Suggested for you
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…
PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…
5 methods to add two numbers in Java In this tutorial, we will discuss the…
Python full Pyramid star pattern program In this tutorial, we will discuss the concept of…
Write a function or method to convert C into F -Entered by the user In…
How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…