C Programming Exercises, Practice, Solution : Function
C Function [12 exercises with solution]
[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]
1. Write a program in C to show the simple structure of a function.
Expected Output :
The total is : 11
2. Write a program in C to find the square of any number using the function.
Test Data :
Input any number for square : 20
Expected Output :
The square of 20 is : 400.00
3. Write a program in C to swap two numbers using a function.
Test Data :
Input 1st number : 2
Input 2nd number : 4
Expected Output :
Before swapping: n1 = 2, n2 = 4 After swapping: n1 = 4, n2 = 2
4. Write a program in C to check if a given number is even or odd using the function.
Test Data :
Input any number : 5
Expected Output :
The entered number is odd.
5. Write a program in C to find the sum of the series 1!/1+2!/2+3!/3+4!/4+5!/5 using the function.
Expected Output :
The sum of the series is : 34
6. Write a program in C to convert a decimal number to a binary number using the function.
Test Data :
Input any decimal number : 65
Expected Output :
The Binary value is : 1000001
7. Write a program in C to check whether a number is a prime number or not using the function.
Test Data :
Input a positive number : 5
Expected Output :
The number 5 is a prime number.
8. Write a program in C to get the largest element of an array using the function.
Test Data :
Input the number of elements to be stored in the array :5
Input 5 elements in the array :
element - 0 : 1
element - 1 : 2
element - 2 : 3
element - 3 : 4
element - 4 : 5
Expected Output :
The largest element in the array is : 5
9. Write a program in C to check Armstrong and Perfect numbers using the function.
Test Data :
Input any number: 371
Expected Output :
The 371 is an Armstrong number. The 371 is not a Perfect number.
10. Write a program in C to print all perfect numbers in a given range using the function.
Test Data :
Input lowest search limit of perfect numbers : 1
Input lowest search limit of perfect numbers : 100
Expected Output :
The perfect numbers between 1 to 100 are : 6 28
11. Write a program in C to check whether two given strings are an anagram.
Test Data :
Input the first String : spare
Input the second String : pears
Expected Output :
spare and pears are Anagram.
12. Write a C program to find the maximum and minimum of some values using a function that returns an array.
Test Data :
Input 5 values
25
11
35
65
20
Expected Output :
Number of values you want to input: Input 5 values Minimum value is: 11 Maximum value is: 65
C Programming Code Editor:
More to Come !
Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.
C Programming: Tips of the Day
Returning an array using C
You can't return arrays from functions in C. You also can't (shouldn't) do this:
char *returnArray(char array []){ char returned [10]; //methods to pull values from array, interpret them, and then create new array return &(returned[0]); //is this correct? }
returned is created with automatic storage duration and references to it will become invalid once it leaves its declaring scope, i.e., when the function returns.
You will need to dynamically allocate the memory inside of the function or fill a preallocated buffer provided by the caller.
Dynamically allocate the memory inside of the function (caller responsible for deallocating ret)
char *foo(int count) { char *ret = malloc(count); if(!ret) return NULL; for(int i = 0; i < count; ++i) ret[i] = i; return ret; }
Call it like so:
int main() { char *p = foo(10); if(p) { // do stuff with p free(p); } return 0; }
Ref : https://bit.ly/3yFIeao
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook