Computer Science, asked by cinthyafabula, 7 months ago

9. Write a C++ program to get two numbers from the user and add them to produce the
desired result
10.Write a C++ program to add three numbers. (Constant method)
11. Write a C++ program to get three numbers from the user and add them to produce
the desired result.
12. Write a C++ program to subtract two numbers. (Constant method)
13. Write a C++ program to get two numbers from the user and subtract them to
produce the desired result.
14. Write a C++ program to subtract three numbers. (Constant method)
15. Write a C++ program to get three numbers from the user and subtract them to
produce the desired result.
16. Write a C++ program to multiply two numbers. (Constant method)
17.Write a C++ program to get two numbers from the user and multiply them to
produce the desired result.
18. Write a C++ program to multiply three numbers. (Constant method)
19. Write a C++ program to get three numbers from the user and multiply them to
produce the desired result
20.Write a C++ program to divide two numbers. (Constant method)
21. Write a C++ program to get two numbers from the user and divide them to produce
the desired result.
22. Write a C++ program to divide two number and display its quotient and remainder.​

Answers

Answered by harshvg20gmailcom
1

Answer:

++ Program to add two numbers

BY CHAITANYA SINGH | FILED UNDER: C++ PROGRAMS

In this tutorial, we will see three ways to add two numbers in C++. 1) Simple C++ program to add two numbers 2) adding numbers using function overloading 3) adding numbers using class and functions.

1) Simple C++ program to add two numbers

In this program we are asking user to input two integer numbers and then we are adding them and displaying the result on screen.

#include <iostream>

using namespace std;

int main(){

//Declaring two integer variables

int num1, num2;

/* cout displays the string provided in the

* double quotes as it is on the screen

*/

cout<<"Enter first integer number: ";

/* cin is used to capture the user input

* and assign it to the variable.

*/

cin>>num1;

cout<<"Enter second integer number: ";

cin>>num2;

cout<<"Sum of entered numbers is: "<<(num1+num2);

return 0;

}

Output:

Enter first integer number: 10

Enter second integer number: 20

Sum of entered numbers is: 30

2) C++ program to add two numbers using function overloading

In this example, we will see how to add two numbers using function overloading. Function overloading is a feature that allows us to have more than one function having same name but different number, type of sequence of arguments. Here we are defining three functions for the same purpose addition, based on the data type of arguments different function is called.

/* Function overloading example. Where we can have

* more than one functions with same name but different

* number, type or sequence of arguments

*/

#include <iostream>

using namespace std;

int sum(int, int);

float sum(float, float);

float sum(int, float);

int main(){

int num1, num2, x;

float num3, num4, y;

cout<<"Enter two integer numbers: ";

cin>>num1>>num2;

//This will call the first function

cout<<"Result: "<<sum(num1, num2)<< endl;

cout<<"Enter two float numbers: ";

cin>>num3>>num4;

//This will call the second function

cout<<"Result: " <<sum(num3, num4)<< endl;

cout<<"Enter one int and one float number: ";

cin>>x>>y;

//This will call the third function

cout<<"Result: " <<sum(x, y)<< endl;

return 0;

}

int sum(int a, int b){

return a+b;

}

float sum(float a, float b){

return a+b;

}

/* Remember that sum of int and float is float

* so the return type of this function is float

*/

float sum(int a, float b){

return a+b;

}

Output:

Enter two integer numbers: 21 88

Result: 109

Enter two float numbers: 10.2 30.7

Result: 40.9

Enter one int and one float number: 20 16.4

Result: 36.4

3) Addition using Class and Function

#include <iostream>

using namespace std;

class Add{

public:

/* Two variables that we are going to

* add. If you want to add float or double

* variables instead, just change the data

* type. for example: float num1, num2;

*/

int num1, num2;

/* This function ask the user for two numbers.

* The numbers that user enter are stored into

* num1 and num2 variables so that we can add

* them later.

*/

void ask(){

cout<<"Enter first number: ";

cin>>num1;

cout<<"Enter second number: ";

cin>>num2;

}

/* This function adds the numbers that are passed

* to it through arguments. I have used parameter names

* as n1 and n2 but you can choose any parameter name.

* This function returns the result.

*/

int sum(int n1, int n2){

return n1+n2;

}

//This function displays the addition result

void show(){

cout<<sum(num1, num2);

}

};

