30 C Programming Basic Exercises for Intermediate with Solutions

Master intermediate C Programming skills with our comprehensive list of top 30 exercises. Dive into coding challenges that improve your understanding and proficiency in C Programming, setting a solid foundation for advanced challenges. Start your journey to C Programming mastery today!

Learning Objectives:

By the end of these exercises, you will be comfortable with intermediate concepts in C, such as pointers, arrays, dynamic memory allocation, and basic structures.

Exercise Instructions:

  • Start with the first exercise and attempt to solve it before checking the hint or solution.
  • Ensure you understand the logic behind each solution, as this will help you in more complex problems.
  • Use these exercises to reinforce your learning and identify areas that may require further study.

1. Write a function to check if a number is a palindrome.

Required Input:

12321

Expected Output:

12321 is a palindrome

Code In C

#include int isPalindrome(int num) { // Your code here } int main() { int num = 12321; if (isPalindrome(num)) { printf("%d is a palindrome\n", num); } else { printf("%d is not a palindrome\n", num); } return 0; }

Output

Click Run Button to view compiled output

2. Implement a function to find the GCD of two numbers.

Required Input:

36 60

Expected Output:

GCD of 36 and 60 is 12

Code In C

#include int gcd(int a, int b) { // Your code here } int main() { int a = 36, b = 60; printf("GCD of %d and %d is %d\n", a, b, gcd(a, b)); return 0; }

Output

Click Run Button to view compiled output

3. Write a function to sort an array {5, 3, 8, 6, 2} in ascending order.

Required Input:

5 3 8 6 2

Expected Output:

Sorted array: 2 3 5 6 8 

Code In C

#include void sortArray(int arr[], int n) { // Your code here } int main() { int arr[] = {5, 3, 8, 6, 2}; sortArray(arr, 5); printf("Sorted array: "); for (int i = 0; i < 5; i++) { printf("%d ", arr[i]); } printf("\n"); return 0; }

Output

Click Run Button to view compiled output

4. Implement a function to reverse a string "programming".

Required Input:

programming

Expected Output:

Reversed string: gnimmargorp

Code In C

#include #include void reverseString(char str[]) { // Your code here } int main() { char str[] = "programming"; reverseString(str); printf("Reversed string: %s\n", str); return 0; }

Output

Click Run Button to view compiled output

5. Write a function to calculate the factorial of a number (6) using recursion.

Required Input:

6

Expected Output:

Factorial of 6 is 720

Code In C

#include int factorial(int n) { // Your code here } int main() { int n = 6; printf("Factorial of %d is %d\n", n, factorial(n)); return 0; }

Output

Click Run Button to view compiled output

6. Write a function to find the minimum element in an array {3, 5, 2, 8, 1}.

Required Input:

3 5 2 8 1

Expected Output:

Minimum element: 1

Code In C

#include int findMin(int arr[], int n) { // Your code here } int main() { int arr[] = {3, 5, 2, 8, 1}; printf("Minimum element: %d\n", findMin(arr, 5)); return 0; }

Output

Click Run Button to view compiled output

7. Print the first 10 elements of the Fibonacci sequence.

Required Input:

10

Expected Output:

0 1 1 2 3 5 8 13 21 34 

Code In C

#include void printFibonacci(int n) { // Your code here } int main() { printFibonacci(10); return 0; }

Output

Click Run Button to view compiled output

8. Count the number of words in a string "C programming is fun".

Required Input:

C programming is fun

Expected Output:

Number of words: 4

Code In C

#include #include int countWords(char str[]) { // Your code here } int main() { char str[] = "C programming is fun"; printf("Number of words: %d\n", countWords(str)); return 0; }

Output

Click Run Button to view compiled output

9. Write a function to find the second largest element in an array {12, 35, 1, 10, 34, 1}.

Required Input:

12 35 1 10 34 1

Expected Output:

Second largest element: 34

Code In C

#include int secondLargest(int arr[], int n) { // Your code here } int main() { int arr[] = {12, 35, 1, 10, 34, 1}; printf("Second largest element: %d\n", secondLargest(arr, 6)); return 0; }

Output

Click Run Button to view compiled output

10. Calculate the sum of the diagonal elements in a 3x3 matrix.

Required Input:

Matrix:
 1 2 3
 4 5 6
 7 8 9

Expected Output:

Sum of diagonal elements: 15

Code In C

#include int diagonalSum(int matrix[3][3]) { // Your code here } int main() { int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; printf("Sum of diagonal elements: %d\n", diagonalSum(matrix)); return 0; }

Output

Click Run Button to view compiled output

ad vertical

1 of 3