Welcome to The Coding College! In this tutorial, we’ll demonstrate how to add two numbers in C++. Whether you’re a beginner or brushing up on your skills, this guide will help you master addition using different techniques in C++.
Simple Addition of Two Numbers
Example: Adding Two Numbers
#include <iostream>
using namespace std;
int main() {
int num1 = 10;
int num2 = 20;
int sum = num1 + num2;
cout << "The sum of " << num1 << " and " << num2 << " is " << sum << endl;
return 0;
}
Output:
The sum of 10 and 20 is 30
Getting Input from the User
Example: Add Two Numbers Using User Input
#include <iostream>
using namespace std;
int main() {
int num1, num2;
cout << "Enter the first number: ";
cin >> num1;
cout << "Enter the second number: ";
cin >> num2;
int sum = num1 + num2;
cout << "The sum of " << num1 << " and " << num2 << " is " << sum << endl;
return 0;
}
Output:
Enter the first number: 15
Enter the second number: 25
The sum of 15 and 25 is 40
Adding Floating-Point Numbers
Example: Add Two Floating-Point Numbers
#include <iostream>
using namespace std;
int main() {
float num1, num2;
cout << "Enter the first number: ";
cin >> num1;
cout << "Enter the second number: ";
cin >> num2;
float sum = num1 + num2;
cout << "The sum of " << num1 << " and " << num2 << " is " << sum << endl;
return 0;
}
Output:
Enter the first number: 12.5
Enter the second number: 7.3
The sum of 12.5 and 7.3 is 19.8
Using Functions to Add Numbers
Example: Function for Addition
#include <iostream>
using namespace std;
// Function to add two numbers
int add(int a, int b) {
return a + b;
}
int main() {
int num1, num2;
cout << "Enter the first number: ";
cin >> num1;
cout << "Enter the second number: ";
cin >> num2;
int sum = add(num1, num2);
cout << "The sum of " << num1 << " and " << num2 << " is " << sum << endl;
return 0;
}
Output:
Enter the first number: 30
Enter the second number: 45
The sum of 30 and 45 is 75
Adding Numbers Using Arrays
Example: Adding Elements of an Array
#include <iostream>
using namespace std;
int main() {
int numbers[] = {10, 20, 30, 40};
int sum = 0;
for (int i = 0; i < 4; i++) {
sum += numbers[i];
}
cout << "The sum of array elements is " << sum << endl;
return 0;
}
Output:
The sum of array elements is 100
Adding Numbers Using Loops
Example: Add Two Numbers Repeatedly
#include <iostream>
using namespace std;
int main() {
int num1, num2;
char choice;
do {
cout << "Enter the first number: ";
cin >> num1;
cout << "Enter the second number: ";
cin >> num2;
int sum = num1 + num2;
cout << "The sum of " << num1 << " and " << num2 << " is " << sum << endl;
cout << "Do you want to add more numbers? (y/n): ";
cin >> choice;
} while (choice == 'y' || choice == 'Y');
return 0;
}
Output:
Enter the first number: 5
Enter the second number: 10
The sum of 5 and 10 is 15
Do you want to add more numbers? (y/n): y
Enter the first number: 20
Enter the second number: 30
The sum of 20 and 30 is 50
Do you want to add more numbers? (y/n): n
Real-World Application Example
Example: Adding Prices of Items
#include <iostream>
using namespace std;
int main() {
int numItems;
double price, total = 0;
cout << "Enter the number of items: ";
cin >> numItems;
for (int i = 1; i <= numItems; i++) {
cout << "Enter price of item " << i << ": ";
cin >> price;
total += price;
}
cout << "The total price is $" << total << endl;
return 0;
}
Output:
Enter the number of items: 3
Enter price of item 1: 12.5
Enter price of item 2: 7.3
Enter price of item 3: 5.2
The total price is $25
Summary
- Adding two numbers in C++ can be done using simple expressions, user input, or functions.
- You can also add multiple numbers using arrays or loops.
- This operation is fundamental in programming and is applicable in real-world scenarios like adding prices or totals.
Learn More at The Coding College
Visit The Coding College for more tutorials on C++ and programming concepts.