0% found this document useful (0 votes)
149 views17 pages

C++ Programming Practice Tasks

C++ assignment

Uploaded by

tanishsharma1004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
149 views17 pages

C++ Programming Practice Tasks

C++ assignment

Uploaded by

tanishsharma1004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Arithmetic Operators

1. Basic Arithmetic:

Write a program that takes two integers as input and performs addition, subtraction, multiplication,
division, and modulo operations. Print the results.

2. Increment and Decrement:

Predict the output of the following code:

int a = 5, b = 10;

cout << a++ + --b << endl;

cout << a << " " << b << endl;

3. Expression Evaluation:

Evaluate and print the result of the following expression:

int x = 5, y = 10;

int z = (x + y) * (x - y) / x % y;

cout << z;

4. Average Calculation:

Write a program to calculate the average of three numbers using arithmetic operators.

5. Area and Perimeter:

Write a program to calculate the area and perimeter of a rectangle using user inputs for length and
width.

Logical Operators

6. Logical Expression:

What will be the output of the following code?


int a = 10, b = 0, c = 5;

if (a > b && b < c)

cout << "Condition is true" << endl;

else

cout << "Condition is false" << endl;

7. Short-Circuit Behavior:

Predict the output of the following code:

int x = 0, y = 5;

if (x != 0 && y / x > 1)

cout << "True";

else

cout << "False";

8. Logical NOT:

Write a program to check if a number is not divisible by 3 using the logical NOT operator (`!`).

Comparison Operators

9. Relational Operations:

Write a program to compare two integers and print whether the first is greater, less, or equal to the
second.

10. Nested Conditions:

Predict the output of the following code:

int a = 15, b = 10;

if (a > b || a == b && b != 0)

cout << "Condition 1 is true" << endl;

else

cout << "Condition 1 is false" << endl;

10 practice questions on if-else, else-if, and if-else ladder constructs in C++


Basic If-Else

1. Even or Odd:

Write a program to check whether a number is even or odd using an if-else statement.

2. Positive, Negative, or Zero:

Write a program to check if a given number is positive, negative, or zero.

3. Eligibility Check:

Write a program to check if a person is eligible to vote (age >= 18) or not.

Else-If Ladder

4. Grade Calculator:

Write a program to assign grades based on marks:

- `90 and above`: Grade A

- `80-89`: Grade B

- `70-79`: Grade C

- `60-69`: Grade D

- Below 60: Fail

5. Maximum of Three Numbers:

Write a program to find the largest of three numbers using an **else-if ladder**.

6. Temperature Levels:

Write a program to categorize temperature:

- `Temp > 40`: Very Hot

- `Temp 30-40`: Hot

- `Temp 20-30`: Warm

- `Temp 10-20`: Cool

- `Temp < 10`: Cold

Nested If-Else
7. Leap Year:

Write a program to check if a year is a leap year:

- Divisible by 4, but not divisible by 100 unless divisible by 400.

8. Triangle Validity:

Write a program to check if a triangle is valid based on its angles (sum of angles = 180).

Complex Conditions

9. Number Classification:

Write a program to classify a number:

- If divisible by 5: Print "Divisible by 5"

- If divisible by 3: Print "Divisible by 3"

- If divisible by both 5 and 3: Print "Divisible by both"

- Otherwise, print "Not divisible by 3 or 5"

10. Electricity Bill:

Write a program to calculate electricity charges based on the following:

- Units <= 100: `₹1.5 per unit`

- Units 101-200: `₹2.5 per unit`

- Units 201-300: `₹3.5 per unit`

- Units > 300: `₹5 per unit`

10 practice questions on **nested if-else** in C++:

Basic Level

1. Odd or Even:

Write a program to check whether a given number is odd or even using nested if-else statements.k

2. Number Sign:

Write a program to determine if a number is positive, negative, or zero using nested if-else.
3. Voting Eligibility:

Write a program that checks if a person is eligible to vote (age >= 18). If eligible, check if they are
eligible for senior citizen benefits (age >= 60).

4. Maximum of Three Numbers:

Write a program to find the largest among three numbers using nested if-else statements.

5. Grade Assignment:

Write a program to assign grades based on marks:

- Marks >= 90: Grade A

- Marks >= 75: Grade B

- Marks >= 50: Grade C

- Marks < 50: Fail

Intermediate Level

6. Triangle Type:

Write a program to check the type of a triangle based on its sides:

- Equilateral (all sides equal)

- Isosceles (two sides equal)

- Scalene (all sides different)

7. Leap Year Check:

Write a program to determine if a year is a leap year. If it is, check if it's divisible by 400 or just by 4 but
not 100.

8. Number Range Check:

Write a program to check if a number is:

- Less than 10

- Between 10 and 100

- Greater than 100


Advanced Level

9. Eligibility for Admission:

A student is eligible for college admission if:

- They scored at least 60% in both Math and Science.

- If Math marks >= 80 and Science >= 70, they get a scholarship.

Write a program to check admission eligibility and scholarship qualification.

10. Day of the Week:

Write a program to take a number (1-7) as input and display the corresponding day of the week using
nested if-else.

10 practice questions on the switch-case statement in C++:

Basic Switch Case

1. Day of the Week:

Write a program to input a number (1 to 7) and print the corresponding day of the week (e.g., 1 =
Monday, 2 = Tuesday, etc.) using a switch-case.

2. Month of the Year:

Write a program to input a number (1 to 12) and print the corresponding month.

3. Arithmetic Operations:

Write a program to perform basic arithmetic operations. Input two numbers and an operator (`+`, `-`,
`*`, `/`) and use a **switch-case** to compute the result.

Advanced Scenarios

4. Vowel or Consonant:

Write a program to check whether a character is a vowel (`a, e, i, o, u`) or a consonant using a switch-
case. Consider both uppercase and lowercase letters.

5. Number to Words:

Write a program to input a single-digit number (0-9) and print its word representation (e.g., 0 = Zero, 1
= One, etc.) using switch-case.

6. Season Finder:
Write a program to input a month number (1-12) and print the corresponding season:

- Winter: 12, 1, 2

- Spring: 3, 4, 5

- Summer: 6, 7, 8

- Autumn: 9, 10, 11

Nested Logic

7. Grade System:

Write a program where the user inputs a grade (`A`, `B`, `C`, `D`, `F`) and the program displays the
grade description using a switch-case:

- `A`: Excellent

- `B`: Good

- `C`: Average

- `D`: Poor

- `F`: Fail

8. Calculator Menu:

Write a program to create a calculator menu:

- 1: Addition

- 2: Subtraction

- 3: Multiplication

- 4: Division

- 5: Exit

Use switch-case to perform the selected operation.

Error Handling

9. Traffic Signal:

Write a program to simulate a traffic signal system. Input a color (`red`, `yellow`, `green`) and display
the action:
- Red: Stop

- Yellow: Ready

- Green: Go

10.Invalid Input Handling:

Modify any of the above programs to handle invalid inputs (e.g., if a number outside the range is
entered). Add a `default` case to display "Invalid input."

Here are 10 practice questions for for loops and while loops

For Loop Practice Questions:

1. Sum of Numbers

Write a program to calculate the sum of all numbers from 1 to 100 using a for loop.

2. Print Multiplication Table

Display the multiplication table of a number entered by the user using a for loop.

3. Reverse a String

Take a string input from the user and print it in reverse using a for loop.

4. Find Even Numbers

Write a program to print all even numbers between 1 and 50.

5. Factorial Calculation

Write a program to calculate the factorial of a number using a for loop.

While Loop Practice Questions:

6. Number Guessing Game

Write a program where the computer selects a random number between 1 and 10, and the user keeps
guessing until they get it right.

