CSE
CSE
Looping Statements
• Write a program in C++ to calculate the sum of the series (1*1) + (2*2) + (3*3) + (4*4) + (5*5) + ... + (n*n).
Sample Output:
Input the value for nth term: 5
1*1 = 1
2*2 = 4
3*3 = 9
4*4 = 16
5*5 = 25
The sum of the above series is: 55
• Write a program in C++ to calculate the series (1) + (1+2) + (1+2+3) + (1+2+3+4) + ... + (1+2+3+4+...+n).
Sample Output:
Input the value for nth term: 5
1=1
1+2 = 3
1+2+3 = 6
1+2+3+4 = 10
1+2+3+4+5 = 15
The sum of the above series is: 35
• Write a C++ program to swap the first and last digits of any number.
Sample Output:
Input any number: 12345
The number after swapping the first and last digits are: 52341
• Write a C++ program that accepts an integer (n) from the user and outputs the number of combinations that
express n as the sum of two prime numbers.
Note: n should be greater than or equal to 4 and less than or equal to 50,000.
An even number of 4 or more can be represented by the sum of two prime numbers. This is called the
Goldbach expectation, and it is confirmed that it is correct up to a considerable number
by computer calculation.For example, 10 can be expressed as the sum of two prime numbers 7+3,5 + 5.
• Write a C++ program that receives the total number of days as input and converts and prints given days into
their respective years, months, weeks, and days
• Write a program in C++ to display the sum of the series [ 9 + 99 + 999 + 9999 ...].
Sample Output:
Input number of terms: 5
9 99 999 9999 99999
The sum of the sarise = 111105
• Write a C++ program to display Pascal's triangle like a right angle triangle.
Sample Output:
Arrays
• Write a C++ program to find all elements in an array of integers that have at least two greater
elements than itself.
• Original array: 7 12 9 15 19 32 56 70
• Elements which have at-least two greater elements: 7 12 9 15 19 32
• Given an array of distinct elements of size N, the task is to rearrange the elements of the
array in a zig-zag fashion, i.e., the elements are arranged as smaller, then larger, then smaller,
and so on. There can be more than one arrangement that follows the form:
arr[0] < arr[1] > arr[2] < arr[3] > arr[4] < …
• Write a C++ program to move all negative elements of an array of integers to the end of the
array. This is done without changing the order of the positive and negative elements of the
array.
• Program for Maximum sum such that no two elements are adjacent
Input: hval[] = {5, 5, 10, 100, 10, 5}
Output: 110
Explanation: Pick the subsequence {5, 100, 5}.
The sum is 110 and no two elements are adjacent. This is the highest possible sum.
Input: hval[] = {3, 2, 7, 10}
Output: 13
Explanation: The subsequence is {3, 10}. This gives sum = 13.
This is the highest possible sum of a subsequence following the given criteria
• Given a binary 2D array, where each row is sorted. Find the row with the maximum number of 1s.
Examples:
Input matrix : 0 1 1 1
0011
1111
0000
Output: 2
Explanation: Row = 2 has maximum number of 1s, that is 4.
• Given an array arr[] where no two adjacent elements are same, find the index of a peak element. An element is considered
to be a peak element if it is strictly greater than its adjacent elements. If there are multiple peak elements, return the index
of any one of them.
Note: Consider the element before the first element and the element after the last element to be negative infinity.
Examples:
Input: arr[] = [1, 2, 4, 5, 7, 8, 3]
Output: 5
Explanation: arr[5] = 8 is a peak element because arr[4] < arr[5] > arr[6].
• Given an array arr[] of n integers where arr[i] represents the number of chocolates in ith packet. Each packet can have a variable number of
chocolates. There are m students, the task is to distribute chocolate packets such that:
Each student gets exactly one packet.
The difference between the maximum and minimum number of chocolates in the packets given to the students is minimized.
Examples:
Input: arr[] = {7, 3, 2, 4, 9, 12, 56}, m = 3
Output: 2
Explanation: If we distribute chocolate packets {3, 2, 4}, we will get the minimum difference, that is 2.
• Write a C++ program to sort characters (numbers and punctuation symbols are not included) in a string.
• Write a C++ program to find a word in a given string that has the highest number of repeated letters.
Example:
Sample Input: Print a welcome text in a separate line.
Sample Output: Word which has the highest number of repeated letters. Separate
• Given a string str, the task is to reverse the order of the words in the given string. Note that str may contain
leading or trailing dots(.) or multiple trailing dots(.) between two words. The returned string should only have a
single dot(.) separating the words.
Examples:
Examples:
Input: s = “racecar”
Output: ‘e’
Explanation: ‘e’ is the only character in the string which does not repeat.
• Write a C++ program that takes a string and reverses the words of three or more lengths in a string. Return the updated
string. As input characters, only spaces and letters are permitted.
Example:
Original string: The quick brown fox jumps over the lazy dog
Reverse the words of three or more lengths of the said string: ehT kciuq nworb xof spmuj revo eht yzal god