c++ practice set
c++ practice set
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.
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.
In this problem, we have to simply check whether the given integer is odd or even and print the
output on the console.
In this problem, you are given 3 numbers, and you have to find out which one is the largest.
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.
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.
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.
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.
In this program, you have to simply reverse the sentence stored as a string.
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.
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.
In this problem, you have to find both maximum(largest) and minimum(smallest) numbers in a
numerical array.
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.
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
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";
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,
Output:
Welcome
Geeks
to
the
GeeksforGeeks
portal.
In this problem, you have to print the simple pyramid pattern shown below:
*
**
***
****
*****
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
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}
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,
Output: 9 is at index 4
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,
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,
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,
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
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
file2.txt
// After execution
Output
This content will remain even after copying.This content will be copied.
In this problem, you will have to print the list of files that are present in some directory using C++
language.
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,
Output:
Math error: Attempted to divide by Zero