int main(){

//Creating object of class Add

Add obj;

//asking for input

obj.ask();

//Displaying the output

obj.show();

return 0;

}

Output:

Enter first number: 21

Enter second number: 19

40

You may be wondering why didn’t I call the sum function from the main() function, this is because the sum function is called from the show() function.

Explanation:

please click brainlist mark

Answered by pulkitkaur28
1

Answer

5.0/5

1

harshvg20gmailcom

Ambitious

54 answers

2.7K people helped

Answer:

++ Program to add two numbers

BY CHAITANYA SINGH | FILED UNDER: C++ PROGRAMS

In this tutorial, we will see three ways to add two numbers in C++. 1) Simple C++ program to add two numbers 2) adding numbers using function overloading 3) adding numbers using class and functions.

1) Simple C++ program to add two numbers

In this program we are asking user to input two integer numbers and then we are adding them and displaying the result on screen.

#include <iostream>

using namespace std;

int main(){

//Declaring two integer variables

int num1, num2;

/* cout displays the string provided in the

* double quotes as it is on the screen

*/

cout<<"Enter first integer number: ";

/* cin is used to capture the user input

* and assign it to the variable.

*/

cin>>num1;

cout<<"Enter second integer number: ";

cin>>num2;

cout<<"Sum of entered numbers is: "<<(num1+num2);

return 0;

}

Output:

Enter first integer number: 10

Enter second integer number: 20

Sum of entered numbers is: 30

2) C++ program to add two numbers using function overloading

In this example, we will see how to add two numbers using function overloading. Function overloading is a feature that allows us to have more than one function having same name but different number, type of sequence of arguments. Here we are defining three functions for the same purpose addition, based on the data type of arguments different function is called.

/* Function overloading example. Where we can have

* more than one functions with same name but different

* number, type or sequence of arguments

*/

#include <iostream>

using namespace std;

int sum(int, int);

float sum(float, float);

float sum(int, float);

int main(){

int num1, num2, x;

float num3, num4, y;

cout<<"Enter two integer numbers: ";

cin>>num1>>num2;

//This will call the first function

cout<<"Result: "<<sum(num1, num2)<< endl;

cout<<"Enter two float numbers: ";

cin>>num3>>num4;

//This will call the second function

cout<<"Result: " <<sum(num3, num4)<< endl;

cout<<"Enter one int and one float number: ";

cin>>x>>y;

//This will call the third function

cout<<"Result: " <<sum(x, y)<< endl;

return 0;

}

int sum(int a, int b){

return a+b;

}

float sum(float a, float b){

return a+b;

}

/* Remember that sum of int and float is float

* so the return type of this function is float

*/

float sum(int a, float b){

return a+b;

}

Output:

Enter two integer numbers: 21 88

Result: 109

Enter two float numbers: 10.2 30.7

Result: 40.9

Enter one int and one float number: 20 16.4

Result: 36.4

3) Addition using Class and Function

#include <iostream>

using namespace std;

class Add{

public:

/* Two variables that we are going to

* add. If you want to add float or double

* variables instead, just change the data

* type. for example: float num1, num2;

*/

int num1, num2;

/* This function ask the user for two numbers.

* The numbers that user enter are stored into

* num1 and num2 variables so that we can add

* them later.

*/

void ask(){

cout<<"Enter first number: ";

cin>>num1;

cout<<"Enter second number: ";

cin>>num2;

}

/* This function adds the numbers that are passed

* to it through arguments. I have used parameter names

* as n1 and n2 but you can choose any parameter name.

* This function returns the result.

*/

int sum(int n1, int n2){

return n1+n2;

}

//This function displays the addition result

void show(){

cout<<sum(num1, num2);

}

};

int main(){

//Creating object of class Add

Add obj;

//asking for input

obj.ask();

//Displaying the output

obj.show();

return 0;

}

Output:

Enter first number: 21

Enter second number: 19

40

You may be wondering why didn’t I call the sum function from the main() function, this is because the sum function is called from the show() function.

Explanation:

please click brainlist mark

Similar questions