C Exercises – Practice C Programming with Hands-On Examples
In this tutorial, you’ll find a collection of C programming exercises that will help you practice and improve your skills. Each exercise is designed to challenge your understanding of key C concepts, from basic syntax to more advanced topics like memory management and data structures.
๐น Exercise 1: Print the First 10 Natural Numbers
Write a C program that prints the first 10 natural numbers (1 to 10) using a loop.
๐ Example 1: Print Natural Numbers
#include <stdio.h> int main() { int i; for (i = 1; i <= 10; i++) { printf("%d ", i); // Print numbers from 1 to 10 } return 0; }
๐น Exercise 2: Check if a Number is Prime
Write a C program to check whether a given number is a prime number or not. A prime number is a number greater than 1 that is divisible only by 1 and itself.
๐ Example 2: Prime Number Check
#include <stdio.h> int main() { int num, i, isPrime = 1; printf("Enter a number: "); scanf("%d", &num); for (i = 2; i <= num / 2; i++) { if (num % i == 0) { isPrime = 0; break; } } if (isPrime && num > 1) { printf("%d is a prime number.\n", num); } else { printf("%d is not a prime number.\n", num); } return 0; }
๐น Exercise 3: Reverse a String
Write a C program to reverse a string entered by the user without using any library functions.
๐ Example 3: Reverse a String
#include <stdio.h>> #include <string.h> int main() { char str[100], temp; int i, j; printf("Enter a string: "); fgets(str, sizeof(str), stdin); i = 0; j = strlen(str) - 1; while (i < j) { temp = str[i]; str[i] = str[j]; str[j] = temp; i++; j--; } printf("Reversed string: %s\n", str); return 0; }
๐น Exercise 4: Calculate Factorial of a Number
Write a C program to calculate the factorial of a given number using both an iterative and recursive approach.
๐ Example 4: Factorial of a Number (Iterative)
#include <stdio.h> int main() { int num, i, fact = 1; printf("Enter a number: "); scanf("%d", &num); for (i = 1; i <= num; i++) { fact *= i; // Multiply the current number with fact } printf("Factorial of %d is %d\n", num, fact); return 0; }
๐ Example 5: Factorial of a Number (Recursive)
#include <stdio.h> int factorial(int n) { if (n == 0) { return 1; // Base case: factorial of 0 is 1 } else { return n * factorial(n - 1); // Recursive call } } int main() { int num; printf("Enter a number: "); scanf("%d", &num); printf("Factorial of %d is %d\n", num, factorial(num)); return 0; }
๐น Exercise 5: Fibonacci Series
Write a C program to generate the Fibonacci series up to a given number. The Fibonacci sequence starts with 0 and 1, and each subsequent number is the sum of the previous two.
๐ Example 6: Fibonacci Series
#include <stdio.h> int main() { int n, first = 0, second = 1, next; printf("Enter the number of terms: "); scanf("%d", &n); printf("Fibonacci series: "); for (int i = 1; i <= n; i++) { if (i == 1) { printf("%d ", first); } else if (i == 2) { printf("%d ", second); } else { next = first + second; first = second; second = next; printf("%d ", next); } } printf("\n"); return 0; }
๐น Conclusion
These exercises cover a range of basic C programming concepts, including loops, conditionals, recursion, and arrays. Working through these examples will help you solidify your understanding of core C programming techniques. As you advance, try creating more complex programs that combine these concepts to build more sophisticated solutions.
๐ Practice Time!
Continue practicing these exercises and experiment with variations of each one. You can also try implementing more challenging problems to further enhance your C programming skills.