w3resource

C++ Basic Algorithm: Exercises, Practice, Solution

C++ Basic Algorithm [129 exercises with solution]

[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]

1. Write a C++ program to compute the sum of two given integer values. If the two values are the same, then return triple their sum.
Sample Input
1, 2
3, 2
2, 2
Sample Output:
3
5
12
Click me to see the sample solution

2. Write a C++ program to find the absolute difference between n and 51. If n is greater than 51 return triple the absolute difference.
Sample Input:
53
30
51
Sample Output:
6
21
0
Click me to see the sample solution

3. Write a C++ program to check two given integers, and return true if one of them is 30 or if their sum is 30.
Sample Input:
30, 0
25, 5
20, 30
20, 25
Sample Output:
1
1
1
0
Click me to see the sample solution

4. Write a C++ program to check a given integer and return true if it is within 10 of 100 or 200.
Sample Input:
103
90
89
Sample Output:
1
1
0
Click me to see the sample solution

5. Write a C++ program to create a string where 'if' is added to the front of a given string. If the string already begins with 'if', return the string unchanged.
Sample Input:
"if else"
"else"
Sample Output:
if else
if else
Click me to see the sample solution

6. Write a C++ program to remove the character at a given position in the string. The given position will be in the range 0..string length -1 inclusive.
Sample Input:
"Python", 1
"Python", o
"Python", 4
Sample Output:
Pthon
ython
Pythn
Click me to see the sample solution

7. Write a C++ program to exchange the first and last characters in a given string and return the result string.
Sample Input:
"abcd"
"a"
"xy"
Sample output:
dbca
a
yx
Click me to see the sample solution

8. Write a C++ program to create a string that is 4 copies of the 2 front characters of a given string. If the given string length is less than 2 return the original string.
Sample Input:
"C Sharp"
"JS"
"a"
Sample Output:
C C C C
JSJSJSJS
a
Click me to see the sample solution

9. Write a C++ program to create a string with the last character added at the front and back of a given string of length 1 or more
Sample Input:
"Red"
"Green"
"1"
Sample Output:
dRedd
nGreenn
111
Click me to see the sample solution

10. Write a C++ program to check if a given positive number is a multiple of 3 or a multiple of 7.
Sample Input
3
14
12
37
Sample Output:
1
1
1
0
Click me to see the sample solution

11. Write a C++ program to create a string taking the first 3 characters of a given string. Then, return the string with the 3 characters added to both the front and back. If the given string length is less than 3, use whatever characters are there.
Sample Input:
"Python"
"JS"
"Code"
Sample Output:
PytPythonPyt
JSJSJS
CodCodeCod
Click me to see the sample solution

12. Write a C++ program to check if a given string starts with 'C#' or not.
Sample Input:
"C++ Sharp"
"C#"
"C++"
Sample Output:
1
1
0
Click me to see the sample solution

13. Write a C++ program to check if one given temperature is less than 0 and the other is greater than 100.
Sample Input:
120, -1
-1, 120
2, 120
Sample Output:
1
1
0
Click me to see the sample solution

14. Write a C++ program to check two given integers whether either of them is in the range 100..200 inclusive.
Sample Input:
100, 199
250, 300
105, 190
Sample Output:
1
0
1
Click me to see the sample solution

15. Write a C++ program to check whether three given integer values are in the range 20..50 inclusive. Return true if 1 or more of them are in the range, otherwise false.
Sample Input:
11, 20, 12
30, 30, 17
25, 35, 50
15, 12, 8
Sample Output:
1
1
1
0
Click me to see the sample solution

16. Write a C++ program to check whether two given integer values are in the range 20..50 inclusive. Return true if 1 or other is in the range, otherwise false.
Sample Input:
20, 84
14, 50
11, 45
25, 40
Sample Output:
1
1
1
0
Click me to see the sample solution

17. Write a C++ program to check if the string 'yt' appears at index 1 in a given string. If it appears return a string without 'yt' otherwise return the original string.
Sample Input:
"Python"
"ytade"
"jsues"
Sample Output:
Phon
ytade
jsues
Click me to see the sample solution

18. Write a C++ program to check the largest number among three given integers.
Sample Input:
1,2,3
1,3,2
1,1,1
1,2,2
Sample Output:
3
3
1
2
Click me to see the sample solution

19. Write a C++ program to check which number is closest to the value 100 among two given integers. Return 0 if the two numbers are equal.
Sample Input:
78, 95
95, 95
99, 70
Sample Output:
95
0
99
Click me to see the sample solution

