C Basic Algorithm: Exercises, Practice, Solution
This resource offers a total of 375 C Basic Algorithm problems for practice. It includes 75 main exercises, each accompanied by solutions, detailed explanations, and four related problems.
[An Editor is available at the bottom of the page to write and execute the scripts.]
1. Triple Sum Condition
Write a C program to compute the sum of the two input values. If the two values are the same, then return triple their sum.
Expected Output:
3 12
2. Absolute Difference from 51
Write a C program that will take a number as input and find the absolute difference between the input number and 51. If the input number is greater than 51, it will return triple the absolute difference.
Expected Output:
6 21 0
3. Check if One or Sum is 30
Write a C program that checks two given integers and returns true if at least one of them is 30 or if their sum is 30. In other words, if either of the two integers is 30 or if their sum equals 30, the program will return true.
Expected Output:
1 1 0
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.
Expected Output:
1 1 0
5. Divisibility by 3 or 7
Write a C program that checks if a positive integer is divisible by either 3 or 7, or both. If the integer is a multiple of 3, then the program will return true. Similarly, if the integer is a multiple of 7, then also the program will return true. If the integer is not a multiple of 3 or 7, then the program will return false.
Expected Output:
1 1 1 0
6. Temperature Range Check
Write a C program that checks two given temperatures and returns true if one temperature is less than 0 and the other is greater than 100, otherwise it returns false.
Expected Output:
1 1 0
7. Range Check 100 to 200
Write a C program to check two given integers whether either of them is in the range 100..200 inclusive.
Expected Output:
1 0 1
8. Triple Range Check 20 to 50
Write a C program that checks if three given integers are in the range of 20 to 50 (inclusive) and returns true if at least one of them is within the range. If none of the integers are within the range, the program returns false.
Expected Output:
1 1 1 0
9. Triple Range Verification 20 to 50
Write a C program that checks if three given integers are in the range of 20 to 50 (inclusive) and returns true if at least one of them is within the range. If none of the integers are within the range, the program returns false.
Expected Output:
1 1 1 0
10. Nearest to 100
Write a C program to check which number is nearest to the value 100 among two given integers. Return 0 if the two numbers are equal.
Expected Output:
95 0 99
11. Dual Range Check (40-50 and 50-60)
Write a C program that checks if two given integers are in the range of 40 to 50 inclusive, or if they are both in the range of 50 to 60 inclusive.
Expected Output:
0 0 1 1
12. Range 20 to 30 with Larger Value
Write a C program that takes two positive integer values as input and checks if either of them is in the range of 20 to 30 (inclusive). If at least one number falls in this range, the program returns the larger value. Otherwise, it returns 0.
Expected Output:
0 30 25 28
13. Same Last Digit Check
Write a C program to check if two given non-negative integers have the same last digit.
Expected Output:
0 1 1 0
14. 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.
Expected Output:
1 0 1
15. Count Adjacent 5's with 6 Twist
Write a C program to count the number of 5's adjacent to each other in an array of integers. Consider the situation where the second 5 is actually a 6.
Expected Output:
1 2 1
16. Triple Occurrence Check
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.
Expected Output:
0 0 1
17. Sum Range 10 to 20 to 30
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.
Expected Output:
29 30 39 30
18. Check for 5 in Number or Sum/Difference
Write a C program that accepts two integers and checks whether either one of them is 5 or their sum or difference is 5. If any of these conditions are met, the program returns true. Otherwise, it returns false.
Expected Output:
1 0 1
19. Multiple of 13 or One More
Write a C program that checks if a given non-negative integer is a multiple of 13 or one more than a multiple of 13. For example, if the given integer is 26 or 27, the program will return true, but if it is 25 or 28, the program will return false.
Expected Output:
1 1 1 0
20. Exclusive Multiple Check for 3 or 7
Write a C program that checks if a given non-negative number is a multiple of 3 or 7, but not both.
Expected Output:
1 1 0
21. Near Multiple of 10 Check
Write a C program to check whether a given number is within 2 of a multiple of 10.
Expected Output:
0 0 1 1
22. Sum with Special Range Override
Write a C program to compute the sum of the two given integers. Return 18 if one of the integer values given is in the range 10..20 inclusive.
Expected Output:
10 18 18 241
23. Three Integer Sum Match
Write a C program to check whether it is possible to add two integers to get the third integer from three given integers.
Expected Output:
1 0 1
24. Ascending Order Check
Write a C program that checks if y is greater than x and z is greater than y, given three integers x, y, and z. If both conditions are true, the program returns true. Otherwise, it returns false.
Expected Output:
1 1 0
25. Same Rightmost Digit in Multiple Integers
Write a C program to check if two or more nonnegative integers have the same rightmost digit.
Expected Output:
1 1 0
26. Comparative Check with Threshold 20
Write a C program to check three given integers and return true if one of them is 20 or more and less than one of the others.
Expected Output:
1 1 0
27. Conditional Larger Integer with Remainder Check
Write a C program to find the larger of two given integers. However if the two integers have the same remainder when divided by 5, choose the smaller integer. If the two integers are the same, return 0.
Expected Output:
11 20 0
28. Common Digit in Two Two-Digit Numbers
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.
Expected Output:
1 0 1
29. Sum of Three with Duplicate Check
Write a C program to compute the sum of three given integers. Return the third value if the two values are the same.
Expected Output:
16 23 12 18
30. Sum Three Excluding 13
Write a C program to compute the sum of the three integers. Do not count a value that is 13 and add it to the sum.
Expected Output:
16 23 10 0
31. Sum Three with Conditional Zeroing
Write a C program to compute the sum of the three given integers with some exceptions. If any of the values is in the range 10..20 inclusive, then that value will be considered as 0, except for 13 and 17.
Expected Output:
16 11 13 13
32. Nearest to 13 Without Crossing
Write a C program to check two given integers and return the one nearest to 13 without crossing over. Return 0 if both numbers go over.
Expected Output:
5 12 13 0
33. Equal Difference Check
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 the same.
Expected Output:
1 0 1
34. Array Ends Equality Check
Write a C program to check a given array of integers of length 1 or more. Return true if the first element and the last element in the array are equal.
Expected Output:
1 0 0
35. Array First or Last Element Comparison
Write a C program to check two given arrays of integers of length 1 or more. Return true if they have the same first element or if they have the same last element.
Expected Output:
1 0
36. Sum Array Elements
Write a C program to compute the sum of the elements of an array of integers.
Expected Output:
150 10
37. Left Rotate Array
Write a C program to rotate the elements of a given array of integers (length 4 ) in the left direction and return the updated array.
Expected Output:
Elements in original array are: 10, 20, 30, 40 Elements in new array are: 20, 30, 40, 10
38. Reverse Array
Write a C program to reverse a given array of integers of length 5.
Expected Output:
Elements in original array are: 10, 20, 30, 40, 50 Elements in reverse array are: 50, 40, 30, 20, 10
39. Extract Middle Elements from Two Arrays
Write a C program to create a new array containing the middle elements from the two given arrays of integers, each of length 5.
Expected Output:
Elements in original array are: 10, 20, -30, -40, 30 10, 20, 30, 40, 30 Elements in new array are: -30, 30
40. New Array from First and Last Elements
Write a C program to create a new array taking the first and last elements of a given array of integers and length one or more.
Expected Output:
Elements in original array are: 10, 20, 30, 40, 50 Elements in new array are: 10, 50
41. Array of Two: Check for 15 or 20
Write a C program to check whether an array of integers with a length of 2 contains 15 or 20.
Expected Output:
1 1 0
42. Array of Two: Exclude 15 and 20
Write a C program to check if an array of integers with length 2 does not contain 15 or 20.
Expected Output:
0 0 1
43. Array Contains Double 10 or 20
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.
Expected Output:
0 1 0
44. Modify Array: 5 Followed by 7 to 5 and 1
Write a C program to check a given array of integers of length 3 and create a new array. If there is a 5 in the given array immediately followed by a 7 then set 7 to 1.
Expected Output:
Elements in original array are: 1, 5, 7 Elements in new array are: 1, 5, 1
45. Compare Sum of Two Arrays
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.
Expected Output:
Elements in original array are: 10, 20, -30 Elements in original array are: 10, 20, 30 The array which has the largest sum.: 10, 20, 30
46. Extract Middle Elements from Even Array
Write a C program to create an array taking two middle elements from a given array of integers of length even.
Expected Output:
Elements in original array are: 1, 5, 7, 9, 11, 13 New array: 7, 9
47. Merge Two Arrays of Length 3
Write a C program to create a new array from two given arrays of integers, each of length 3.
Expected Output:
Elements in original array1 are: 10, 20, 30 Elements in original array2 are: 40, 50, 60 New array: 10, 20, 30, 40, 50, 60
48. Swap First and Last in Array
Write a C program to create a new array by swapping the first and last elements of a given array of integers whose length is at least 1.
Expected Output:
Elements in original array1 are: 1, 5, 7, 9, 11, 13 New array, after swapping first and last elements: 13, 5, 7, 9, 11, 1
49. Extract Middle Subarray of Length 3
Write a C program to create an array of length 3 from a given array (length at least 3) containing the elements from the middle of the array.
Expected Output:
Elements in original array1 are: 1, 5, 7, 9, 11, 13 New array: 7, 9, 11
50. Largest Among Extremes
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).
Expected Output:
1 9 9
51. Count Even Elements in Array
Write a C program to count the even number of elements in a given array of integers.
Expected Output:
3
52. Sum Array 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.
Expected Output:
Sum of values in the array of integers except the number 17: 46
53. Sum Array Excluding Pattern Starting with 5
Write a C program to compute the sum of the numbers in a given array except those that begin with 5 followed by at least one 6. Return 0 if the given array has no integers.
Expected Output:
Sum of values in the array of integers except the number 17: 37
54. Check for Adjacent 5's in Array
Write a C program to check whether a given array of integers contains 5 next to a 5 somewhere.
Expected Output:
0 1 1
55. Array Contains 5's and 7's
Write a C program to check whether a given array of integers contains 5's and 7's.
Expected Output:
1 0 1
56. Sum of 5's Equals 15 Check
Write a C program to check if the sum of all 5's in the array is exactly 15 in a given array of integers.
Expected Output:
0 1 0
57. Compare Count of 3's and 5's
Write a C program to check whether the number of 3's is greater than the number of 5's.
Expected Output:
1 0 0
58. Array Contains 3 or 5
Write a C program to check whether a given array of integers contains a 3 or a 5.
Expected Output:
1 0 1
59. Array Contains No 3 or 5
Write a C program to check if a given array of integers contains no 3 or 5.
Expected Output:
1 1 0 1
60. Adjacent Duplicates of 3 or 5
Write a C program to check whether an array of integers contains a 3 next to a 3 or a 5 next to a 5 or both.
Expected Output:
1 0 1
61. Two 5's Adjacent or Separated by One
Write a C program to check a given array of integers. The program will return true if the given array contains two 5's next to each other, or two 5's separated by one element.
Expected Output:
1 0 1
62. Check for 3 Followed by 5 Later
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.
Expected Output:
0 1 0
63. Check for Adjacent Pair of Even or Odd Numbers
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.
Expected Output:
0 1 1
64. Check for 5 Appearing 5 Times Non-Adjacent
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.
Expected Output:
1 0 1 0
65. Every 5 is Adjacent to Another
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.
Expected Output:
1 0 1 1
66. Matching Start and End Sequence
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.
Expected Output:
1 0 1
67. Three Consecutive Increasing Numbers
Write a C program to check a given array of integers and return true if the array contains three increasing adjacent numbers.
Expected Output:
1 0 1
68. Left Shift Array Elements
Write a C program to shift an element in the left direction and return a newly created array.
Expected Output:
Elements in original array are: 10, 20, 30, 40 Elements in new array are: 20, 30, 40, 10
69. New Array Before Element 5
Write a C program to create a new array taking the elements before the element value 5 from a given array of integers.
Expected Output:
Elements in original array are: 1, 2, 3, 5, 7 Elements in new array are: 1, 2, 3
70. New Array After Element 5
Write a C program to create a array taking the elements after the element value 5 from a given array of integers.
Expected Output:
Elements in original array are: 1, 2, 3, 5, 7, 9, 11 Elements in new array are: 7, 9, 11
71. Shift Zeros to Left in Array
Write a C program to create a new array from a given array of integers shifting all zeros to left direction.
Expected Output:
Elements in original array are: 1, 2, 0, 3, 5, 7, 0, 9, 11 Elements in new array are: 0, 0, 1, 3, 5, 7, 2, 9, 11
72. Replace 5 with 0 and Shift Zeros Right
Write a C program to create an array after replacing all the values 5 with 0 and shifting all zeros to the right.
Expected Output:
Elements in original array are: 1, 2, 0, 3, 5, 7, 0, 9, 11, 5 Elements in new array are: 1, 2, 0, 3, 7, 0, 9, 11, 0, 0
73. Partition Array: Evens Before Odds
Write a C program to create an array from a given array of integers shifting all even numbers before all odd numbers.
Expected Output:
Elements in original array are: 1, 2, 5, 3, 5, 4, 6, 9, 11 Elements in new array are: 2, 4, 6, 3, 5, 1, 5, 9, 11
74. Check Non-Decreasing Array
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.
Expected Output:
0 1 1
75. Check for Adjacent 15's in Array
Write a C program to check a given array (length will be at least 2) of integers and return true if there are two values 15, 15 next to each other.
Expected Output:
1 0 1
C Programming 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.