Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
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.]

1. Write a C program to compute the sum of the two given integer values. If the two values are the same, then return triple their sum. Go to the editor
Expected Output:

3
12

Click me to see the solution

2. Write a C program to get the absolute difference between n and 51. If n is greater than 51 return triple the absolute difference. Go to the editor
Expected Output:

6
21
0

Click me to see the 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. Go to the editor
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. Go to the editor
Expected Output:

1
1
0

Click me to see the solution

5. Write a C program to check whether a given positive number is a multiple of 3 or a multiple of 7. Go to the editor
Expected Output:

1
1
1
0

Click me to see the solution

6. Write a C program to check whether a given temperatures is less than 0 and the other is greater than 100. Go to the editor
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. Go to the editor
Expected Output:

1
0
1

Click me to see the solution

8. 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 said range otherwise return false. Go to the editor
Expected Output:

1
1
1
0

Click me to see the solution

9. 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 said range otherwise false. Go to the editor
Expected Output:

1
1
1
0

Click me to see the solution

10. Write a C program to check which number nearest to the value 100 among two given integers. Return 0 if the two numbers are equal. Go to the editor
Expected Output:

95
0
99

Click me to see the solution

11. 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. Go to the editor
Expected Output:

0
0
1
1

Click me to see the solution

12. Write a C program to find the larger value from two positive integer values that is in the range 20..30 inclusive, or return 0 if neither is in that range. Go to the editor
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. Go to the editor
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. Go to the editor
Expected Output:

1
0
1

Click me to see the solution

15. Write a C program to count the number of two 5's are next to each other in an array of integers. Also count the situation where the second 5 is actually a 6. Go to the editor
Expected Output:

1
2
1

Click me to see the solution

16. Write a C program to check if a triple is presents in an array of integers or not. If a value appears three times in a row in an array it is called a triple. Go to the editor
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.Go to the editor
Expected Output:

29
30
39
30

Click me to see the solution

18. Write a C program that accept two integers and return true if either one is 5 or their sum or difference is 5. Go to the editor
Expected Output:

1
0
1

Click me to see the solution

19. Write a C program to to test whether a given non-negative number is a multiple of 13 or it is one more than a multiple of 13. Go to the editor
Expected Output:

1
1
1
0

Click me to see the solution

20. Write a C program to check whether a given non-negative number is a multiple of 3 or 7, but not both. Go to the editor
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. Go to the editor
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. If one of the given integer value is in the range 10..20 inclusive return 18. Go to the editor
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. Go to the editor
Expected Output:

1
0
1

Click me to see the solution

24. Write a C program to check whether y is greater than x, and z is greater than y from three given integers x,y,z. Go to the editor
Expected Output:

1
1
0

Click me to see the solution

25. Write a C program to check whether two or more non-negative given integers have the same rightmost digit. Go to the editor
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 less than one of the others. Go to the editor
Expected Output:

1
1
0

Click me to see the solution

27. Write a C program to find the larger from two given integers. However if the two integers have the same remainder when divided by 5, then the return the smaller integer. If the two integers are the same, return 0. Go to the editor
Expected Output:

11
20
0

Click me to see the solution

28. Write a C program to check two given integers, each in the range 10..99. Return true if a digit appears in both numbers, such as the 3 in 13 and 33. Go to the editor
Expected Output:

1
0
1

Click me to see the solution

29. Write a C program to compute the sum of three given integers. If the two values are same return the third value. Go to the editor
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. If one of the values is 13 then do not count it and its right towards the sum.Go to the editor
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. However, if any of the values is in the range 10..20 inclusive then that value counts as 0, except 13 and 17. Go to the editor
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 value whichever value is nearest to 13 without going over. Return 0 if both numbers go over. Go to the editor
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 same. Go to the editor
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 and return true if the first element and the last element are equal in the given array. Go to the editor
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 and return true if they have the same first element or they have the same last element. Go to the editor
Expected Output:

1
0

Click me to see the solution

36. Write a C program to compute the sum of the elements of a given  array of integers. Go to the editor
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 left direction and return the new array. Go to the editor
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 and length 5. Go to the editor
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 length 5. Go to the editor
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. Go to the editor
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 a given array of integers of length 2, contains 15 or 20. Go to the editor
Expected Output:

1
1
0

Click me to see the solution

42. Write a C program to check whether a given array of integers of length 2, does not contain 15 or 20. Go to the editor
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. Go to the editor
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. Go to the editor
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 of length 3 and find the array which has the largest sum. Go to the editor
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. Go to the editor
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 array of integers, each length 3. Go to the editor
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 swapping the first and last elements of a given array of integers and length will be least 1. Go to the editor
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 a new array of length 3 from a given array (length atleast 3) containing the elements from the middle of the array. Go to the editor
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 first, last, and middle elements of a given array of integers of odd length (atleast 1). Go to the editor
Expected Output:

1
9
9

Click me to see the solution

51. Write a C program to count number of even elements in a given array of integers. Go to the editor
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 integer.Go to the editor
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 numbers starting with 5 followed by atleast one 6. Return 0 if the given array has no integer.Go to the editor
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. Go to the editor
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. Go to the editor
Expected Output:

1
0
1

Click me to see the solution

56. Write a C program to check whether the sum of all 5' in the array exactly 15 in a given array of integers. Go to the editor
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. Go to the editor
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.Go to the editor
Expected Output:

1
0
1

Click me to see the solution

59. Write a C program to check  whether a given array of integers contains no 3 or a 5. Go to the editor
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. Go to the editor
Expected Output:

1
0
1

Click me to see the solution

61. Write a C program to check a given array of integers and return true if the given array contains two 5's next to each other, or two 5 separated by one element. Go to the editor
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. Go to the editor
Expected Output:

0
1
0

Click me to see the solution

63. Write a C program to check a given array of integers and return true if the given array contains either 2 even or 2 odd values all next to each other. Go to the editor
Expected Output:

0
1
1

Click me to see the solution

64. Write a C program to check a given array of integers and return true if the value 5 appears 5 times and there are no 5 next to each other. Go to the editor
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. Go to the editor
Expected Output:

1
0
1
1

Click me to see the solution

66. Write a C program to check a given array of integers and return true if the specified number of same elements appears at the start and end of the given array. Go to the editor
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. Go to the editor
Expected Output:

1
0
1

Click me to see the solution

68. Write a C program to shift an element in left direction and return a new array. Go to the editor
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. Go to the editor
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 new array taking the elements after the element value 5 from a given array of integers. Go to the editor
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. Go to the editor
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 a new array after replacing  all the values 5 with 0 shifting all zeros to right direction. Go to the editor
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 new array from a given array of integers shifting all even numbers before all odd numbers. Go to the editor
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 whether the value of each element is equal or greater than the value of previous element of a given array of integers. Go to the editor
Expected Output:

0
1
1

Click me to see the solution

75. Write a C program to check a given array (length will be atleast 2) of integers and return true if there are two values 15, 15 next to each other. Go to the editor
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.



C Programming: Tips of the Day

Static variable inside of a function in C

The scope of variable is where the variable name can be seen. Here, x is visible only inside function foo().

The lifetime of a variable is the period over which it exists. If x were defined without the keyword static, the lifetime would be from the entry into foo() to the return from foo(); so it would be re-initialized to 5 on every call.

The keyword static acts to extend the lifetime of a variable to the lifetime of the programme; e.g. initialization occurs once and once only and then the variable retains its value - whatever it has come to be - over all future calls to foo().

Ref : https://bit.ly/3fOq7XP