C++ Examples : C++ is a powerful programming language that is widely used in the development of software, games, and other applications.
It offers a wide range of features, including built-in functions and libraries, which makes it a popular choice among programmers. In this article, we will discuss 10 C++ examples programs that are commonly used by programmers.
1) Swap Two Numbers
Swapping two numbers is a fundamental operation in programming. The following code swaps the values of two variables using a temporary variable:
#include <iostream> using namespace std; int main() { int num1 = 15; int num2 = 20; cout << "Before swapping, num1 = " << num1 << " and num2 = " << num2 << endl; // Swap the numbers using a temporary variable int temp = num1; num1 = num2; num2 = temp; cout << "After swapping, num1 = " << num1 << " and num2 = " << num2 << endl; return 0; }
output
Before swapping, a = 15 and b = 20 After swapping, a = 20 and b = 15
we first declare two integer variables num1 and num2 and initialize them with the values 15 and 20
Then, we print the values of num1 and num2 before swapping them.
Next, we declare a temporary variable called temp and assign it the value of num1. Then, we assign the value of num2 to num1 and the value of temp (which is the original value of num1) to num2. This effectively swaps the values of num1 and num2.
Finally, we print the values of num1 and num2 after swapping them to confirm that the swap was successful.
Also Read This :- what is file handling in c++
2 ) Find the Maximum of Two Numbers
#include <iostream> using namespace std; int main() { int num1 = 5; int num2 = 10; // Find the maximum of num1 and num2 using a conditional statement int maxNum = (num1 > num2) ? num1 : num2; cout << "The maximum of " << num1 << " and " << num2 << " is " << maxNum << endl; return 0; }
output
The maximum of 5 and 10 is 10
In this example, we first declare two integer variables num1 and num2 and initialize them with the values 5 and 10, respectively.
Next, we use a conditional statement to find the maximum of num1 and num2. The expression (num1 > num2) ? num1 : num2 checks if num1 is greater than num2. If this expression is true, then num1 is assigned to maxNum. Otherwise, num2 is assigned to maxNum.
Finally, we print the result of the maximum calculation to the console using cout.
This is a simple example of how to find the maximum of two numbers in C++. If you need to find the maximum of more than two numbers, you can use a loop or nested conditional statements to accomplish this task.
3) Calculate the Factorial of a Number
#include <iostream> using namespace std; int main() { int num, fact=1; num = 5; for(int i=1; i<=num; i++) { fact *= i; } cout << "Factorial of " << num << " = " << fact << endl; return 0; }
output
Factorial of 5 = 120
n this code, we first take the input number from the user using cin. Then, we use a for loop to multiply the numbers from 1 to num.
We initialize the fact variable to 1 outside the loop and update it in each iteration by multiplying it with the loop variable i. Finally, we output the value of fact as the factorial of the input number using cout.
4) Check if a Number is Prime
#include <iostream> using namespace std; int main() { int num; bool isPrime = true; cout << "Enter a positive integer: "; cin >> num; // Check if the number is prime if (num <= 1) { isPrime = false; } else { for (int i = 2; i <= num / 2; ++i) { if (num % i == 0) { isPrime = false; break; } } } if (isPrime) { cout << num << " is a prime number." << endl; } else { cout << num << " is not a prime number." << endl; } return 0; }
output
Enter a positive integer: 7 7 is a prime number.
In this example, we first declare an integer variable num and a boolean variable isPrime, which we initialize to true.
Next, we prompt the user to enter a positive integer using cout and store the user input in the num variable using cin.
Then, we use a conditional statement to check if the number is less than or equal to 1. If it is, then it is not a prime number and isPrime is set to false. Otherwise, we use a for loop to check if the number is divisible by any integer between 2 and num / 2. If it is, then it is not a prime number and isPrime is set to false. If none of the numbers in this range divide num, then it is a prime number and isPrime remains true.
Finally, we use another conditional statement to print whether the number is prime or not to the console using cout.
This is a simple example of how to check if a number is prime in C++. If you need to check if multiple numbers are prime, you can put this code in a loop to iterate over a list of numbers.
5) Generate Fibonacci Series
#include <iostream> using namespace std; int main() { int num, t1 = 0, t2 = 1, nextTerm = 0; cout << "Enter the number of terms: "; cin >> num; cout << "Fibonacci Series: "; for (int i = 1; i <= num; ++i) { if (i == 1) { cout << t1 << " "; continue; } if (i == 2) { cout << t2 << " "; continue; } nextTerm = t1 + t2; t1 = t2; t2 = nextTerm; cout << nextTerm << " "; } return 0; }
output
Enter the number of terms: 10 Fibonacci Series: 0 1 1 2 3 5 8 13 21 34
In this example, we first declare three integer variables: num, t1, and t2. t1 and t2 represent the first two terms of the Fibonacci series, which are initialized to 0 and 1, respectively.
The variable num represents the number of terms in the series that the user wants to generate.
Next, we prompt the user to enter the number of terms they want to generate using cout and store the user input in the num variable using cin.
Then, we use a for loop to generate the series. Inside the loop, we use conditional statements to handle the first two terms of the series, since they are not generated by adding two preceding terms.
After that, we use the formula next term = t1 + t2 to calculate the next term in the series, where t1 and t2 are the preceding terms.
We then update t1 and t2 to the current values of t2 and the next term, respectively, in preparation for the next iteration of the loop. Finally, we output the next term to the console using cout.
Finally, we use another cout statement to print the entire Fibonacci series to the console.
we will post part two of c++ examples as soon as possible
Very nice post. I just stumbled upon your weblog and wished to say that I’ve truly enjoyed surfing around your blog posts. After all I will be subscribing for your rss feed and I’m hoping you write again soon!