Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

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.]

1. Write a program in C to show the simple structure of a function.Go to the editor
Expected Output :

The total is :  11  

Click me to see the solution

2. Write a program in C to find the square of any number using the function. Go to the editor
Test Data :
Input any number for square : 20
Expected Output :

The square of 20 is : 400.00

Click me to see the solution

3. Write a program in C to swap two numbers using function. Go to the editor
Test Data :
Input 1st number : 2
Input 2nd number : 4
Expected Output :

Before swapping: n1 = 2, n2 = 4                                                  
After swapping: n1 = 4, n2 = 2 

Click me to see the solution

4. Write a program in C to check a given number is even or odd using the function. Go to the editor
Test Data :
Input any number : 5
Expected Output :

 The entered number is odd.  

Click me to see the solution

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. Go to the editor
Expected Output :

 The sum of the series is : 34 

Click me to see the solution

6. Write a program in C to convert decimal number to binary number using the function. Go to the editor
Test Data :
Input any decimal number : 65
Expected Output :

 The Binary value is : 1000001 
 

Click me to see the solution

7. Write a program in C to check whether a number is a prime number or not using the function. Go to the editor
Test Data :
Input a positive number : 5
Expected Output :

The number 5 is a prime number. 

Click me to see the solution

8. Write a program in C to get the largest element of an array using the function. Go to the editor
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   

Click me to see the solution

9. Write a program in C to check armstrong and perfect numbers using the function. Go to the editor
Test Data :
Input any number: 371
Expected Output :

 The 371 is an Armstrong number.                                                 
 The 371 is not a Perfect number.  

Click me to see the solution

10. Write a program in C to print all perfect numbers in given range using the function. Go to the editor
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  
 

Click me to see the solution

11. Write a program in C to check whether two given strings are an anagram. Go to the editor
Test Data :
Input the first String : spare
Input the second String : pears
Expected Output :

 spare and pears are Anagram.    

Click me to see the solution

12. Write a C programming to find out maximum and minimum of some values using function which will return an array. Go to the editor
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  

Click me to see the solution

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

Static variable inside of a function in C

The scope of variable is where the variable name can be seen. Here, x is visible only inside function foo().

The lifetime of a variable is the period over which it exists. If x were defined without the keyword static, the lifetime would be from the entry into foo() to the return from foo(); so it would be re-initialized to 5 on every call.

The keyword static acts to extend the lifetime of a variable to the lifetime of the programme; e.g. initialization occurs once and once only and then the variable retains its value - whatever it has come to be - over all future calls to foo().

Ref : https://bit.ly/3fOq7XP