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. Triple Sum for Same Values

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. Absolute Difference with 51

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. Check 30 or Sum 30

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. Within 10 of 100 or 200

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. Add 'if' to String

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. Remove Character from String

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. Swap First and Last Characters

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. 4 Copies of First 2 Characters

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. Add Last Character to Front and Back

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. Multiple of 3 or 7

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. Add First 3 Characters to Front and Back

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. Check If String Starts with 'C#'

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. Temperature Less than 0 or Greater than 100

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. Check if Integers are in Range 100�200

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. Check if Integers are in Range 20�50

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. Check if Either Integer is in Range 20�50

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. Remove 'yt' at Index 1

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. Largest of Three Integers

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. Closest to 100

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. Check Two Integers in Ranges 40�50 or 50�60

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. Larger Value in Range 20�30

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. Check 2�4 'z' Characters

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. Same Last Digit for Integers

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. n Copies of String

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. n Copies of First 3 Characters

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. Count 'aa' in String

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. Check 'aa' Immediately After First 'a'

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. String with Every Other Character

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. Create Sequence String

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. Count Substrings of Length 2

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. Check Sequence 1, 2, 3 in Array

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. Compare Strings for Same Length 2 Substrings

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. Remove Specified Character Except at Ends

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. Characters at Index 0, 1, 4, 5, ...

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. Count Consecutive 5's in Array

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. Check for Triple in Array

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. Sum of Integers, Return 30 in Range 10�20

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. Check if 5, Sum 5, or Difference 5

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. Multiple of 13 or One More

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. Multiple of 3 or 7 but Not Both

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. Within 2 of Multiple of 10

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. Return 18 if Integer in Range 10�20

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. Check String Starts with 'F' or Ends with 'B'

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. Add Two Integers to Get Third

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. Check y > x and z > y

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. Strict Increasing Order with Equality Option

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. Same Rightmost Digit for Non-Negative Integers

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. 20 Less Than Another Integer

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. Larger Integer or Smaller with Same Remainder Modulo 7

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. Shared Digit Between Two Integers (Range 10�99)

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. Sum with Same Number of Digits or Return x

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. Sum or Third Integer if Two are Equal

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. Sum Ignoring 13 and Right Values

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. Sum with 10�20 Treated as Zero Except 13/17

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. Nearest to 13 Without Crossing Over

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. Equal Differences for Three Integers

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. String Format s1s2s2s1

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. Insert String in Middle of Another

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. Three Copies of Last Two Characters

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. First Two Characters or Full String if Less Than 2

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. First Half of Even-Length String

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. Remove First and Last Characters

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. Long+Short+Long String Combination

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. Combine Strings Without First Characters

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. Move First Two Characters to End

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. String Without First and Last Characters

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. Two Middle Characters of Even-Length String

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. First and Last n Characters from String

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. String of Length 2 Starting at Given Index

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. Middle Characters String

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. Two Characters or Pad with #

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. First and Last Character from Two Strings

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. Swap Last Two Characters

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. Starts with 'abc' or 'xyz', Return It or Empty

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. First Two = Last Two Characters Check

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. Add Strings After Trimming Longer String

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. 3 Copies of First 2 Characters or Full String

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. Remove First Two Characters if Start = End

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. Remove First/Last 'a' or Return Original

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. Remove First/First Two 'a's if Present

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. Check 10 in First or Last Element

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. First and Last Elements Match Check

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. Same First or Last Element in Two Arrays

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. Sum of Array Elements

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. Left Rotate Array (Length 4)

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. Reverse Array (Length 5)

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. Middle Elements from Two Arrays

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. First and Last Elements of Array

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. Check 15 or 20 in Array (Length 2)

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. Check if 15 or 20 Not Present in Array (Length 2)

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. Check Array for Two 10s or 20s

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. Replace '7' with '1' if Preceded by '5'

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. Sum Two Arrays and Find Larger

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. Create Array from Middle Elements of Even-Length Array

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. Swap First and Last Elements of Array

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. Create Array of Middle 3 Elements (Length = 3)

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. Find Largest of First, Last, Middle Elements in Odd-Length Array

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. Count Even Numbers in Array

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. Difference Between Largest and Smallest Values in Array

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. Sum Array Elements Excluding 17

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. Sum Array Excluding Numbers Starting with '5' and Followed by '6'

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. Check for Adjacent '5's in Array

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. Check if Array Contains Both '5' and '7'

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. Check if Sum of All '5's Equals 15

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. Compare Count of '3's and '5's in Array

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. Check for Presence of '3' or '5' in Array

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. Check if Array Contains No '3' or '5'

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. Check for Adjacent '3's or '5's in Array

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. Check if Array Contains Two '5's Next or Separated by One Element

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. Check if '3' is Followed by '5' in Array

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. Check for Two Adjacent Even or Odd Numbers in Array

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. Check if Five '5's Exist Without Adjacent Ones

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. Check if All '5's Are Next to Each Other

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. Check if Specified Elements Appear at Both Start and End of Array

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. Check for Three Increasing Adjacent Numbers in Array

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. Check if Array is Non-Decreasing

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. Check if Two Adjacent '15's Exist in Array

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. Find Larger Average Between First and Second Halves of Array

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. Count Strings of Given Length in String Array

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. Create Array of First n Strings from Array

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. Create Array with Strings of Given Length from String Array

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. Check if Number Contains Digit '3'

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. Create Array of Odd Numbers with Specific Length

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. Multiply Each Integer in Array by 5

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. Multiply Each Integer in Array Three Times

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. Add "$" at Start and End of Each String in Array

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. Create List of Strings Each Repeated 3 Times

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. Add 3 and Multiply by 4 for Each Integer in Array

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. Create List of Rightmost Digits of Numbers in Array

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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/cpp-exercises/basic-algorithm/index.php