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

CSE

Uploaded by

kireetiv2005
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

CSE

Uploaded by

kireetiv2005
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Control Statements and

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 program in C++ to calculate the sum of the series 1.2+2.3+3.4+4.5+5.6+.......


Sample Output:
Input the last integer between 1 to 98 without fraction you want to add: 10
1.2 + 2.3 + 3.4 + 4.5 + 5.6 + 6.7 + 7.8 + 8.9 + 9.1 + 10.11 The sum of the series =59.61
• Write a program in C++ to convert a decimal number to Binary, Hexadecimal, Octal

• 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 pyramid.


Sample Output:

• 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] < …

• Input: N = 7, arr[] = {4, 3, 7, 8, 6, 2, 1}


Output: 3, 7, 4, 8, 2, 6, 1
Explanation: The given array in zig-zag pattern as we can see 3 < 7 > 4 < 8 > 2 < 6 >1
• Input: N = 4, arr[] = {1, 4, 3, 2}
Output: 1, 4, 2, 3
• Write a C++ program to rearrange a given sorted array of positive integers.

Note: In final array, first -> maximum value


second -> minimum value
third -> second maximum value
fourth -> second minimum value
fifth -> third maximum and so on.
Original array: 0 1 3 4 5 6 7 8 10
Array elements after rearranging: 10 0 8 1 7 3 6 4 5

• 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.

• Original array: 0 9 -7 2 -12 11 -20


• Array elements after rearrange: 0 9 2 11 -7 -12 -20
• Given an array arr[], the task is to find the subarray that has the maximum sum and return its sum.
Examples:

Input: arr[] = {2, 3, -8, 7, -1, 2, 3}


Output: 11
Explanation: The subarray {7, -1, 2, 3} has the largest sum 11.

• 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].

Input: arr[] = [10, 20, 15, 2, 23, 90, 80]


Output: 1 or 5
Explanation: arr[1] = 20 and arr[5] = 90 are peak elements because arr[0] < arr[1] > arr[2] and arr[4] < arr[5] > arr[6].
• Given the heights of n towers and a positive integer k, increase or decrease the height of all towers by k (only once). After modifications,
the task is to find the minimum difference between the heights of the tallest and the shortest tower.
Examples:
Input: arr[] = [12, 6, 4, 15, 17, 10], k = 6
Output: 8
Explanation: Update arr[] [12 – 6, 6 + 6, 4 + 6, 15 – 6, 17 – 6, 10 – 6] = [6, 12, 10, 9, 11, 4]. Now, the minimum difference is 12 – 4 = 8.

Input: arr[] = [1, 5, 10, 15], k = 3


Output: 8
Explanation: Update arr[] as [1 + 3, 5 + 3, 10 – 3, 15 – 3] = [4, 8, 7, 12]. Now, the minimum difference is 8.

• 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.

Input: arr[] = {7, 3, 2, 4, 9, 12, 56}, m = 5


Output: 7
Explanation: If we distribute chocolate packets {3, 2, 4, 9, 7}, we will get the minimum difference, that is 9 – 2 = 7.
• Write a C++ program to change every letter in a given string with the letter following it in the alphabet (i.e. a
becomes b, p becomes q, z becomes a).

• 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:

Input: str = “i.like.this.program.very.much”


Output: str = “much.very.program.this.like.i”
• Given a string s of lowercase English letters, the task is to find the first non-repeating character. If there is no such
character, return ‘$’.

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

• Write code to convert a given number into words.


Examples:
Input: 438237764
Output: forty three crore eighty two lakh thirty seven thousand seven hundred and sixty four

You might also like