w3resource

C Basic Algorithm: Exercises, Practice, Solution

C Programming Basic Algorithm [75 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 the two input values. If the two values are the same, then return triple their sum.
Expected Output:

3
12

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

13. Write a C program to check if two given non-negative integers have the same last digit.
Expected Output:

0
1
1
0

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

21. Write a C program to check whether a given number is within 2 of a multiple of 10.
Expected Output:

0
0
1
1

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

25. Write a C program to check if two or more nonnegative integers have the same rightmost digit.
Expected Output:

1
1
0

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

36. Write a C program to compute the sum of the elements of an array of integers.
Expected Output:

150
10

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

51. Write a C program to count the even number of elements in a given array of integers.
Expected Output:

3

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

54. Write a C program to check whether a given array of integers contains 5 next to a 5 somewhere.
Expected Output:

0
1
1

Click me to see the solution

55. Write a C program to check whether a given array of integers contains 5's and 7's.
Expected Output:

1
0
1

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

58. Write a C program to check whether a given array of integers contains a 3 or a 5.
Expected Output:

1
0
1

Click me to see the solution

59. Write a C program to check if a given array of integers contains no 3 or 5.
Expected Output:

1
1
0
1

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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.



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://w3resource.com/c-programming-exercises/basic-algo/index.php