7. Sum of Digits

Take a number as input and calculate the sum of its digits using a while loop.
8. Reverse a Number

Write a program to reverse a number (e.g., 123 -> 321) using a while loop.

9. Fibonacci Sequence

Write a program to print the first 10 numbers of the Fibonacci sequence using a while loop.

10. Check Palindrome

Write a program to check if a given number is a palindrome (e.g., 121 is a palindrome) using a while
loop.

10 basic pattern printing exercises using nested loops:

1. Right-Angled Triangle of Stars

**

***

****

*****

2. Inverted Right-Angled Triangle

*****

****

***

**

3.Pyramid of Stars

***

*****

*******

*********
4. Diamond Shape

***

*****

*******

*********

*******

*****

***

5. Square of Stars

*****

*****

*****

*****

*****

6. Hollow Square

*****

* *

* *

* *

*****

7. Right-Angled Triangle of Numbers


1

12

123

1234

12345

8.Inverted Right-Angled Triangle of Numbers

12345

1234

123

12

9. Floyd’s Triangle

23

456

7 8 9 10

11 12 13 14 15

10. Checkerboard Pattern

*****

*****

*****

*****

*****
10 basic practice questions on arrays:

Basic Array Practice Questions

1. Sum of Array Elements

Write a program to calculate the sum of all elements in an array.

2. Find Maximum and Minimum

Write a program to find the largest and smallest elements in an array.

3. Reverse an Array

Write a program to reverse the elements of an array.

4. Search for an Element

Write a program to search for a specific element in an array and display its position.

5. Count Occurrences of an Element

Write a program to count how many times a specific element appears in an array.

Intermediate Array Practice Questions

6. Sort an Array

Write a program to sort an array in ascending or descending order.

7. Merge Two Arrays

Write a program to merge two arrays into a single array.

8. Find Duplicate Elements

Write a program to identify duplicate elements in an array.

9. Rotate an Array

Write a program to rotate the elements of an array by a given number of positions.

10. Find the Second Largest Element

Write a program to find the second largest element in an array.


Basic Function Practice Questions

1.Addition of Two Numbers

Write a function add(int a, int b) that returns the sum of two numbers.

2.Factorial Calculation

Write a function factorial(int n) that calculates the factorial of a number using recursion.

3.Check Prime

Write a function isPrime(int n) that checks if a number is prime and returns true or false.

4.Find Maximum of Two Numbers

Write a function max(int a, int b) that returns the larger of two numbers.

5.Fibonacci Sequence

Write a function fibonacci(int n) that returns the nth Fibonacci number using recursion.

Here are 10 simple questions on functions in C++ without using advanced concepts like call-by-
reference:

more Basic Function Practice Questions

1. Addition of Two Numbers

Write a function `add(int a, int b)` that takes two integers as input and returns their sum.

2. Check Even or Odd

Write a function `isEven(int num)` that returns `true` if the number is even and `false` otherwise.

3. Find the Maximum of Two Numbers

Write a function `max(int a, int b)` that takes two numbers as input and returns the greater number.

4. Calculate the Square of a Number


Write a function `square(int num)` that takes a number and returns its square.

5. Print Hello N Times

Write a function `printHello(int n)` that prints "Hello" `n` times.

Slightly Advanced Function Practice Questions

6. Check if a Number is Positive

Write a function `isPositive(int num)` that returns `true` if the number is positive and `false` otherwise.

7. Find the Smallest of Three Numbers

Write a function `min(int a, int b, int c)` that returns the smallest among three numbers

8. Sum of N Natural Numbers

Write a function `sumNatural(int n)` that calculates and returns the sum of the first `n` natural
numbers.

9. Find Factorial of a Number

Write a function `factorial(int n)` that calculates the factorial of a number (use a loop) and use the
concept of recursion.

10. Check if a Character is a Vowel