20. Write a C++ program to check whether two given integers are in the range 40..50 inclusive, or they are both in the range 50..60 inclusive.
Sample Input:
78, 95
25, 35
40, 50
55, 60
Sample Output:
0
0
1
1
Click me to see the sample solution

21. Write a C++ program to find the larger value from two positive integer values that is in the range 20..30 inclusive. Also, return 0 if neither is in that range.
Sample Input:
78, 95
20, 30
21, 25
28, 28
Sample Output:
0
30
25
28
Click me to see the sample solution

22. Write a C++ program to check if a given string contains between 2 and 4 'z' characters.
Sample Input:
"frizz"
"zane"
"Zazz"
"false"
Sample Output:
1
0
1
0
Click me to see the sample solution

23. Write a C++ program to check if two given non-negative integers have the same last digit.
Sample Input:
123, 456
12, 512
7, 87
12, 45
Sample Output:
0
1
1
0
Click me to see the sample solution

24. Write a C++ program to create the string which is n (non-negative integer) copies of a given string.
Sample Input:
"JS", 2
"JS", 3
"JS", 1
Sample Output:
JSJS
JSJSJS
JS
Click me to see the sample solution

25. Write a C++ program to create another string which is n (non-negative integer) copies of the first 3 characters of a given string. If the length of the given string is less than 3 then return n copies of the string.
Sample Input:
"Python", 2
"Python", 3
"JS", 3
Sample Output:
PytPyt
PytPytPyt
JSJSJS
Click me to see the sample solution

26. Write a C++ program to count the string "aa" in a given string and assume "aaa" contains two "aa".
Sample Input:
"bbaaccaag"
"jjkiaaasew"
"JSaaakoiaa"
Sample Output:
2
2
3
Click me to see the sample solution

27. Write a C++ program to check if the first appearance of "a" in a given string is immediately followed by another "a".
Sample Input:
"caabb"
"babaaba"
"aaaaa"
Sample Output:
1
0
1
Click me to see the sample solution

28. Write a C++ program to create another string made of every other character starting with the first from a given string.
Sample Input:
"Python"
"PHP"
"JS"
Sample Output:
Pto
PP
J
Click me to see the sample solution

29. Write a C++ program to create a string like "aababcabcd" from a given string "abcd".
Sample Input:
"abcd"
"abc"
"a"
Sample Output:
aababcabcd
aababc
a
Click me to see the sample solution

30. Write a C++ program to count the number of times a substring of length 2 appears in a given string as well as its last two characters. Do not count the end substring.
Sample Input:
"abcdsab"
"abcdabab"
"abcabdabab"
"abcabd"
Sample Output:
1
2
3
0
Click me to see the sample solution

31. Write a C++ program to check whether the sequence of numbers 1, 2, 3 appears in a given array of integers somewhere.
Sample Input:
{1,1,2,3,1}
{1,1,2,4,1}
{1,1,2,1,2,3}
Sample Output:
1
0
1
Click me to see the sample solution

32. Write a C++ program to compare two given strings and return the number of positions where they contain the same length 2 substring.
Sample Input:
{ 5, 5, 1, 15, 15 }
{ 15, 2, 3, 4, 15 }
{ 3, 3, 15, 15, 5, 5}
{ 1, 5, 15, 7, 8, 15}
Sample Output:
1
0
1
0
Click me to see the sample solution

33. Write a C++ program to create a new string from a given string where a specified character is removed except at the beginning and end.
Sample Input:
"xxHxix", "x"
"abxdddca", "a"
"xabjbhtrb", "b"
Sample Output:
xHix
abxdddca
xajhtrb
Click me to see the sample solution

34. Write a C++ program to create the string of the characters at indexes 0,1,4,5,8,9 ... from a given string.
Sample Input:
"Python"
"JavaScript"
"HTML"
Sample Output:
Pyon
JaScpt
HT
Click me to see the sample solution

35. Write a C++ program to count the number of 5's next to each other in an array of integers. Count the situation where the second 5 is actually a 6.
Sample Input:
{ 5, 5, 2 }
{ 5, 5, 2, 5, 5 }
{ 5, 6, 2, 9}
Sample Output:
1
2
1
Click me to see the sample solution

