0% found this document useful (0 votes)
15 views

c++ practice set

Uploaded by

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

c++ practice set

Uploaded by

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

Write a Program to Swap Two Numbers.

You have to create a program that swaps the value of two number variables. It means that the
value of the first variable will be stored in the second variable and the value of the second
variable should be stored in the first variable.

Write a Program to Find Compound Interest.

In this problem, you have to write a program that calculates and prints the compound interest
for the given Principle, Rate of Interest, and Time.

Write a Program to Check Even or Odd Integers.

In this problem, we have to simply check whether the given integer is odd or even and print the
output on the console.

Write a Program to Find the Largest Among 3 Numbers.

In this problem, you are given 3 numbers, and you have to find out which one is the largest.

Write a Program to Check if a Given Year Is a Leap Year.

In this problem, you have to write a program that takes a year as an input, and then checks
whether it is a leap year or not.

Write a Program to Check Whether a Number Is Prime or Not.

The number can be prime or non-prime based on the number of its factors. In this program, we
have to check whether the given number is prime or not and print the result on the console
screen.

Write a Program to Check Whether a Number Is a Palindrome or Not.

A palindrome number is a number that is equal to itself even after reversing its digits. In this
program, we have to check for palindrome numbers.

Write a Program to Make a Simple Calculator.

In this problem, you have to make a program that can perform addition, subtraction,
multiplication, and division on two numbers entered by the user. The type of arithmetic
operation can also be selected by the user.

Write a Program to Reverse a Sentence Using Recursion.

In this program, you have to simply reverse the sentence stored as a string.

Write a Program for Fibonacci Numbers Using Recursion.

The Fibonacci Series is a mathematical sequence in which the next number is the sum of the last
two numbers in the sequence. In this program, you will have to print the Nth number in the
sequence using recursion.

Write a Program to Swap Two Numbers Using a Function.

In this program, you have to swap the values of two variables using another function.
Write a Program to Check if Two Arrays Are Equal or Not.

An array is said to be equal if the elements at the given index are equal in both arrays. In this
program, you have to take two arrays, and then check whether the two arrays are equal.

Write a Program to Find the Maximum and Minimum in an Array.

In this problem, you have to find both maximum(largest) and minimum(smallest) numbers in a
numerical array.

Write a Program to Search an Element in an Array (Linear Search).

In this program, you have to search for the given element in an array. If the element is found, you
will print the index of the element. The array is unsorted.

Write a Program to Compute the Sum of Diagonals of a Matrix.

In this problem, you have to calculate the Sum of both the diagonal elements of a matrix. Matrix
are generally represented as 2D arrays.

For Example,

matrix = 1 2 3
4 5 6
7 8 9

Output:
Principal Diagonal = 16
Secondary Diagonal = 15

Write a Program to Add 2 Binary Strings.

In some cases, numbers can also be represented as strings. In this problem, you have a string
representation of a binary number and you have to convert this binary number string to the
numerical integer form.

For Example,

str = "101011";

Output: Binary Number = 101011

Write a Program to Split a String into a Number of Sub-Strings.

In this problem, you will write a program to split the given string into substrings based on some
delimiter. It is also called tokenization.

For Example,

str = "Welcome Geeks to the GeeksforGeeks portal."

Output:
Welcome
Geeks
to
the
GeeksforGeeks
portal.

Write a Program to Print a Simple Full Pyramid Pattern.

In this problem, you have to print the simple pyramid pattern shown below:

*
**
***
****
*****

This pattern will vary with the number of rows specified.

Write a Program That Receives a Number and Prints It Out in Large Size.

In this problem, you will write a program to print the given numbers in large form as shown
below:

Input: 12

Output:
# #####
## #
# #
# #####
# #
# #
##### #####
Write a Program to Print Pascal’s Triangle.

Pascal Triangle is a pattern in which binomial coefficients are used as the elements. In this
problem, you have to write the program to print the Pascal triangle in the console.

For Example,

1
11
121
1331
14641

Write a Program to Sort an Array(Bubble Sort).

Sorting algorithms are used to sort the data collection into some defined order. Write a program
that sorts the array in increasing order using the bubble sort sorting algorithm.

For Example,
arr[] = {1, 85, 41, 23, 11}

Output: arr[] = {1, 11, 23, 41, 85}

Write a Program to Search an Element in an Array (Binary Search).

Binary Search is a search algorithm similar to linear search. In this problem, you have to search
for a given element K in and print its index. If the element is not found, then print “K is not
found”. You have to use binary search.

For Example,

arr[] = {1, 2, 3, 8, 9, 11}


K=9

Output: 9 is at index 4

Write a Program of Merge Sort.

Merge Sort is an efficient sorting algorithm that can be used to sort the collection of values. In
this problem, implement merge sort algorithm to sort the array in increasing order.

For Example,

arr[] = {1, 85, 41, 23, 11}

Output: arr[] = {1, 11, 23, 41, 85}

Write a Program to Store Information of a Student in a Structure.

In this problem, you have to create a program to store the information of the individual student
i.e. name, roll number, subjects enrolled, marks in each subject, and CGPA in a structure.

For Example,

Student Name: Aditya Arpan


Roll Number: 69420
Subjects Enrolled: {"DSA", "CN", "CO", "TOC", "DBMS"}
Marks: {92, 85, 89, 93, 99}
CGPA: 9.8

Write a Program to Create an Interface.

Interfaces are not part of C++ but their behavior can be imitated. In this problem, you have to
create a class that works in a similar way as the interface works in Java.

For Example,

class interfaceClass { // declartion }

class D: public interfaceClass { // own definition }


class X: public interfaceClass { // own definition }
Write a Program to Create a New File.

In this program, you have to create a new file using a C++ program. Also, check if the file is
created. If failed, print “File Creation Failure”, otherwise print “File Creation Successful”.

For Example,

/* before execution */
/some_parent_directory/{empty directory}

//*after execution */
/some_parent_directory/new_file.txt

Output: File Creation Successful.

Write a Program to Append the Content of One Text File to Another.

In the problem, you have to write a program that copies the content of one file to another file
using C++ such that it does not replace the existing content in the second file. After copying, print
the contents of the second file.

file1.txt

This content will be copied.

file2.txt

This content will remain even after copying.

// After execution

Output

This content will remain even after copying.This content will be copied.

Write a Program to Get the List of Files in a Directory.

In this problem, you will have to print the list of files that are present in some directory using C++
language.

For Example, consider the following directory.


Output

Files in Parent Directory:


file1.txt
file2.exe
thisFile.exe
someProg.bin

Write a Program for Handling the Divide by Zero Exception.

Exception handling provides a defined way to deal with errors. In this problem, you will create a
C++ program to handle one of the most commonly encountered errors which occurs when some
numeric value is divided by zero.

For Example,

// somewhere in the code


num = 10 / 0

Output:
Math error: Attempted to divide by Zero

You might also like