basic c++ question set (25) Day 1
basic c++ question set (25) Day 1
Very Easy
Calculate the sum of all natural numbers from 1 to n, where n is a positive integer. Use the
formula:
Sum=n×(n+1)/2 .
Take n as input and output the sum of natural numbers from 1 to n .
Task
Input Format
Constraints
● 1 ≤n ≤104.
Output Format
Test Cases:
Example 1
Input:
Output:
15
Explanation:
Using the formula, Sum=5×(5+1)/2=15 .
Example 2
Input:
100
Output:
5050
Explanation:
Using the formula, Sum=100×(100+1)/2=5050 .
Example 3
Input:
Output:
Explanation:
Using the formula, Sum=1×(1+1)/2=1 .
Objective
Check if a given number n is a prime number. A prime number is a natural number greater than 1
that has no positive divisors other than 1 and itself.
Task
Given an integer n, print "Prime" if the number is prime, or "Not Prime" if it is not.
Input Format
One integer n.
Constraints
● 2 ≤n ≤ 105
Output Format
Test Cases:
Example 1
Input:
Output:
Prime
Explanation:
7 has no divisors other than 1 and itself, so it is a prime number.
Example 2
Input:
Output:
Not Prime
Explanation:
9 is divisible by 3, so it is not a prime number.
Example 3
Input:
Output:
Prime
Explanation:
2 is a prime number as it has only two divisors: 1 and 2.
3) Print Odd Numbers up to N
Objective
Print all odd numbers between 1 and n, inclusive. Odd numbers are integers that are not divisible
by 2. These numbers should be printed in ascending order, separated by spaces.
Task
Input Format
Constraints
● 1 ≤n ≤104
Output Format
Test Cases:
Example 1
Input:
10
Output:
1 3 5 7 9
Example 2
Input:
7
Output:
1 3 5 7
Example 3
Input:
Output:
Objective
Calculate the sum of all odd numbers from 1 to n. An odd number is an integer that is not
divisible by 2.The sum of odd numbers, iterate through all the numbers from 1 to n, check if each
number is odd, and accumulate the sum.
Task
Input Format
Constraints
● 1 ≤n ≤104
Output Format
Test Cases:
Example 1:
Input:
Output:
9
Explanation:
The odd numbers are 1, 3, 5. Their sum is 1+3+5=9.
Example 2:
Input:
10
Output:
25
Explanation:
The odd numbers are 1, 3, 5, 7, 9. Their sum is 1+3+5+7+9=25.
Example 3:
Input:
Output:
Explanation:
The only odd number is 1.
Objective
Print the multiplication table of a given number n. A multiplication table for a number n is a list
of products of n with integers from 1 to 10. For example, the multiplication table for 3 is:
3×1=3,3×2=6,…,3×10=30.
Task
Input Format
One integer n.
Constraints
● 1≤n≤100
Output Format
For each integer i from 1 to 10, print the product n×i in the format:
n×i=product.
Test Cases
Example 1:
Input:
3
Output:
Copy code
3x1=3
3x2=6
3x3=9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
3 x 10 = 30
Example 2:
Input:
7
Output:
7x1=7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
Example 3:
Input:
10
Output:
10 x 1 = 10
10 x 2 = 20
10 x 3 = 30
10 x 4 = 40
10 x 5 = 50
10 x 6 = 60
10 x 7 = 70
10 x 8 = 80
10 x 9 = 90
10 x 10 = 100
Easy:
Objective
Count the total number of digits in a given number n. The number can be a positive integer. For
example, for the number 12345, the count of digits is 5. For a number like 900000, the count of
digits is 6.
Given an integer n, your task is to determine how many digits are present in n. This task will
help you practice working with loops, number manipulation, and conditional logic.
Task
Input Format
One integer n.
Constraints
● 1≤n≤109
Output Format
Test Cases
Example 1:
Input:
12345
Output:
Explanation:
The number 12345 has 5 digits: 1, 2, 3, 4, 5.
Example 2:
Input:
900000
Output:
Explanation:
The number 900000 has 6 digits: 9, 0, 0, 0, 0, 0.
Example 3:
Input:
Output:
Explanation:
The number 1 has only 1 digit.
2) Reverse a Number
Objective
Reverse the digits of a given number n. For example, if the input number is 12345, the output
should be 54321. The task involves using loops and modulus operators to extract the digits and
construct the reversed number.
Task
Given an integer n, print the number with its digits in reverse order.
Input Format
One integer n.
Constraints
● 1≤n≤109
Output Format
Test Cases
Example 1:
Input:
12345
Output:
54321
Explanation:
The digits of 12345 in reverse order are 54321.
Example 2:
Input:
9876
Output:
6789
Explanation:
The digits of 9876 in reverse order are 6789.
Example 3:
Input:
1000
Output:
1
Explanation:
The digits of 1000 in reverse order are 0001, which is equivalent to 1.
Objective
Find the largest digit in a given number n. For example, for the number 2734, the largest digit is
7. You need to extract each digit from the number and determine the largest one. The task will
involve using loops and modulus operations to isolate the digits.
Task
Input Format
One integer n.
Constraints
● 1≤n≤109
Output Format
Test Cases
Example 1:
Input:
2734
Output:
Explanation:
The digits of 2734 are 2, 7, 3, and 4. The largest digit is 7.
Example 2:
Input:
9450
Output:
9
Explanation:
The digits of 9450 are 9, 4, 5, and 0. The largest digit is 9.
Example 3:
Input:
1111
Output:
Explanation:
All digits of 1111 are 1, so the largest digit is also 1.
Objective
Task
Given an integer n, print "Palindrome" if the number is a palindrome, otherwise print "Not
Palindrome".
Input Format
One integer n.
Constraints
● 1≤n≤109
Output Format
Explanation
For input n=121n = 121n=121, the number is the same when reversed, so it is a palindrome.
For input n=12345n = 12345n=12345, the number is not the same when reversed, so it is not a
palindrome.
Test Cases
Example 1
Input:
121
Output:
Palindrome
Explanation:
The number 121 reads the same backward as forward.
Example 2:
Input:
12345
Output:
Not Palindrome
Explanation:
The number 12345 does not read the same backward as forward.
Example 3:
Input:
12321
Output:
Palindrome
Explanation:
The number 12321 reads the same backward as forward.
Calculate the sum of the digits of a given number n. For example, for the number 12345, the sum
of the digits is 1+2+3+4+5=15. To solve this, you will need to extract each digit from the
number and calculate the total sum.
Task
Input Format
One integer n.
Constraints
● 1≤n≤109
Output Format
Test Cases
Example 1
Input:
12345
Output:
15
Explanation:
The digits of 12345 are 1, 2, 3, 4, and 5. The sum is 1 + 2 + 3 + 4 + 5 = 15.
Example 2:
Input:
4567
Output:
22
Explanation:
The digits of 4567 are 4, 5, 6, and 7. The sum is 4 + 5 + 6 + 7 = 22.
Example 3:
Input:
999
Output:
27
Explanation:
The digits of 999 are 9, 9, and 9. The sum is 9 + 9 + 9 = 27.
Medium:
Write a program to calculate the area of different shapes using function overloading. Implement
overloaded functions to compute the area of a circle, a rectangle, and a triangle.
Input Format
The program should accept:
1. Radius of the circle for the first function.
2. Length and breadth of the rectangle for the second function.
3. Base and height of the triangle for the third function.
Constraints
1 ≤ radius, length, breadth, base, height ≤ 10 3
Use 3.14159 for the value of π.
Output Format
Print the computed area of each shape in a new line.
Test Cases:
Example 1
Input:
Radius = 5
Length = 4, breadth = 6
Base = 3, height = 7
Output:
78.53975
24
10.5
Explanation:
● The area of the circle with radius 5 is 3.14159* 52 = 78.53975.
● The area of the triangle with base 3 and height 7 is 0.5*3*7 = 10.5.
Example 2
Input:
Radius = 10
Length = 15, breadth = 8
Base = 12, height = 9
Output:
314.159
120
54
Explanation:
- The area of the circle with radius 10 is 3.14159 * 102 = 314.159.
- The area of the rectangle with length 15 and breadth 8 is 15* 8 = 120.
- The area of the triangle with base 12 and height 9 is 0.5* 12 * 9 = 54.
Example 3
Input:
Radius = 1
length = 2, breadth = 3
Base = 5, height = 8
Output:
3.14159
6
20
Explanation:
The area of the circle with radius 1 is 3.14159 * 12 = 3.14159 .
The area of the rectangle with length 2 and breadth 3 is 2* 3 = 6.
The area of the triangle with base 5 and height 8 is 0.5 *5 * 8 = 20.
Input Format:
The program should accept:
Constraints
Output Format
Print the calculated salary for each level of the hierarchy on a new line.
Test Cases:
Example 1
Input:
Stipend = 10000
base salary = 50000, bonuses = 20000
base salary = 80000, bonuses = 30000, incentives = 20000
Output:
Intern Salary: 10000
Employee Salary: 70000
Manager Salary: 130000
Explanation:
Example 3
Input:
stipend = 5000
base_salary = 45000, bonuses = 15000
base_salary = 70000, bonuses = 20000, incentives = 10000
Output:
Intern Salary: 5000
Employee Salary: 60000
Manager Salary: 100000
Input Format
The program should accept:
1. Employee ID as an integer.
2. Employee Name as a string.
3. Employee Salary as a floating-point number.
Constraints
● 1≤ Employee ID ≤ 106.
● Name can have up to 50 characters.
● 1.0 ≤Salary ≤ 107.
Output Format
Print the employee details, including ID, Name, and Salary, on separate lines.
Test Cases:
Example 1
Input:
ID = 101
Name = John Doe
Salary = 75000.5
Output:
Employee ID: 101
Employee Name: John Doe
Employee Salary: 75000.5
Explanation:
Encapsulation ensures that employee details are accessed and modified only through public
methods.
Example 2
Input:
ID = 202
Name = Jane Smith
Salary = 85000.75
Output:
Employee ID: 202
Employee Name: Jane Smith
Employee Salary: 85000.75
Example 3
Input:
ID = 303
Name = Robert Brown
Salary = 67000.0
Output:
Employee ID: 303
Employee Name: Robert Brown
Employee Salary: 67000.0
Objective
Create a program that demonstrates inheritance by defining:
- A base class Student to store details like Roll Number and Name.
- A derived class Result to store marks for three subjects and calculate the total and
percentage.
Input Format
The program should accept:
Constraints
● 1 ≤ Roll Number ≤ 10 6.
Output Format
Print the student details, marks, total, and percentage.
Test Cases:
Example 1
Input:
Roll Number = 101
Name = Alice Smith
Marks = 85, 90, 80
Output:
Roll Number: 101
Name: Alice Smith
Marks: 85, 90, 80
Total: 255
Percentage: 85.0%
Explanation:
The Result class inherits Student details and calculates Total=85 + 90 + 80 and Percentage =
(255/300) × 100.
Example 2
Input:
Roll Number = 202
Name = Bob Martin
Marks = 70, 75, 65
Output:
Roll Number: 202
Name: Bob Martin
Marks: 70, 75, 65
Total: 210
Percentage: 70.0%
Example 3
Input:
Roll Number = 303
Name = Clara Brown
Marks = 95, 92, 88
Output:
Roll Number: 303
Name: Clara Brown
Marks: 95, 92, 88
Total: 275
Percentage: 91.67%
Constraints
Output Format
Print the area of each shape on separate lines, rounded to 2 decimal places.
Test Cases:
Example 1
Input:
Radius = 7
Length = 10, Breadth = 5
Base = 8, Height = 6
Output:
Circle Area: 153.94
Rectangle Area: 50.00
Triangle Area: 24.00
Explanation:
Example 2:
Input:
Radius = 5
Length = 12, Breadth = 8
Base = 15, Height = 10
Output:
Circle Area: 78.54
Rectangle Area: 96.00
Triangle Area: 75.00
Example 3:
Input:
Radius = 10
Length = 20, Breadth = 15
Base = 10, Height = 12
Output:
Circle Area: 314.16
Rectangle Area: 300.00
Triangle Area: 60.00
Hard:
1)Implementing Polymorphism for Shape Hierarchies.
Objective
Write a program to demonstrate runtime polymorphism in C++ using a base class Shape and
derived classes Circle, Rectangle, and Triangle. The program should use virtual functions to
calculate and print the area of each shape based on user input.
Input Format
The program should accept:
Constraints
Output Format
Print the computed area of each shape on a new line.
Test Cases:
Example 1
Input:
Radius = 5
Length = 4, breadth = 6
Base = 3, height = 7
Output:
Area of Circle: 78.53975
Area of Rectangle: 24
Area of Triangle: 10.5
Explanation:
Example 2
Input:
Radius = 10
Length = 15, breadth = 8
Base = 12, height = 9
Output:
Area of Circle: 314.159
Area of Rectangle: 120
Area of Triangle: 54
Explanation:
Example 2
Input:
Radius = 1
Length = 2, breadth = 3
Base = 5, height = 8
Output:
Area of Circle: 3.14159
Area of Rectangle: 6
Area of Triangle: 20
Explanation:
Constraints
● 1 ≤ m, n ,p ≤ 10.
● Matrix elements are integers in the range −100 to 100.
Output Format
Test Cases:
Example 1: Matrix Addition
Input:
Matrix A:
12
34
Matrix B:
56
78
Operation: 1
Output:
68
10 12
Example 2: Matrix Multiplication
Input:
Matrix A:
123
456
Matrix B:
78
9 10
11 12
Operation: 2
Output:
58 64
139 154
Objective:
Design a C++ program using polymorphism to calculate the area of different shapes:
A Rectangle (Area = Length × Breadth).
A Circle (Area = π × Radius²).
A Triangle (Area = ½ × Base × Height).
Create a base class Shape with a pure virtual function getArea(). Use derived classes Rectangle,
Circle, and Triangle to override this function.
Input Format
Constraints
Test Cases:
Example 1: Rectangle Area
Input:
Shape Type: 1
Length: 5
Breadth: 4
Output:
The area of the rectangle is: 20
Constraints
Test Cases:
Example 1: Rectangle Area
Input:
Book Details:
Title: C++ Basics
Author: John Doe
ISBN: 1234
Borrower Details:
Name: Alice
ID: 42
Output:
Borrower Alice (ID: 42) has borrowed "C++ Basics" by John Doe (ISBN: 1234).
Input Format
Constraints
Test Cases:
Example 1: Savings Account Interest
Input:
Account Type: 1
Balance: 10000
Interest Rate: 5
Time: 3
Output:
Savings Account Interest: 1500
Very Hard
1)Hierarchical Inheritance for Employee Management System
Objective
Create a C++ program to simulate an employee management system using hierarchical
inheritance. Design a base class Employee that stores basic details (name, ID, and salary). Create
two derived classes:
Manager: Add and calculate bonuses based on performance ratings.
Developer: Add and calculate overtime compensation based on extra hours worked.
The program should allow input for both types of employees and display their total earnings.
Input Format
Constraints
Input Format
1. Vehicle Type (1 for Car, 2 for Electric Car).
2. Brand (string), Model (string), and Mileage (double).
3. For Car: Fuel (gallons) and Distance Covered (miles).
4. For Electric Car: Battery Capacity (kWh), Efficiency (miles per kWh).
Constraints
Test Cases:
Example 1: Fuel Efficiency for Car
Input:
Vehicle Type: 1
Brand: Toyota
Model: Corolla
Mileage: 30000
Fuel: 15
Distance: 300
Output:
Vehicle: Toyota Corolla
Mileage: 30000
Fuel Efficiency: 20 miles/gallon
Input Format
Constraints
Test Cases:
Example 1: Addition of Complex Numbers
Input:
Operation Type: 1
Complex Number 1: 3 2
Complex Number 2: 1 -4
Output:
Result: 4 - 2i
Example 2: Multiplication of Complex Numbers
Input:
Operation Type: 2
Complex Number 1: 2 3
Complex Number 2: 1 -1
Output:
Result: 5 + 1i
Example 3: Magnitude of a Complex Number
Input:
Operation Type: 3
Complex Number: 3 4
Output:
Result: Magnitude = 5
Input Format
Constraints
Test Cases:
Example 1: Rectangle Area
Input:
Shape Type: 1
Length: 5
Width: 10
Output:
Shape: Rectangle
Area: 50.00
Example 2: Circle Area
Input:
Shape Type: 2
Radius: 7
Output:
Shape: Circle
Area: 153.94
Example 3: Triangle Area
Input:
Shape Type: 3
Base: 8
Height: 6
Output:
Shape: Triangle
Area: 24.00
Example 4: Invalid Shape Type
Input:
Shape Type: 4
Output:
Invalid shape type.
Input Format
● 1 ≤ choice ≤ 3 .
● 0.0 < parameters ≤ 106 .
Test Cases:
Example 1: Circle
Input:
Choice: 1
Radius: 7.5
Output:
Shape: Circle
Radius: 7.5
Area: 176.714
Example 2: Rectangle
Input:
Choice: 2
Length: 8.0
Breadth: 4.5
Output:
Shape: Rectangle
Length: 8.0
Breadth: 4.5
Area: 36.000
Example 3: Triangle
Input:
Choice: 3
Base: 6.0
Height: 9.0
Output:
Shape: Triangle
Base: 6.0
Height: 9.0
Area: 27.000
Example 4: Invalid Choice
Input:
Choice: 4
Output:
Invalid choice. Please select a valid shape type.