Here are 10 C programming exercises that involve writing and using functions:
Calculate Area of a Circle: Write a program that calculates the area of a circle. Use a function to take the radius as input and return the area.
Factorial Calculation: Create a program to calculate the factorial of a given number. Write a function to perform the factorial calculation.
Palindrome Check: Implement a function to check if a given string is a palindrome (reads the same backward as forward). Ignore spaces and consider case.
Sum of Array Elements: Write a program that calculates the sum of elements in an array. Use a function to perform the summation.
Reverse a String: Implement a function to reverse a string. Modify the original string in place.
Prime Number Checker: Create a program to check if a given number is prime. Use a function to perform the prime checking.
Calculate Power: Write a program that calculates the power of a number. Create a function that takes the base and exponent as parameters and returns the result.
Fibonacci Series: Generate the first N terms of the Fibonacci series. Implement a function to calculate each term.
GCD (Greatest Common Divisor): Write a program to find the GCD of two numbers. Create a function to calculate the GCD using the Euclidean algorithm.
Matrix Multiplication: Implement a program that multiplies two matrices. Define a function to perform the matrix multiplication.
These exercises cover a range of function-related concepts and will help you practice and strengthen your skills in C programming. I have solved all of the above with explanatory comments. You should first make a sincere attempt at solving these problems before looking at the solutions.
1. Calculate Area of a Circle
#include <stdio.h>
float calculateArea(float radius) {
float pi = 3.14159;
float area = pi * radius * radius;
return area;
}
int main() {
float radius, area;
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
if (radius >= 0) {
area = calculateArea(radius);
printf("The area of the circle with radius %.2f is: %.2f\n", radius, area);
} else {
printf("Error: Radius cannot be negative.\n");
}
return 0;
}
2. Factorial Calculation
#include <stdio.h>
int factorial(int n) {
if (n <= 1)
return 1;
return n * factorial(n - 1);
}
int main() {
int num;
printf("Enter a positive integer: ");
scanf("%d", &num);
if (num >= 0) {
printf("Factorial of %d is %d\n", num, factorial(num));
} else {
printf("Error: Enter a positive integer.\n");
}
return 0;
}
3. Palindrome Check
#include <stdio.h>
#include <string.h>
int isPalindrome(char str[]) {
int len = strlen(str);
for (int i = 0; i < len / 2; i++) {
if (str[i] != str[len - i - 1]) {
return 0;
}
}
return 1;
}
int main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str);
if (isPalindrome(str)) {
printf("%s is a palindrome.\n", str);
} else {
printf("%s is not a palindrome.\n", str);
}
return 0;
}
4. Sum of Array Elements
#include <stdio.h>
int arraySum(int arr[], int n) {
int sum = 0;
for (int i = 0; i < n; i++) {
sum += arr[i];
}
return sum;
}
int main() {
int n, arr[100];
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Sum of array elements is %d\n", arraySum(arr, n));
return 0;
}
5. Reverse a String
#include <stdio.h>
#include <string.h>
void reverseString(char str[]) {
int len = strlen(str);
for (int i = 0, j = len - 1; i < j; i++, j--) {
char temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
int main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str);
reverseString(str);
printf("Reversed string: %s\n", str);
return 0;
}
6. Prime Number Checker
#include <stdio.h>
#include <string.h>
void reverseString(char str[]) {
int len = strlen(str);
for (int i = 0, j = len - 1; i < j; i++, j--) {
char temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
int main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str);
reverseString(str);
printf("Reversed string: %s\n", str);
return 0;
}
7. Calculate Power
#include <stdio.h>
double power(double base, int exponent) {
double result = 1.0;
for (int i = 0; i < exponent; i++) {
result *= base;
}
return result;
}
int main() {
double base, result;
int exponent;
printf("Enter base and exponent: ");
scanf("%lf %d", &base, &exponent);
result = power(base, exponent);
printf("%.2lf raised to power %d is %.2lf\n", base, exponent, result);
return 0;
}
8. Fibonacci Series
#include <stdio.h>
void fibonacci(int n) {
int a = 0, b = 1, c;
printf("Fibonacci series up to %d terms:\n", n);
for (int i = 0; i < n; i++) {
printf("%d ", a);
c = a + b;
a = b;
b = c;
}
printf("\n");
}
int main() {
int n;
printf("Enter number of terms: ");
scanf("%d", &n);
fibonacci(n);
return 0;
}
9. GCD (Greatest Common Divisor)
#include <stdio.h>
int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
int main() {
int num1, num2;
printf("Enter two positive integers: ");
scanf("%d %d", &num1, &num2);
printf("GCD of %d and %d is %d\n", num1, num2, gcd(num1, num2));
return 0;
}
10. Matrix Multiplication.
#include <stdio.h>
void multiplyMatrices(int firstMatrix[10][10], int secondMatrix[10][10], int result[10][10], int rowFirst, int colFirst, int rowSecond, int colSecond) {
for (int i = 0; i < rowFirst; ++i) {
for (int j = 0; j < colSecond; ++j) {
result[i][j] = 0;
}
}
for (int i = 0; i < rowFirst; ++i) {
for (int j = 0; j < colSecond; ++j) {
for (int k = 0; k < colFirst; ++k) {
result[i][j] += firstMatrix[i][k] * secondMatrix[k][j];
}
}
}
}
void displayMatrix(int matrix[10][10], int row, int col) {
printf("Matrix:\n");
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
}
int main() {
int rowFirst, colFirst, rowSecond, colSecond;
printf("Enter dimensions of the first matrix (rows and columns): ");
scanf("%d %d", &rowFirst, &colFirst);
printf("Enter dimensions of the second matrix (rows and columns): ");
scanf("%d %d", &rowSecond, &colSecond);
if (colFirst != rowSecond) {
printf("Error: Matrix multiplication is not possible.\n");
return 0;
}
int firstMatrix[10][10], secondMatrix[10][10], result[10][10];
printf("Enter elements of the first matrix:\n");
for (int i = 0; i < rowFirst; ++i) {
for (int j = 0; j < colFirst; ++j) {
scanf("%d", &firstMatrix[i][j]);
}
}
printf("Enter elements of the second matrix:\n");
for (int i = 0; i < rowSecond; ++i) {
for (int j = 0; j < colSecond; ++j) {
scanf("%d", &secondMatrix[i][j]);
}
}
multiplyMatrices(firstMatrix, secondMatrix, result, rowFirst, colFirst, rowSecond, colSecond);
printf("\n");
displayMatrix(firstMatrix, rowFirst, colFirst);
printf("\n");
displayMatrix(secondMatrix, rowSecond, colSecond);
printf("\nResult of matrix multiplication:\n");
displayMatrix(result, rowFirst, colSecond);
return 0;
}