36. Write a C++ program to check if a triple is present in an array of integers or not. If a value appears three times in a row in an array it is called a triple.
Sample Input:
{ 1, 1, 2, 2, 1 }
{ 1, 1, 2, 1, 2, 3 }
{ 1, 1, 1, 2, 2, 2, 1 }
Sample Output:
0
0
1
Click me to see the sample solution

37. Write a C++ program to compute the sum of the two given integers. If the sum is in the range 10..20 inclusive return 30.
Sample Input:
12, 17
2, 17
22, 17
20, 0
Sample Output:
29
30
39
30
Click me to see the sample solution

38. Write a C++ program that accepts two integers and returns true if either one is 5 or their sum or difference is 5.
Sample Input:
5, 4
4, 3
1, 4
Sample Output:
1
0
1
Click me to see the sample solution

39. Write a C++ program to test if a given non-negative number is a multiple of 13 or it is one more than a multiple of 13.
Sample Input:
13
14
27
41
Sample Output:
1
1
1
0
Click me to see the sample solution

40. Write a C++ program to check if a given number that is not negative is a multiple of 3 or 7, but not both.
Sample Input:
3
7
21
Sample Output:
1
1
0
Click me to see the sample solution

41. Write a C++ program to check if a given number is within 2 of a multiple of 10.
Sample Input:
3
7
8
21
Sample Output:
0
0
1
1
Click me to see the sample solution

42. Write a C++ program to compute the sum of the two given integers. Return 18 if one of the given integer values is in the range 10..20 inclusive.
Sample Input:
3, 7
10, 11
10, 20
21, 220
Sample Output:
10
18
18
241
Click me to see the sample solution

43. Write a C++ program to check whether a given string begins with "F" or ends with "B". If the string starts with "F" return "Fizz" and return "Buzz" if it finishes with "B". If the string starts with "F" and ends with "B" return "FizzBuzz". In other cases return the original string.
Sample Input:
"FB"
"Fsafs"
"AuzzB"
"founder"
Sample Output:
FizzBuzz
Fizz
Buzz
founder
Click me to see the sample solution

44. Write a C++ program to check if it is possible to add two integers to get the third integer from three given integers.
Sample Input:
1, 2, 3
4, 5, 6
-1, 1, 0
Sample Output:
1
0
1
Click me to see the sample solution

45. Write a C++ program to check if y is greater than x, and z is greater than y from three given integers x,y,z.
Sample Input:
1, 2, 3
4, 5, 6
-1, 1, 0
Sample Output:
1
1
0
Click me to see the sample solution

46. Write a C++ program to check if three given numbers are in strict increasing order. For example, 4, 7, 15, or 45, 56, 67, but not 4 ,5, 8 or 6, 6, 8. However, if a fourth parameter is true, equality is allowed, such as 6, 6, 8 or 7, 7, 7.
Sample Input:
1, 2, 3, false
1, 2, 3, true
10, 2, 30, false
10, 10, 30, true
Sample Output:
1
1
0
1
Click me to see the sample solution

47. Write a C++ program to check if two or more integers that are not negative have the same rightmost digit.
Sample Input:
11, 21, 31
11, 22, 31
11, 22, 33
Sample Output:
1
1
0
Click me to see the sample solution

48. Write a C++ program to check three given integers and return true if one of them is 20 lower than one of the others.
Sample Input:
11, 21, 31
11, 22, 31
10, 20, 15
Sample Output:
1
1
0
Click me to see the sample solution

49. Write a C++ program to find the larger of two given integers. However if the two integers have the same remainder when divided by 7, then return the smaller integer. If the two integers are the same, return 0.
Sample Input:
11, 21
11, 20
10, 10
Sample Output:
21
20
0
Click me to see the sample solution

50. Write a C++ program to check two given integers. Each integer is in the range 10..99. Return true if a digit appears in both numbers, such as the 3 in 13 and 33.
Sample Input:
11, 21
11, 20
10, 10
Sample Output:
1
0
1
Click me to see the sample solution

51. Write a C++ program to compute the sum of two given non-negative integers x and y as long as the sum has the same number of digits as x. If the sum has more digits than x, return x without y.
Sample Input:
4, 5
7, 4
10, 10
Sample Output:
9
7
20
Click me to see the sample solution

52. Write a C++ program to compute the sum of three given integers. Return the third value if the two values are the same.
Sample Input:
4, 5, 7
7, 4, 12
10, 10, 12
12, 12, 18
Sample Output:
16
23
12
18
Click me to see the sample solution

53. Write a C++ program to compute the sum of the three integers. If one of the values is 13 then do not count it and its right towards the sum.
Sample Input:
4, 5, 7
7, 4, 12
10, 13, 12
13, 12, 18
Sample Output:
16
23
10
0
Click me to see the sample solution

54. Write a C++ program to compute the sum of the three given integers. Except for 13 and 17, any value in the range 10..20 inclusive counts as 0.
Sample Input:
4, 5, 7
7, 4, 12
10, 13, 12
17, 12, 18
Sample Output:
16
11
13
17
Click me to see the sample solution

55. Write a C++ program to check two integers and return the value nearest to 13 without crossing over. Return 0 if both numbers go over.
Sample Input:
4, 5
7, 12
10, 13
17, 33
Sample Output:
5
12
13
0
Click me to see the sample solution

56. Write a C++ program to check three given integers (small, medium and large) and return true if the difference between small and medium and the difference between medium and large is same.
Sample Input:
4, 5, 6
7, 12, 13
-1, 0, 1
Sample Output:
1
0
1
Click me to see the sample solution

57. Write a C++ program to create another string using two given strings s1, s2, the format of the new string will be s1s2s2s1.
Sample Input:
"Hi", "Hello"
"whats", "app"
Sample Output:
HiHelloHelloHi
whatsappappwhats
Click me to see the sample solution

58. Write a C++ program to insert a given string into middle of the another given string of length 4.
Sample Input:
"[[]]","Hello"
"(())", "Hi"
Sample Output:
[[Hello]]
((Hi))
Click me to see the sample solution

59. Write a C++ program to create another string using three copies of the last two characters of a given string of length at least two.
Sample Input:
"Hello"
"Hi"
Sample Output:
lololo
HiHiHi
Click me to see the sample solution

60. Write a C++ program to create a new string using the first two characters of a given string. If the string length is less than 2, return the original string.
Sample Input:
"Hello"
"Hi"
"H"
" "
Sample Output:
He
Hi
H
Click me to see the sample solution

61. Write a C++ program to create the string of the first half of a given string of even length.
Sample Input:
"Hello"
"Hi"
Sample Output:
He
H
Click me to see the sample solution

62. Write a C++ program to create a new string without the first and last characters of a given string of length at least two.
Sample Input:
"Hello"
"Hi"
"Python"
Sample Output:
ell
ytho
Click me to see the sample solution

63. Write a C++ program to create a new string from two given strings, one of which is shorter and the other is larger. The format of the updated string will be long string + short string + long string.
Sample Input:
"Hello", "Hi"
"JS", "Python"
Sample Output:
HelloHiHello
PythonJSPython
Click me to see the sample solution

64. Write a C++ program to combine two strings of length at least 1, after removing their first character.
Sample Input:
"Hello", "Hi"
"JS", "Python"
Sample Output:
elloi
Sython
Click me to see the sample solution

65. Write a C++ program to move the first two characters to the end of a given string of length at least two.
Sample Input:
"Hello"
"JS"
Sample Output:
lloHe
JS
Click me to see the sample solution

66. Write a C++ program to create a new string without the first and last characters of a given string of any length.
Sample Input:
"Hello"
"JS"
""
Sample Output:
ell

Click me to see the sample solution

67. Write a C++ program to create a string using the two middle characters of a given string of even length (at least 2).
Sample Input:
"Hell"
"JS"
Sample Output:
el
JS
Click me to see the sample solution

68. Write a C++ program to create a new string using the first and last n characters from a given string of length at least n.
Sample Input:
"Hello", 1
"Python", 2
"on", 1
"o", 1
Sample Output:
Ho
Pyon
on
oo
Click me to see the sample solution

69. Write a C++ program to create a string of length 2 starting at the given index of a given string.
Sample Input:
"Hello", 1
"Python", 2
"on", 1
Sample Output:
el
th
on
Click me to see the sample solution

70. Write a C++ program that takes at least 3 characters from the middle of a given string that would be used to create a string.
Sample Input:
"Hello"
"Python"
"abc"
Sample Output:
ell
yth
abc
Click me to see the sample solution

71. Write a C++ program to create a new string of length 2, using the first two characters of a given string. If the given string length is less than 2 use '#' as missing characters.
Sample Input:
"Hello"
"Python"
"a"
""
Sample Output:
He
Py
a#
##
Click me to see the sample solution

