C Programming Basic Algorithm - Exercises, Practice, Solution - W3resource
C Programming Basic Algorithm - Exercises, Practice, Solution - W3resource
[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
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
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
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
1
1
1
0
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
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
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
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
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
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
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
13. Write a C program to check if two given non-negative integers have the same last
digit.
Expected Output:
0
1
1
0
1
0
1
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
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
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
1
1
1
0
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
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
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
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
25. Write a C program to check if two or more nonnegative integers have the same
rightmost digit.
Expected Output:
1
1
0
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
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
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
16
23
12
18
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
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
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
1
0
1
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
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
36. Write a C program to compute the sum of the elements of an array of integers.
Expected Output:
150
10
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:
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:
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:
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
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
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
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:
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:
46. Write a C program to create an array taking two middle elements from a given array
of integers of length even.
Expected Output:
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:
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:
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
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:
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:
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
55. Write a C program to check whether a given array of integers contains 5's and 7's.
Expected Output:
1
0
1
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
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
1
0
1
1
1
0
1
1
0
1
1
0
1
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
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
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
1
0
1
1
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
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
68. Write a C program to shift an element in the left direction and return a newly
created array.
Expected Output:
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:
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:
71. Write a C program to create a new array from a given array of integers shifting all
zeros to left direction.
Expected Output:
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:
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:
0
1
1
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