Practice Questions

Overview#

Print your name
my_name.cpp
#include <iostream>
int main() {
// Change string "YOUR NAME HERE" to your name
std::cout << "YOUR NAME HERE" << std::endl;
return 0;
}
Print your name, age, hobbies
bio.cpp
#include <iostream>
int main() {
std::cout << "C++\n"; // \n is new line
std::cout << 25 << std::endl;
std::cout << "Coding" << std::endl;
return 0;
}

Variables#

Create a variable to store your favourite number and print it
fav_num.cpp
#include <iostream>
using namespace std;
int main() {
// declare and initialize a variable
int favNum = 3;
// printing the variable value
cout << favNum << endl;
return 0;
}
Create variables to store your name, favourite number and print it
fav_num.cpp
#include <iostream>
using namespace std;
int main() {
// declare and initialize variables
string myName = "C++";
int favNum = 3;
// printing the variable value
cout << myName << endl;
cout << favNum << endl;
return 0;
}
Swap values of two variables
swap_values.cpp
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 20;
cout << "Before Swapping... A: " << a << " B: " <<b << endl;
// swapping values using third variable temp
int temp = a;
a = b;
b = temp;
cout << "After Swapping... A: " << a << " B: " <<b << endl;
}
tip
Swap the values of two variables without using third variable
swap_values2.cpp
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 20;
cout << "Before Swapping... A: " << a << " B: " <<b << endl;
// swapping values wihtout using third variable
a = a + b;
b = a - b;
a = a - b;
cout << "After Swapping... A: " << a << " B: " <<b << endl;
}

Input/Output#

Take input of favourite number and print it on screen
numInput.cpp
#include <iostream>
using namespace std;
int main() {
int favNum;
cout << "Enter yout fav.number: ";
cin >> favNum;
cout << favNum << endl;
return 0;
}
Take full name of user and age of user as inputs and print them
stringInput.cpp
#include <iostream>
using namespace std;
int main() {
string name;
cout << "Enter your name : ";
// using getline since cin considers space as termination of input
getline(cin, name);
int age;
cout << "Enter your age: ";
cin >> age;
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
return 0;
}

Conditionals#

Check the given number is odd or even and print
odd_even.cpp
#include <iostream>
using namespace std;
int main() {
// even odd and zero is neither even nor odd
int n;
cin >> n;
if (n != 0) {
if (n % 2 == 0) {
cout << n <<" is even" << endl;
} else {
cout << n <<" is odd" << endl;
}
} else {
cout << n <<" is neither odd nor even." << endl;
}
return 0;
}
Largest among three numbers
largest3.cpp
#include <iostream>
using namespace std;
int main() {
float a, b, c;
cout << "Enter three numbers: ";
cin >> a >> b >> c;
if (a >= b) {
if (a >= c) {
cout << a <<" is the largest number." << endl;
} else {
cout << c <<" is the largest number." << endl;
}
} else {
if (b >= c) {
cout << b <<" is the largest number." << endl;
} else {
cout << c <<" is the largest number." << endl;
}
}
/** // using logical operators in conditions
if (a >= b && a >= c) {
cout << a <<" is the largest number." << endl;
}
else if (b >= a && b >= c) {
cout << b <<" is the largest number." << endl;
}
else {
cout << c <<" is the largest number." << endl;
}
**/
}
check whether the triangle formed by the given sides(inputs) is equilateral, isosceles, or scalene.
triangle.cpp
#include <iostream>
using namespace std;
int main() {
int a, b, c;
cout << "Enter three sides of the triangle: ";
cin >> a >> b >> c;
// Check for equilateral triangle
if (a == b && b == c) { //a = b = c
cout << "Equilateral Triangle";
}
// Check for isoceles triangle
else if (a == b || b == c || c == a) { // a=b or b = c or c = a
cout << "Isoceles Triangle";
}
// Otherwise scalene triangle
else {
cout << "Scalene Triangle";
}
return 0;
}
Given coefficients of a quadratic equation , you need to print the nature of the roots (Real and Distinct , Real and Equal or Imaginary) and the roots.

If Real and Distinct , print the roots in increasing order. If Real and Equal , print the same repeating root twice If Imaginary , no need to print the roots.

Note : Print only the integer part of the roots.

quadratic_equation.cpp
#include <cmath>
#include <iostream>
using namespace std;
int main() {
int a, b, c, d;
cin >> a >> b >> c;
d = (b * b) - (4 * a * c);
int root1 = (sqrt(d) - b) / (2 * a);
int root2 = -1 * (sqrt(d) + b) / (2 * a);
// when b2 − 4ac is positive, we get two Real solutions
if (d > 0 && a != 0) {
cout << "Real and Distinct" << endl;
cout << root2 << " " << root1 << endl;
}
// when it is zero we get just ONE real solution (both answers are the same)
else if (d == 0 && a != 0) {
cout << "Real and Equal" << endl;
cout << root1 << " " << root1 << endl;
}
// when it is negative we get a pair of Complex solutions
else {
cout << "Imaginary" << endl;
}
return 0;
}

