In this tutorial, we will learn to solve some of the C++ basic questions for practice. These basic questions are well suited for practice, especially for beginners. Write these questions on your compiler and solve them by yourself. This tutorial consists of several frequently asked questions in C++ programming during interviews and technical rounds.
Also Read: Digital Marketing Executive Job Description: Roles and Responsibilities Explained
C++ Basic Exercise List Of Questions
Are you starting your journey with C++ programming language? This tutorial consists of several C++ basic Questions along with solutions. Try to solve them by yourself and then go to the solutions to verify whether they are right or not. We are first listing the questions and then providing the solutions in the below sections.
- Write a C++ basic program to print “Hello, World!” on a new line.
- Write a C++ basic program to calculate the product of two numbers and display the result.
- Write a C++ basic program to find the size of various pointer types.
- Write a C++ basic program to calculate the difference between two numbers using variables.
- Write a C++ basic program to display the upper and lower limits of float and double types.
- Write a C++ basic program to check if a student’s grade falls within valid limits.
- Write a C++ basic program to perform arithmetic operations (addition, subtraction, multiplication) on three numbers.
- Write a C++ basic program to check for overflow in integer addition.
- Write a C++ basic program to demonstrate pre and post-increment and decrement operations.
- Write a C++ basic program to display a number with 2 decimal places.
- Write a C++ basic program to calculate the square of a number.
- Write a C++ basic program to swap two numbers without using a temporary variable.
- Write a C++ basic program to check whether a number is even or odd.
- Write a C++ basic program to find the ASCII value of a character.
- Write a C++ basic program to calculate the sum of the first n natural numbers.
- Write a C++ basic program to check if a number is prime.
- Write a C++ basic program to reverse a given number.
- Write a C++ basic program to calculate the factorial of a number.
- Write a C++ basic program to check if a string is a palindrome.
- Write a C++ basic program to find the largest number in an array.
Also Read: The Top DevOps Benefits: Why Its Crucial for Your Business
C++ Basic Exercise With Solutions
You can find the solutions for the C++ basic problems given above if you are not getting where to start. You can also use these solutions to verify whether your solution is correct or not.
1. Write a C++ basic program to print “Hello, World!” on a new line.
We can easily print “Hello World!” on the screen using the cout object in C++.
#include <iostream>
using namespace std; int main() { cout << “Hello, World!” << endl; return 0; } |
2. Write a C++ program to calculate the product of two numbers and display the result.
We have to calculate the product of two numbers ad print the result. Suppose the numbers are 3 and 2 then the product of the numbers must be 6. Store 3 in a variable x and 2 in a variable y, and now perform the multiplication and store the result in a separate variable, say z, and print the output.
#include <iostream>
using namespace std; int main() { int num1 = 15, num2 = 12; cout << “The product of ” << num1 << ” and ” << num2 << ” is: ” << num1 * num2 << endl; return 0; } |
3. Write a C++ program to find the size of various pointer types
To find the data types in C++ programming language, we can use the sizeof() library extension and display the result.
#include <iostream>
using namespace std; int main() { cout << “Size of int pointer: ” << sizeof(int*) << ” bytes” << endl; cout << “Size of float pointer: ” << sizeof(float*) << ” bytes” << endl; cout << “Size of double pointer: ” << sizeof(double*) << ” bytes” << endl; cout << “Size of char pointer: ” << sizeof(char*) << ” bytes” << endl; return 0; } |
4. Write a basic C++ program to calculate the difference between two numbers using variables.
We have to calculate the difference between two numbers and print the result. Suppose the numbers are 9 and 7 then the product of the numbers must be 2. Store 9 in a variable x and 7 in a variable y, and now perform the multiplication and store the result in a separate variable, say z, and print the output.
Note: Make sure to keep the greater number first or use mod to avoid negative results in the output.
#include <iostream>
using namespace std; int main() { int a = 50, b = 20; int difference = a – b; cout << “The difference between ” << a << ” and ” << b << ” is: ” << difference << endl; return 0; } |
5. Write a basic C++ program to display the upper and lower limits of float and double types.
You can use the predefined functions in C++ language to print the upper and lower limits of a data type, such as FLT_MAX, FLT_MIN, DBL_MAX, DBL_MIN, etc
#include <iostream>
#include <cfloat> using namespace std; int main() { cout << “Maximum value of float: ” << FLT_MAX << endl; cout << “Minimum value of float: ” << FLT_MIN << endl; cout << “Maximum value of double: ” << DBL_MAX << endl; cout << “Minimum value of double: ” << DBL_MIN << endl; return 0; } |
6. Write a C++ program to check if a student’s grade falls within valid limits.
You can use the if and else conditional statements to solve this basic problem using C++ language.
#include <iostream>
using namespace std; int main() { char grade = ‘A’; if (grade >= ‘A’ && grade <= ‘F’) { cout << “The grade ” << grade << ” is valid.” << endl; } else { cout << “The grade ” << grade << ” is invalid.” << endl; } return 0; } |
7. Write a C++ program to perform arithmetic operations (addition, subtraction, multiplication) on three numbers.
You only have to display the output of sum, difference, and multiplication over three numbers using the C++ programming language.
#include <iostream>
using namespace std; int main() { int x = 10, y = 20, z = 30; cout << “Sum: ” << x + y + z << endl; cout << “Product: ” << x * y * z << endl; cout << “Difference: ” << x – y – z << endl; return 0; } |
8. Write a C++ program to check for overflow in integer addition.
This C++ basic problem can be solved by using INT_MAX and conditional statements in C++ programming.
#include <iostream>
#include <climits> using namespace std; int main() { int a = INT_MAX, b = 1; if (a > INT_MAX – b) { cout << “Overflow detected in addition!” << endl; } else { cout << “Sum: ” << a + b << endl; } return 0; } |
9. Write a C++ program to demonstrate pre and post-increment and decrement operations.
The increment and decrement problem is a common problem where you have to keep increasing the number or decreasing the given number by one. You can use the increment and decrement operators to solve this problem using C++.
Note: Carefully place the increment and decrement operator with the variable.
#include <iostream>
using namespace std; int main() { int num = 5; cout << “Initial value: ” << num << endl; cout << “Post-increment: ” << num++ << endl; cout << “After post-increment: ” << num << endl; cout << “Pre-decrement: ” << –num << endl; cout << “After pre-decrement: ” << num << endl; return 0; } |
10. Write a C++ program to display a number with 2 decimal places.
This problem can easily be solved using the predefined “setprecision()” method in C++ programming language.
#include <iostream>
#include <iomanip> using namespace std; int main() { double num = 3.14159; cout << “Value: ” << fixed << setprecision(2) << num << endl; return 0; } |
11. Write a C++ program to calculate the square of a number.
You can either use a multiplication method to solve the square problem or use a predefined method in C++ programming language to solve this problem.
#include <iostream>
using namespace std; int main() { int num = 8; cout << “Square of ” << num << ” is: ” << num * num << endl; return 0; } |
12. Write a C++ program to swap two numbers without using a temporary variable.
We can easily swap numbers using the arithmetic method, and check the solution using C++ programming below.
#include <iostream>
using namespace std; int main() { int a = 5, b = 10; cout << “Before Swap: a = ” << a << “, b = ” << b << endl; a = a + b; b = a – b; a = a – b; cout << “After Swap: a = ” << a << “, b = ” << b << endl; return 0; } |
13. Write a C++ program to check whether a number is even or odd.
A number is even when it can completely be divided by 2 and it is odd when it can be divided by 3.
#include <iostream>
using namespace std; int main() { int num = 7; if (num % 2 == 0) { cout << num << ” is even.” << endl; } else { cout << num << ” is odd.” << endl; } return 0; } |
14. Write a C++ program to find the ASCII value of a character.
#include <iostream>
using namespace std; int main() { char ch = ‘A’; cout << “ASCII value of ” << ch << ” is: ” << int(ch) << endl; return 0; } |
15. Write a C++ program to calculate the sum of the first n natural numbers.
#include <iostream>
using namespace std; int main() { int n = 10, sum = 0; for (int i = 1; i <= n; ++i) { sum += i; } cout << “Sum of first ” << n << ” natural numbers is: ” << sum << endl; return 0; } |
16. Write a C++ program to check if a number is prime.
A Prime number is only divisible by 1 or the number itself. Use this property to solve this problem using the C++ language.
#include <iostream>
using namespace std; int main() { int num = 29; bool isPrime = true; if (num <= 1) { isPrime = false; } 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; } |
17. Write a C++ program to reverse a given number.
You can reverse a number using the C++ programming language.
#include <iostream>
using namespace std; int main() { int num = 12345, reversed = 0; while (num != 0) { reversed = reversed * 10 + num % 10; num /= 10; } cout << “Reversed number: ” << reversed << endl; return 0; } |
18. Write a C++ program to calculate the factorial of a number.
#include <iostream>
using namespace std; int main() { int num = 5, factorial = 1; for (int i = 1; i <= num; ++i) { factorial *= i; } cout << “Factorial of ” << num << ” is: ” << factorial << endl; return 0; } |
19. Write a C++ program to check if a string is a palindrome.
#include <iostream>
#include <string> using namespace std; int main() { string str = “madam”, reversed = string(str.rbegin(), str.rend()); if (str == reversed) cout << str << ” is a palindrome.” << endl; else cout << str << ” is not a palindrome.” << endl; return 0; } |
20. Write a C++ program to find the largest number in an array.
#include <iostream>
using namespace std; int main() { int arr[] = {10, 20, 5, 50, 30}; int n = sizeof(arr) / sizeof(arr[0]); int largest = arr[0]; for (int i = 1; i < n; ++i) { if (arr[i] > largest) { largest = arr[i]; } } cout << “Largest number is: ” << largest << endl; return 0; } |
Learn C++ Programming With PW Skills
Become a proficient C++ programmer and learn to solve competitive-level questions with interactive and in-depth tutorials for C++ programming language with our Decode DSA With C++ Course. Solve real world case problems, build projects, rectify your doubts, and much more within this self paced course. Get a course completion certificate, job ready portfolio, and much more only with pwskills.com
Ans: C++ is an object oriented programming language with objects, classes, polymorphism, abstraction, and encapsulation. Ans: You can solve C++ programming practice questions and build projects to learn C++ programming. Use an online compiler and produce the code using C++ programming. You can also join a certification course in C++. Ans: C++ is a popular programming language that can be used to solve various problems. C++ offers object oriented programming, STL libraries and is used in the development of gaming applications, Android applications, etc. Ans: You can become proficient in C++ by solving practice problems using C++. Understand the basics and master concepts of the C++ program. C++ Basic FAQs
Q1. Is C++ an object-oriented programming language?
Q2. How to learn C++ programming?
Q3. Is C++ a programming language?
Q4. How to become proficient in C++ programming language?