72. Write a C++ program to create a string taking the first character from a string and the last character from another given string. If the length of any given string is 0, use '#' as its missing character.
Sample Input:
"Hello", "Hi"
"Python", "PHP"
"JS", "JS"
"Csharp", ""
Sample Output:
Hi
PP
JS
C#
Click me to see the sample solution

73. Write a C++ program to create a new string from a given string after swapping the last two characters.
Sample Input:
"Hello"
"Python"
"PHP"
"JS"
"C"
Sample Output:
Helol
Pythno
PPH
SJ
C
Click me to see the sample solution

74. Write a C++ program to check if a given string begins with 'abc' or 'xyz'. If the string begins with 'abc' or 'xyz' return 'abc' or 'xyz' otherwise return the empty string.
Sample Input:
"abc"
"abcdef"
"C"
"xyz"
"xyzsder"
Sample Output:
abc
abc

xyz
xyz
Click me to see the sample solution

75. Write a C++ program to check whether the first two characters and the last two characters of a given string are the same.
Sample Input:
"abab"
"abcdef"
"xyzsderxy"
Sample Output:
1
0
1
Click me to see the sample solution

76. Write a C++ program to add two given strings. If the given strings have different lengths, remove the characters from the longer string.
Sample Input:
"abc", "abcd"
"Python", "Python"
"JS", "Python"
Sample Output:
abcbcd
PythonPython
JSon
Click me to see the sample solution

77. Write a C++ program to create a new string using 3 copies of the first 2 characters of a given string. If the length of the given string is less than 2 use the whole string.
Sample Input:
"abc"
"Python"
"J"
Sample Output:
ababab
PyPyPy
JJJ
Click me to see the sample solution

78. Write a C++ program to create a new string from a string. Return the given string without the first two characters if the two characters at the beginning and end are the same. Otherwise, return the original string.
Sample Input:
"abcab"
"Python"
"abcabab"
Sample Output:
cab
Python
cabab
Click me to see the sample solution

79. Write a C++ program to create a string from a given string without the first and last character. This is possible if the first or last characters are 'a' otherwise return the original given string.
Sample Input:
"abcab"
"python"
"abcda"
"jython"
Sample Output:
bcab
python
bcd
jython
Click me to see the sample solution

80. Write a C++ program to create a new string from a given string. If the first or first two characters are 'a', return the string without those 'a' characters, otherwise return the original string.
Sample Input:
"abcab"
"python"
"aacda"
"jython"
Sample Output:
bcab
python
cda
jython
Click me to see the sample solution

81. Write a C++ program to check a given array of integers of length 1 or more and return true if 10 appears as either first or last element in the given array.
Sample Input:
{ 10, 20, 40, 50 }
{ 5, 20, 40, 10 }
{ 10, 20, 40, 10 }
{ 12, 24, 35, 55 }
Sample Output:
1
1
1
0
Click me to see the sample solution

82. Write a C++ program to check a given array of integers of length 1 or more. The program will return true if the first element and the last element are equal in the given array.
Sample Input:
{ 10, 20, 40, 50 }
{ 10, 20, 40, 10 }
{ 12, 24, 35, 55 }
Sample Output:
0
1
0
Click me to see the sample solution

83. Write a C++ program to check two given arrays of integers of length 1 or more. This will return true if they have the same first element or if they have the same last element.
Sample Input:
{ 10, 20, 40, 50 }, { 10, 20, 40, 50 }
{ 10, 20, 40, 50 }, { 10, 20, 40, 5 }
{ 10, 20, 40, 50 }, { 1, 20, 40, 5 }
Sample Output:
1
1
0
Click me to see the sample solution

84. Write a C++ program to compute the sum of the elements of an array of integers.
Sample Input:
{ 10, 20, 30, 40, 50 }
{ 10, 20, -30, -40, 50 }
Sample Output:
150
10
Click me to see the sample solution

85. Write a C++ program to rotate the elements of a given array of integers (length 4 ) in the left direction and return the changed array.
Sample Input:
{ 10, 20, 30, 40 }
Sample Output:
Rotated array:
20 30 40 10
Click me to see the sample solution

86. Write a C++ program to reverse a given array of integers of length 5.
Sample Input:
{ 0, 10, 20, 30, 40 }
Sample Output:
Reverse array:
40 30 20 10 0
Click me to see the sample solution

87. Write a C++ program to create an array containing the middle elements from the two given arrays of integers, each of length 5.
Sample Input:
{0, 10, 20, 30, 40}
{0, -10, -20, -30, -40}
Sample Output:
New array:
20 -20
Click me to see the sample solution