Loops#

tip

Check out for pattern questions on GeeksForGeeks or Here

Read N numbers and print their average. HINT: You would be given first N, and then N integers of the list.
average.cpp
#include <iostream>
using namespace std;
int main() {
int N, num, total= 0;
cin >> N;
int count = N;
while(count--) {
cin >> num;
total += num;
}
cout << "Average :" << (total/N) << endl;
return 0;
}
Given a number check if it is a member of Fibonacci sequence or not?
is_Fibonacci.cpp
#include <iostream>
using namespace std;
int main() {
int n, a = 0, b = 1, c = a + b;
cin >> n;
if(n == a || n == b) {
cout << "Fibonacci Sequence Number" << endl;
return 0;
}
while(c <= n) {
if (n == c) {
cout << "Fibonacci Sequence Number" << endl;
return 0;
}
a = b;
b = c;
c = a + b;
}
cout << "Not a Fibonacci Sequence Number" << endl;
return 0;
}
Given a number N, find sum of its digits
sum_of_digits.cpp
#include <iostream>
using namespace std;
int main() {
long long n;
int rem, total = 0;
cin >> n;
while (n > 0) {
rem = n % 10;
total += rem;
n /= 10;
}
cout << total << endl;
return 0;
}

Patterns#

Print the following pattern (square)
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
pattern.cpp
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cout << "* ";
}
cout << endl;
}
return 0;
}
Print the following pattern (Rectangle)
*******
*******
*******
*******
pattern.cpp
#include <iostream>
using namespace std;
int main() {
int row, col;
cin >> row >> col;
for (int i = 1; i <= row; i++) {
for (int j = 1; j <= col; j++) {
cout << "*";
}
cout << endl;
}
return 0;
}
Print the following pattern (Hollow Rectangle)
********
* *
* *
********
pattern.cpp
#include <iostream>
using namespace std;
int main() {
int row, col;
cin >> row >> col;
for (int i = 1; i <= row; i++) {
for (int j = 1; j <= col; j++) {
if (i == 1 || i == row || j == 1 || j == col) {
cout << "*";
} else {
cout << " ";
}
}
cout << endl;
}
return 0;
}
Print the following pattern
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
pattern.cpp
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
cout << i << " ";
}
cout << endl;
}
return 0;
}
Print the following pattern
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
pattern.cpp
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n + 1 - i; j++) {
cout << j << " ";
}
cout << endl;
}
return 0;
}
Print the following pattern (Floyd's Triangle)
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
pattern.cpp
#include <iostream>
using namespace std;
int main() {
int n, count = 1;
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
cout << count << " ";
count++;
}
cout << endl;
}
return 0;
}
Print the following pattern (Half Pyramid after 180°)
*
**
***
****
*****
pattern.cpp
#include <iostream>
using namespace std;
int main() {
// half pyramid after 180 deree rotation
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (j <= n - i) {
cout << " ";
} else {
cout << "*";
}
}
cout << endl;
}
return 0;
}
Print the following pattern (Inverted Half Pyramid)
* * * * *
* * * *
* * *
* *
*
pattern.cpp
#include <iostream>
using namespace std;
int main() {
// Inverted half pyramid
int n;
cin >> n;
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
cout << "* ";
}
cout << endl;
}
return 0;
}
Print the following pattern (0-1 pattern)
1
0 1
1 0 1
0 1 0 1
1 0 1 0 1
pattern.cpp
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
if ((i + j) % 2 == 0) {
cout << "1 ";
} else {
cout << "0 ";
}
}
cout << endl;
}
return 0;
}
Print the following pattern (Rhombus)
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
pattern.cpp
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
cout << " ";
}
for (int j = 1; j <= n; j++) {
cout << "* ";
}
cout << endl;
}
return 0;
}
Print the following pattern (Pyramid Numbers)
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
pattern.cpp
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j < n + 1 - i; j++) {
cout << " ";
}
for (int j = 1; j <= i; j++) {
cout << j << " ";
}
cout << endl;
}
return 0;
}

Functions#

Write a function to reverse the given number N as input.
reverse_num.cpp
#include <iostream>
using namespace std;
int reverse(int num) {
int rev_num = 0;
while (num > 0) {
rev_num = rev_num * 10 + num % 10;
num = num / 10;
}
return rev_num;
}
int main() {
int num;
cin >> num;
int ans = 0;
while (num > 0) {
ans = ans * 10 + num % 10;
num = num / 10;
}
cout << ans << endl;
return 0;
}
Last updated on by Rashik Ansar