# 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

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

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

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

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

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

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

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

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

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

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

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

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

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

**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*:

Elements in original array are: 10, 20, 30, 40 Elements in new array are: 20, 30, 40, 10

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

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

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

**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*:

Elements in original array are: 1, 5, 7 Elements in new array are: 1, 5, 1

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

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

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

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

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

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

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

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

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

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

*Expected Output*:

1 0 1

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

*Expected Output*:

1 1 0 1

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

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

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

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

**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*:

Elements in original array are: 10, 20, 30, 40 Elements in new array are: 20, 30, 40, 10

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

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

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

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

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

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

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

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

**Returning an array using C **

You can't return arrays from functions in C. You also can't (shouldn't) do this:

char *returnArray(char array []){ char returned [10]; //methods to pull values from array, interpret them, and then create new array return &(returned[0]); //is this correct? }

returned is created with automatic storage duration and references to it will become invalid once it leaves its declaring scope, i.e., when the function returns.

You will need to dynamically allocate the memory inside of the function or fill a preallocated buffer provided by the caller.

Dynamically allocate the memory inside of the function (caller responsible for deallocating ret)

char *foo(int count) { char *ret = malloc(count); if(!ret) return NULL; for(int i = 0; i < count; ++i) ret[i] = i; return ret; }

Call it like so:

int main() { char *p = foo(10); if(p) { // do stuff with p free(p); } return 0; }

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

**Weekly Trends**- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join

We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook