w3resource logo
C# Sharp Exercises

C# Sharp programming Exercises, Practice, Solution : Function

C# Sharp Function [12 exercises with solution]

1. Write a program in C# Sharp to create a user define function. Go to the editor
Expected Output :
Welcome Friends!
Have a nice day!
Click me to see the solution

2. Write a program in C# Sharp to create a user define function with parameters. Go to the editor
Test Data :
Please input a name : John
Expected Output :
Welcome friend John !
Have a nice day!
Click me to see the solution

3. Write a program in C# Sharp to create a function for the sum of two numbers. Go to the editor
Test Data :
Enter a number: 15
Enter another number: 16
Expected Output :
The sum of two numbers is : 31
Click me to see the solution

4. Write a program in C# Sharp to create a function to input a string and count number of spaces are in the string. Go to the editor
Test Data :
Please input a string : This is a test string.
Expected Output :
"This is a test string." contains 4 spaces
Click me to see the solution

5. Write a program in C# Sharp to calculate the sum of elements in an array. Go to the editor
Test Data :
Input 5 elements in the array :
element - 0 : 5
element - 1 : 7
element - 2 : 3
element - 3 : 2
element - 4 : 9
Expected Output :
The sum of the elements of the array is 26
Click me to see the solution

6. Write a program in C# Sharp to create a function to swap the values of two integer numbers. Go to the editor
Test Data :
Enter a number: 5
Enter another number: 6
Expected Output :
Now the 1st number is : 6 , and the 2nd number is : 5
Click me to see the solution

7. Write a program in C# Sharp to create a function to calculate the result of raising an integer number to another. Go to the editor
Test Data :
Input Base number: 3
Input the Exponent : 2
Expected Output :
So, the number 3 ^ (to the power) 2 = 9
Click me to see the solution

8. Write a program in C# Sharp to create a function to display the n number Fibonacci sequence. Go to the editor
Test Data :
Input number of Fibonacci Series : 5
Expected Output :
The Fibonacci series of 5 numbers is :
0 1 1 2 3
Click me to see the solution

9. Write a program in C# Sharp to create a function to check whether a number is prime or not. Go to the editor
Test Data :
Input a number : 31
Expected Output :
31 is a prime number
Click me to see the solution

10. Write a program in C# Sharp to create a function to calculate the sum of the individual digits of a given number. Go to the editor
Test Data :
Enter a number: 1234
Expected Output :
The sum of the digits of the number 1234 is : 10
Click me to see the solution

11. Write a program in C# Sharp to create a recursive function to find the factorial of a given number. Go to the editor
Test Data :
Enter a number: 5
Expected Output :
The factorial of 5! is 120
Click me to see the solution

12. Write a program in C# Sharp to create a recursive function to calculate the Fibonacci number of a specific term. Go to the editor
Test Data :
Enter a number: 10
Expected Output :
The Fibonacci of 10 th term is 55
Click me to see the solution

C# Sharp Code Editor:





More to Come !