Problem Solving using C Language

0 of 76 lessons complete (0%)

Functions

Practice Questions

Basic Function Practice:

  1. Simple Addition Function: Write a function that takes two integers as input and returns their sum. Call this function from main().
  2. Function to Find the Maximum of Two Numbers: Write a function max() that accepts two integer arguments and returns the larger of the two.
  3. Temperature Conversion: Write a function that converts a given temperature from Celsius to Fahrenheit.
  4. Function to Check Even or Odd: Write a function isEven() that takes an integer and returns 1 if it’s even and 0 if it’s odd.
  5. Factorial Calculation: Write a recursive function factorial() to calculate the factorial of a number.
  6. Simple Interest Calculation: Write a function to calculate the simple interest, given principal, rate of interest, and time (in years).

Intermediate Function Practice:

  1. Prime Number Check: Write a function isPrime() that takes an integer as input and returns 1 if it’s a prime number and 0 otherwise.
  2. Function to Reverse a Number: Write a function reverseNumber() that takes an integer as input and returns the reverse of that number.
  3. String Length Calculation: Write a function stringLength() that takes a string as input and returns the length of the string without using the strlen() function.
  4. Find GCD of Two Numbers: Write a function gcd() that returns the greatest common divisor (GCD) of two integers using recursion.
  5. Fibonacci Series: Write a function fibonacci(n) that prints the first n terms of the Fibonacci sequence using recursion.

Advanced Function Practice:

  1. Check Palindrome: Write a function isPalindrome() that checks if a given string is a palindrome.
  2. Pass By Reference: Write a function to swap two numbers using pass-by-reference (using pointers).
  3. Matrix Addition: Write a function addMatrices() that takes two 2D arrays (matrices) and returns the result of their addition.
  4. String Comparison: Write a function compareStrings() that takes two strings and returns 0 if they are equal, and a non-zero value if they are not (without using strcmp()).
  5. Function to Find Minimum and Maximum in an Array: Write two functions findMin() and findMax() that take an array and its size as input, and return the minimum and maximum values, respectively.

Function and Arrays Practice:

  1. Sum of Array Elements: Write a function sumArray() that takes an array and its size as input and returns the sum of all its elements.
  2. Average of Array Elements: Write a function averageArray() that calculates and returns the average of the elements in an array.
  3. Search Element in Array: Write a function searchElement() that searches for a specific element in an array and returns its index or -1 if the element is not found.
  4. Sorting an Array: Write a function sortArray() that sorts an array of integers in ascending order using any sorting algorithm (e.g., bubble sort).

Function with Structures:

  1. Structure and Function: Define a structure Student with name, age, and marks. Write a function displayStudentDetails() that takes a Student structure as input and prints the student’s details.
  2. Pass Structure to Function: Write a program where you create a structure Point with x and y coordinates, and write a function distance() to calculate the distance between two points.

Advanced Recursive Function Practice:

  1. Tower of Hanoi: Write a recursive function to solve the Tower of Hanoi puzzle.
  2. Power of a Number: Write a recursive function power(base, exponent) to calculate the value of a number raised to a power.
  3. Binary Search: Write a recursive function binarySearch() to search for an element in a sorted array.