88. Write a C++ program to create an array taking the first and last elements of a given array of integers and length 1 or more.
Sample Input:
{ 10, 20, -30, -40, 30 }
Sample Output:
New array:
10 30
Click me to see the sample solution

89. Write a C++ program to determine whether a given array of integers of length 2 contains 15 or 20.
Sample Input:
{ 12, 20 }
{ 14, 15 }
{ 11, 21 }
Sample Output:
1
1
0
Click me to see the sample solution

90. Write a C++ program to check if an array of integers with length 2 does not contain 15 or 20.
Sample Input:
{ 12, 20 }
{ 14, 15 }
{ 11, 21 }
Sample Output:
0
0
1
Click me to see the sample solution

91. Write a C++ program to check a given array of integers and return true if the array contains 10 or 20 twice. The length of the array will be 0, 1, or 2.
Sample Input:
{ 12, 20 }
{ 20, 20 }
{ 10, 10 }
{ 10 }
Sample Output:
0
1
1
0
Click me to see the sample solution

92. Write a C++ program to check a given array of integers, length 3 and create an array. If there is a 5 in the given array immediately followed by a 7 then set 7 to 1.
Sample Input:
{ 1, 5, 7 }
{ 1, 5, 3, 7 }
Sample Output:
New array:
1 5 1
New array:
1 5 3 7
Click me to see the sample solution

93. Write a C++ program to compute the sum of the two given arrays of integers, length 3 and find the array that has the largest sum.
Sample Input:
{ 1, 5, 7 }
{ 1, 5, 3 }
Sample Output:
New array:
1 5 7
Click me to see the sample solution

94. Write a C++ program to create an array taking two middle elements from a given array of integers of length even.
Sample Input:
{ 1, 5, 7, 9, 11, 13 }
Sample Output:
New array:
7 9
Click me to see the sample solution

95. Write a C++ program to create an array by swapping the first and last elements of a given array of integers with a length of at least 1.
Sample Input:
{ 1, 5, 7, 9, 11, 13 }
Sample Output:
New array (after swapping the first and last elements of the said array):
13 5 7 9 11 1
Click me to see the sample solution

96. Write a C++ program to create an array length 3 from a given array (length at least 3) using the elements from the middle of the array.
Sample Input:
{ 1, 5, 7, 9, 11, 13 }
Sample Output:
New array:
7 9 11
Click me to see the sample solution

97. Write a C++ program to find the largest value from the first, last, and middle elements of a given array of integers of odd length (at least 1).
Sample Input:
{1}
{1,2,9}
{1,2,9,3,3}
{1,2,3,4,5,6,7}
{1,2,2,3,7,8,9,10,6,5,4}
Sample Output:
1
9
9
7
8
Click me to see the sample solution

98. Write a C++ program to count the even number of elements in a given array of integers.
Sample Input:
{ 1, 5, 7, 9, 10, 12 }
{0, 2, 4, 6, 8, 10}
Sample Output:
2
6
Click me to see the sample solution

99. Write a C++ program to compute the difference between the largest and smallest values in a given array of integers and length one or more.
Sample Input:
{ 1, 5, 7, 9, 10, 12}
{0, 2, 4, 6, 8, 10}
Sample Output:
11
10
Click me to see the sample solution

100. Write a C++ program to compute the sum of values in a given array of integers except the number 17. Return 0 if the given array has no integers.
Sample Input:
{ 1, 5, 7, 9, 10, 17 }
Sample Output:
Sum of values in the array of integers except the number 17:
32
Click me to see the sample solution

101. Write a C++ program to compute the sum of the numbers in a given array except the ones starting with 5 followed by at least one 6. Return 0 if the given array has no integers.
Sample Input:
{ 1, 5, 9, 10, 17 }
{ 1, 5, 6, 9, 10, 17 }
Sample Output:
Sum of the numbers of the said array except those numbers starting with 5 followed by atleast one 6:
42
Sum of the numbers of the said array except those numbers starting with 5 followed by atleast one 6:
37
Click me to see the sample solution

102. Write a C++ program to check if a given array of integers contains 5 next to a 5 somewhere.
Sample Input:
{ 1, 5, 6, 9, 10, 17 }
{ 1, 5, 5, 9, 10, 17 }
{ 1, 5, 5, 9, 10, 17, 5, 5 }
Sample Output:
0
1
1
Click me to see the sample solution

