C++ Basic Programs with Solutions
This article will guide you through some basic C++ programs with solutions. These programs will help you understand fundamental concepts in C++ programming.
1. Hello World!
The classic first program in any programming language is "Hello World!". This simple program displays the message "Hello World!" on the screen.
Code:
#include
using namespace std;
int main() {
cout << "Hello World!" << endl;
return 0;
}
Explanation:
- #include <iostream>: This line includes the iostream library, which provides input/output functionalities.
- using namespace std;: This line brings the standard namespace into scope, allowing us to use elements like
cout
andendl
without explicitly specifyingstd::
. - int main(): This is the main function, where the program execution starts.
- cout << "Hello World!" << endl;: This line prints the message "Hello World!" to the console.
cout
is the standard output stream,<<
is the insertion operator used to send data to the output stream, andendl
inserts a newline character at the end of the output. - return 0;: This statement returns 0, indicating successful program execution.
2. Addition of Two Numbers
This program takes two numbers as input from the user and displays their sum.
Code:
#include
using namespace std;
int main() {
int num1, num2, sum;
cout << "Enter the first number: ";
cin >> num1;
cout << "Enter the second number: ";
cin >> num2;
sum = num1 + num2;
cout << "Sum = " << sum << endl;
return 0;
}
Explanation:
- int num1, num2, sum;: This line declares three integer variables to store the two input numbers and their sum.
- cin >> num1;: This line takes the first number from the user and stores it in the variable
num1
.cin
is the standard input stream, and>>
is the extraction operator used to read data from the input stream. - sum = num1 + num2;: This line calculates the sum of the two numbers and stores it in the variable
sum
. - cout << "Sum = " << sum << endl;: This line displays the calculated sum on the console.
3. Finding the Largest Number
This program takes two numbers as input from the user and displays the largest number.
Code:
#include
using namespace std;
int main() {
int num1, num2, largest;
cout << "Enter the first number: ";
cin >> num1;
cout << "Enter the second number: ";
cin >> num2;
if (num1 > num2) {
largest = num1;
} else {
largest = num2;
}
cout << "Largest number = " << largest << endl;
return 0;
}
Explanation:
- if (num1 > num2) { ... } else { ... }: This conditional statement checks if the first number is greater than the second number. If it is, the
largest
variable is assigned the value ofnum1
. Otherwise,largest
is assigned the value ofnum2
.
4. Factorial of a Number
This program takes a positive integer as input and calculates its factorial.
Code:
#include
using namespace std;
int main() {
int num, factorial = 1;
cout << "Enter a positive integer: ";
cin >> num;
for (int i = 1; i <= num; ++i) {
factorial *= i;
}
cout << "Factorial of " << num << " = " << factorial << endl;
return 0;
}
Explanation:
- for (int i = 1; i <= num; ++i) { ... }: This loop iterates from 1 to the input number.
- factorial *= i;: In each iteration, the factorial variable is multiplied by the current loop counter
i
.
5. Checking Prime Number
This program takes a positive integer as input and checks if it is a prime number.
Code:
#include
using namespace std;
int main() {
int num, i, isPrime = 1;
cout << "Enter a positive integer: ";
cin >> num;
for (i = 2; i <= num / 2; ++i) {
if (num % i == 0) {
isPrime = 0;
break;
}
}
if (isPrime == 1) {
cout << num << " is a prime number." << endl;
} else {
cout << num << " is not a prime number." << endl;
}
return 0;
}
Explanation:
- for (i = 2; i <= num / 2; ++i) { ... }: This loop iterates from 2 to half of the input number.
- if (num % i == 0) { ... }: This condition checks if the input number is divisible by the current loop counter
i
. If it is, the number is not prime, and theisPrime
flag is set to 0. - break;: This statement exits the loop if the number is not prime.
These basic C++ programs provide a good starting point for learning the language. As you progress, you can explore more complex programs and concepts.