0% found this document useful (0 votes)
4 views

C Programming Function Problems Corrected

Uploaded by

pakisin499
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

C Programming Function Problems Corrected

Uploaded by

pakisin499
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

C Programming Function Problems

C Programming Function Problems


C Programming Function Problems

Easy:

1. Sum of Two Numbers

Write a function sum(int a, int b) that takes two integers and returns their sum. Call this function from main and print the

result.

2. Factorial of a Number

Write a function int factorial(int n) that returns the factorial of a given number n. Test the function by printing the factorial

of numbers 1 to 5.

3. Maximum of Two Numbers

Write a function int max(int a, int b) that returns the maximum of two numbers. Use this function to find the maximum of

two numbers input by the user.

4. Check Even or Odd

Write a function int isEven(int n) that returns 1 if a number is even and 0 if it is odd. Use this function in main to check if

a given number is even or odd.


C Programming Function Problems

Intermediate:

5. Prime Number Check

Write a function int isPrime(int n) that returns 1 if a number is prime and 0 if it is not. Use this function to check whether

a given number is prime.

6. GCD of Two Numbers

Write a function int gcd(int a, int b) that returns the greatest common divisor of two numbers. Test this function with

different pairs of numbers.

7. Array Sum

Write a function int sumArray(int arr[], int size) that takes an array and its size as arguments and returns the sum of its

elements. Test the function with different arrays.

8. Fibonacci Sequence

Write a function int fibonacci(int n) that returns the nth Fibonacci number. Use this function to print the first 10 Fibonacci

numbers.

9. Swap Two Numbers Using Pointers

Write a function void swap(int *a, int *b) that swaps the values of two integers using pointers. Test this function by

swapping two numbers in main.


C Programming Function Problems

Advanced:

10. Recursive Power Function

Write a recursive function int power(int base, int exp) that returns the value of base raised to the power exp. Use this

function to compute 2^5, 3^4, etc.

11. Palindrome Check

Write a function int isPalindrome(int num) that checks if a given number is a palindrome (reads the same forwards and

backwards). Use this function to check if different numbers are palindromes.

12. Sorting an Array

Write a function void sortArray(int arr[], int size) that sorts an array of integers in ascending order using the Bubble Sort

algorithm. Test the function with different arrays.

13. Matrix Multiplication

Write a function void multiplyMatrices(int mat1[3][3], int mat2[3][3], int res[3][3]) that multiplies two 3x3 matrices and

stores the result in a third matrix. Print the result in main.

14. Binary Search

Write a function int binarySearch(int arr[], int size, int target) that implements the binary search algorithm to find the

position of a target element in a sorted array. If the element is not found, return -1.

15. Menu-Driven Program

Create a menu-driven program where the user can choose different operations (like sum, difference, product, quotient,

GCD, etc.) to be performed using functions. Implement each operation as a separate function.
C Programming Function Problems

Expert:

16. String Reversal Using Pointers

Write a function void reverseString(char *str) that reverses a string in place using pointers. Test the function with various

strings.

17. N-Queens Problem

Write a function void solveNQueens(int n) that finds all possible solutions to the N-Queens problem using backtracking

and prints the results.

18. Tower of Hanoi

Write a recursive function void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod) to solve the Tower of

Hanoi puzzle. Print the sequence of moves needed to solve the puzzle.

19. Merge Sort

Write a function void mergeSort(int arr[], int l, int r) that implements the Merge Sort algorithm to sort an array of integers.

Test the function with different arrays.

20. Sudoku Solver

Write a function int solveSudoku(int grid[9][9]) that solves a given Sudoku puzzle using backtracking and prints the

solution.
C Programming Function Problems

Bonus Challenge:

21. Expression Evaluation

Write a function int evaluateExpression(char *expression) that evaluates a mathematical expression given as a string

(e.g., "3 + 5 * (2 - 4)") and returns the result. Implement support for basic arithmetic operations and parentheses.

You might also like