103. Write a C++ program to check whether a given array of integers contains 5's and 7's.
Sample Input:
{ 1, 5, 6, 9, 10, 17 }
{ 1, 4, 7, 9, 10, 17 }
{ 1, 1, 2, 9, 10, 17}
Sample Output:
1
1
0
Click me to see the sample solution

104. Write a C++ program that checks if the sum of all 5' in the array is exactly 15.
Sample Input:
{ 1, 5, 6, 9, 10, 17 }
{ 1, 5, 5, 5, 10, 17 }
{ 1, 1, 5, 5, 5, 5}
Sample Output:
0
1
0
Click me to see the sample solution

105. Write a C++ program to check if the number of 3's is greater than the number of 5's.
Sample Input:
{ 1, 5, 6, 9, 3, 3 }
{ 1, 5, 5, 5, 10, 17 }
{ 1, 3, 3, 5, 5, 5}
Sample Output:
1
0
0
Click me to see the sample solution

106. Write a C++ program to check if a given array of integers contains a 3 or a 5.
Sample Input:
{ 5, 5, 5, 5, 5 }
{ 3, 3, 3, 3 }
{ 3, 3, 3, 5, 5, 5}
{ 1, 6, 8, 10}
Sample Output:
1
1
1
0
Click me to see the sample solution

107. Write a C++ program to check if a given array of integers contains no 3 or 5.
Sample Input:
{ 5, 5, 5, 5, 5 }
{ 3, 3, 3, 3 }
{ 3, 3, 3, 5, 5, 5}
{ 1, 6, 8, 10}
Sample Output:
1
1
0
1
Click me to see the sample solution

108. Write a C++ program to check if an array of integers contains a 3 next to a 3 or a 5 next to a 5 or both.
Sample Input:
{ 5, 5, 5, 5, 5 }
{ 1, 2, 3, 4 }
{ 3, 3, 5, 5, 5, 5}
{ 1, 5, 5, 7, 8, 10}
Sample Output:
1
0
1
1
Click me to see the sample solution

109. Write a C++ program to check a given array of integers. Then, return true if the given array contains two 5's next to each other, or two 5's separated by one element.
Sample Input:
{ 5, 5, 1, 5, 5 }
{ 1, 2, 3, 4 }
{ 3, 3, 5, 5, 5, 5}
{ 1, 5, 5, 7, 8, 10}
Sample Output:
1
0
1
1
Click me to see the sample solution

110. Write a C++ program to check a given array of integers and return true if there is a 3 with a 5 somewhere later in the given array.
Sample Input:
{ 3, 5, 1, 3, 7 }
{ 1, 2, 3, 4 }
{ 3, 3, 5, 5, 5, 5}
{ 2, 5, 5, 7, 8, 10}
Sample Output:
1
0
1
0
Click me to see the sample solution

111. Write a C++ program to check a given array of integers. The program will return true if the given array contains either 2 even or 2 odd values all next to each other.
Sample Input:
{ 3, 5, 1, 3, 7 }
{ 1, 2, 3, 4 }
{ 3, 3, 5, 5, 5, 5}
{ 2, 4, 5, 6}
Sample Output:
1
0
1
1
Click me to see the sample solution

112. Write a C++ program to check a given array of integers. The program will return true if the value 5 appears 5 times and there are no 5 next to each other.
Sample Input:
{ 3, 5, 1, 5, 3, 5, 7, 5, 1, 5 }
{ 3, 5, 5, 5, 5, 5, 5}
{ 3, 5, 2, 5, 4, 5, 7, 5, 8, 5}
{ 2, 4, 5, 5, 5, 5}
Sample Output:
1
0
1
0
Click me to see the sample solution

113. Write a C++ program to check a given array of integers and return true if every 5 that appears in the given array is next to another 5.
Sample Input:
{ 3, 5, 5, 3, 7 }
{ 3, 5, 5, 4, 1, 5, 7}
{ 3, 5, 5, 5, 5, 5}
{ 2, 4, 5, 5, 6, 7, 5}
Sample Output:
1
0
1
0
Click me to see the sample solution

114. Write a C++ program to check a given array of integers. The program will return true if the specified number of the same elements appears at the start and end of the given array.
Sample Input:
{ 3, 7, 5, 5, 3, 7 }, 2
{ 3, 7, 5, 5, 3, 7 }, 3
{ 3, 7, 5, 5, 3, 7, 5 }, 3
Sample Output:
1
0
1
Click me to see the sample solution