Write a function `isVowel(char ch)` that checks if a given character is a vowel (both uppercase and
lowercase).

10 very basic questions on classes in C++ that avoid inheritance, polymorphism, constructors, and
destructors:

Basic Class Practice Questions

1. Create a Class to Store an Integer

Write a class `Number` that has a private member variable `int num` and a public method to set and
get its value.

2. Class with Member Function to Add Two Numbers

Write a class `Calculator` with a private member variable `int result` and a public function `add(int a, int
b)` to add two numbers and store the result.

3. Class to Represent a Point in 2D Space


Write a class `Point` with two private member variables `x` and `y`. Write a public function to set the
values of `x` and `y` and another to display them.

4. Class with a Member Function to Check Even/Odd

Write a class `CheckNumber` with a private member variable `int number` and a public function
`isEven()` that checks if the number is even.

5. Class to Store a String

Write a class `StringHolder` with a private member variable `string str` and a public function to set and
get the value of `str`.

Slightly Advanced Class Practice Questions

6. Class to Calculate the Area of a Rectangle

Write a class `Rectangle` with private member variables `length` and `width`, and a public function
`calculateArea()` that returns the area of the rectangle.

7. Class to Calculate the Perimeter of a Circle

Write a class `Circle` with a private member variable `radius`, and a public function
`calculatePerimeter()` that returns the perimeter of the circle (use `3.14` as Pi).

8. Class with a Member Function to Multiply Two Numbers

Write a class `Multiplier` with a private member variable `int result` and a public function `multiply(int
a, int b)` that multiplies the two numbers and stores the result.

9. Class with Member Functions to Set and Get Date

Write a class `Date` with private member variables `day`, `month`, and `year`, and public functions to set
and display the date.

10. Class to Count Vowels in a String

Write a class `VowelCounter` with a private member variable `string text` and a public function
`countVowels()` that returns the count of vowels in the string.

Here are 10 easy practice questions on inheritance** in C++ **without using constructors or function
overloading**:

Basic Inheritance Practice Questions


1.Basic Inheritance Example

Create a base class `Animal` with a function `speak()`, and a derived class `Dog` that overrides `speak()`
to print "Bark".

2. Single Inheritance with Member Variables

Create a class `Person` with a member variable `name`. Derive a class `Employee` from `Person` and
add a member variable `employeeId`. Display both the `name` and `employeeId` in the derived class.

3. **Accessing Base Class Members

Create a base class `Book` with a member variable `title`. Derive a class `Ebook` and access the `title` in
the derived class, then print it.

4. Override Function Without Parameters

Create a base class `Shape` with a function `draw()`. Derive a class `Circle` and override the `draw()`
function to print "Drawing Circle".

5. Multi-level Inheritance Example

Create a base class `Person`, a derived class `Student`, and another derived class `GraduateStudent`.
Add a function in each class to display the details of the student and graduate student.

Intermediate Inheritance Practice Questions

6. Accessing Base Class Function in Derived Class

Create a base class `Vehicle` with a function `displayInfo()`. Derive a class `Car` from `Vehicle` and call
the `displayInfo()` function in the derived class to display the vehicle information.

7. Using `public` and `private` Inheritance

Create a base class `Person` with a public member function `showName()`. Derive a class `Employee`
with private inheritance and check if you can access the `showName()` function from the derived class.

8.Call to Base Class Function

Create a base class `Animal` with a function `speak()`. Derive a class `Dog` that overrides `speak()` and
also calls the base class's `speak()` using `Animal::speak()`.

9. Simple Inheritance to Display Student Info

Create a base class `Student` with member variables `name` and `age`. Derive a class `CollegeStudent`
and add an additional member variable `course`. Display all the information from the derived class.

10. Inheritance and Access Control


Create a base class `Person` with a public member variable `name` and a private member variable `age`.
Derive a class `Employee` and try accessing both variables from the derived class. Show how the private
member of the base class is inaccessible.

You might also like