Practice Problems on C Functions In C Language

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

  1. Write a function in C that takes two integers as arguments and returns the largest of the two.

Copy code

int max(int a, int b) {    if (a > b) {        return a;    } else {        return b;    } }

  1. Write a function in C that takes an array of integers and its size as arguments, and returns the sum of all the elements in the array.

Copy code

int sumArray(int* arr, int size) {    int sum = 0;    for (int i = 0; i < size; i++) {        sum += arr[i];    }    return sum; }

  1. Write a function in C that takes a string as an argument and returns the number of vowels in the string.

Copy code

int countVowels(char* str) {    int count = 0;    for (int i = 0; str[i] != '\0'; i++) {        if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u' ||            str[i] == 'A' || str[i] == 'E' || str[i] == 'I' || str[i] == 'O' || str[i] == 'U') {            count++;        }    }    return count; }

  1. Write a function in C that takes a positive integer as an argument and returns the factorial of that number.

Copy code

int factorial(int n) {    if (n == 0) {        return 1;    }    return n * factorial(n - 1); }

  1. Write a function in C that takes an array of integers and its size as arguments, and sorts the array in ascending order using the bubble sort algorithm.

Copy code

void bubbleSort(int* arr, int size) {    for (int i = 0; i < size - 1; i++) {        for (int j = 0; j < size - i - 1; j++) {            if (arr[j] > arr[j + 1]) {                int temp = arr[j];                arr[j] = arr[j + 1];                arr[j + 1] = temp;            }        }    } }

Please note that these are just sample solutions to the problems, and there are many ways to solve a problem in C. Also, these problems are not meant to be difficult, the goal is to give you an idea of how functions work in C and how they can be used to solve problems.

6. Function

Comments: 0

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?