115. Write a C++ program to check a given array of integers and return true if the array contains three increasing adjacent numbers.
Sample Input:
{ 1, 2, 3, 5, 3, 7 }
{ 3, 7, 5, 5, 3, 7 }
{ 3, 7, 5, 5, 6, 7, 5 }
Sample Output:
1
0
1
Click me to see the sample solution

116. Write a C++ program to check if the value of each element is equal or greater than the value of the previous element of a given array of integers.
Sample Input:
{ 5, 5, 1, 5, 5 }
{ 1, 2, 3, 4 }
{ 3, 3, 5, 5, 5, 5}
{ 1, 5, 5, 7, 8, 10}
Sample Output:
0
1
1
1
Click me to see the sample solution

117. Write a C++ program to check if there are two values 15, 15 adjacent to each other in a given array (length should be at least 2) of integers. Return true otherwise false.
Test Data:
({ 5, 5, 1, 15, 15 }) -> 1
({ 15, 2, 3, 4, 15 }) -> 0
({ 3, 3, 15, 15, 5, 5}) -> 1
({ 1, 5, 15, 7, 8, 15})-> 0
Click me to see the sample solution

118. Write a C++ program to find the largest average value between the first and second halves of a given array of integers. Ensure that the minimum length is at least 2. Assume that the second half begins at index (array length)/2.
Test Data:
({ 1, 2, 3, 4, 6, 8 }) -> 6
({ 15, 2, 3, 4, 15, 11 }) -> 10
Click me to see the sample solution

119. Write a C++ program to count the number of strings with a given length in a given array of strings.
Test Data:
({"a", "b", "bb", "c", "ccc" }, 1) ->3
({"a", "b", "bb", "c", "ccc" }, 2) ->1
({"a", "b", "bb", "c", "ccc" }, 3) ->1
Click me to see the sample solution

120. Write a C++ program to create an array using the first n strings from a given array of strings. (n>=1 and <=length of the array).
Test Data:
({"a", "b", "bb", "c", "ccc" }, 2) -> {"a", "b"}
({"a", "b", "bb", "c", "ccc" }, 3) -> {"a", "b", "bb"}
Click me to see the sample solution

121. Write a C++ program to create an array from a given array of strings using all the strings whose lengths are matched with the given string length.
Test Data:
({a,aaa,b,bb,bbb,c,cc,ccc},1) -> {a,b,c}
({a,aaa,b,bb,bbb,c,cc,ccc},2) -> {bb, cc}
Click me to see the sample solution

122. Write a C++ program to check a positive integer and return true if it contains the number 3. Otherwise return false.
Test Data:
(143) -> 1
(678) -> 0
(963) -> 1
Click me to see the sample solution

123. Write a C++ program that creates a new array of odd numbers with specific lengths from a given array of positive integers.
Test Data:
({1,2,3,5,7,9,10},3) -> {1,3,5}
Click me to see the sample solution

124. Write a C++ program to create a list from a given list of integers where each element is multiplied by 5.
Test Data:
({ 1, 2, 3}) -> {5, 10, 15}
({2, 4, 7, 8}) -> {10, 20, 35, 40})
Click me to see the sample solution

125. Write a C++ program that multiplies each integer three times to create a list from a given list of integers.
Test Data:
({1, 2, 3, 4}-> (1 8 27 64}
Click me to see the sample solution

126. Write a C++ program to create a list from a given list of strings where each element has "$" added at the beginning and end position.
Test Data:
Sample Input:
{ "1", "2", "3" , "4" }
Expected Output:
$1$ $2$ $3$ $4$
Click me to see the sample solution

127. Write a C++ program to create a list from a given list of strings where each element is replaced by 3 copies of the string concatenated together.
Test Data:
Sample Input:
{ "1", "2", "3" , "4" }
Expected Output :
111 222 333 444
Click me to see the sample solution

128. Write a C++ program to create a list from a given list of integers. In this program, each integer value is added to 3 and the result value is multiplied by 4.
Test Data:
({ 1, 2, 3 , 4 }) -> {16 20 24 28}
Click me to see the sample solution

129. Write a C++ program to create a list of the rightmost digits from a given list of positive integers.
Test Data:
({ 12, 20, 351, 4449}) -> {2, 0, 1, 9}
Click me to see the sample solution

CPP Code Editor:

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.



Follow us on Facebook and Twitter for latest update.