In this Blog we will see some C# function programming examples.
Problem 1:
Create a function named
"Double" to calculate and return an integer number doubled. For
example. Double(7) should return 14.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Exercisefunc8
{
class Program
{
public static int Double(int number)
{
return number*2;
}
static void Main(string[] args)
{
Console.WriteLine("Enter any number");
int number = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Double of number {0} is {1}", number, Double(number));
Console.ReadLine();
}
}
}
Output:-
Create a function named "Double" to calculate the double of an integer number, and modify the data passed as an argument. It must be a "void" function and you must use "refererence parameters". For example.
x = 5;
Double(ref x);
Console.Write(x);
would display 10
Solution:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Exercisefunc8
{
class Program
{
public static void Double(ref int number)
{
number = number +number;
}
static void Main(string[] args)
{
int x = 5;
Double(ref x);
Console.Write(x);
Console.ReadLine();
}
}
}
OutPut:-

Create a function named
"Swap" to swap the values of two integer numbers, which are passed by
reference.
An example of use might be:
int x=5, y=3;
Swap(ref x, ref y);
Console.WriteLine("x={0}, y={1}", x, y);
(which should write "x=3, y=5").
An example of use might be:
int x=5, y=3;
Swap(ref x, ref y);
Console.WriteLine("x={0}, y={1}", x, y);
(which should write "x=3, y=5").
Solution:--
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Exercisefunc8
{
class Program
{
public static void Swap(ref int x,ref int y)
{
int swap;
swap = x;
x = y;
y = swap;
}
static void Main(string[] args)
{
int x = 5,y=3;
Swap(ref x,ref y);
Console.WriteLine("Swaped Values of x ={0}, y = {1}",x,y);
Console.ReadLine();
}
}
}
Output:-
Problem 4:-
Create a program that uses recursion to calculate a number in the Fibonacci series (in which the first two items are 1, and for the other elements, each one is the sum of the preceding two).
Solution:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Exercisefunc9
{
class Program
{
public static int Fibonacci(int number)
{
if ((number == 1) || (number == 2))
return 1;
else
return Fibonacci(number - 1) + Fibonacci(number - 2);
}
static void Main(string[] args)
{
int number;
Console.Write("Enter a number");
number = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Fibonacci of number {0} is {1}", number, Fibonacci(number));
Console.ReadLine();
}
}
}
Output:
Problem 5:
Create a function named "IsPrime", which receives an integer number and retuns true if it is prime, or false if it is not:
if (isPrime(127)) ...
if (isPrime(127)) ...
Solution:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Exercisefunc14
{
class Program
{
public static bool isprime(int n)
{
for (int i = 2; i < n; i++)
if (n % 2 == 0)
return false;
return true;
}
static void Main(string[] args)
{
//int number = Convert.ToInt32(Console.ReadLine());
if (isprime(97))
Console.WriteLine("97 number is prime");
else
Console.WriteLine("not prime");
Console.ReadLine();
}
}
}
Output:

Problem 6:
Create a recursive function to calculate the factorial of the number specified as parameter:
Console.Write ( Factorial (6) );
would display 720
Solution:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Exercisefunc18
{
class Program
{
public static int Factorial(int number)
{
if (number == 1)
return 1;
else
return number * Factorial(number - 1);
}
static void Main(string[] args)
{
int n;
Console.WriteLine("Enter any number");
n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Factorial of number {0} is {1}", n, Factorial(n));
Console.ReadLine();
}
}
}
Output:
Problem 7:
Create an iterative (non-recursive) function to calculate the factorial of the number specified as parameter:
Console.Write ( Factorial (6) );
would display
720
Console.Write ( Factorial (6) );
would display
720
Solution:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Exercisefunc18
{
class Program
{
public static int Factorial(int number)
{
int result = 1;
for (int i = 1; i <= number; i++)
result = result * i;
return result;
}
static void Main(string[] args)
{
int n;
Console.WriteLine("Enter any number");
n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Factorial of number {0} is {1}", n, Factorial(n));
Console.ReadLine();
}
}
}
Output:
Problem 8:
Create a function named "GetInt", which displays on screen the text received as a parameter, asks the user for an integer number, repeats if the number is not between the minimum value and the maximum value which are indicated as parameters, and finally returns the entered number:
age = GetInt("Enter your age", 0, 150);
would become:
Enter your age: 180 Not a valid answer. Must be no more than 150. Enter your age: -2 Not a valid answer. Must be no less than 0. Enter your age: 20 (the value for the variable "age" would be 20)
Solution :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Exercisefunc19
{
class Program
{
public static int getInt(string text, int low, int high)
{
int answer;
do
{
Console.Write(text);
answer = Convert.ToInt32(Console.ReadLine());
if ((answer > high))
Console.WriteLine("Not a valid answer. Must be no more than 150");
if ((answer < low))
Console.WriteLine("Not a valid answer. Must be no less than 0");
} while ((answer < low) || (answer > high));
return answer;
}
static void Main(string[] args)
{
int age = getInt("Enter your age: ", 0, 150);
Console.WriteLine("The age is {0}", age);
Console.ReadLine();
}
}
}
OutPut:
Problem 9:
Create a function which draws a parallelogram, with the width, height and character specified as parameters:
DrawParallelogram(10,4,'*');
would display
**********
_**********
__**********
___**********
(The symbol _ means a position where a blank space must be displayed)
DrawParallelogram(10,4,'*');
would display
**********
_**********
__**********
___**********
(The symbol _ means a position where a blank space must be displayed)
Solution:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Exercisefunc20
{
class Program
{
static void drawparreellogram(int height,int width,char character)
{
for(int i=0; i<height; i++)
{
for(int j=0;j<width;j++)
Console.Write(character);
Console.WriteLine();
for(int c=0;c<=i;c++)
Console.Write(" ");
}
}
static void Main(string[] args)
{
drawparreellogram(10, 4, '%');
Console.ReadLine();
}
}
}
Output:
Problem 10:
Create a function that calculates the amount of numeric digits and vowels that a text string contains. It will accept three parameters: the string that we want to search, the variable that returns the number of digits, and the number of vowels, in that order). The function should be called "CountDV". Use it like this:
CountDV ("This is the phrase 12", ref amountOfDigits, ref amountOfVowels)
In this case, amountOfDigits would be 2 and amountOfVowels would be 5
CountDV ("This is the phrase 12", ref amountOfDigits, ref amountOfVowels)
In this case, amountOfDigits would be 2 and amountOfVowels would be 5
Solution:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Exercisefunc21
{
class Program
{
public static void CountDV(string answer,ref int amountofDigits,ref int amountofVowels)
{
amountofDigits = 0;
amountofVowels = 0;
for(int i =0;i<answer.Length;i++)
{
switch(answer.Substring(i,1).ToLower())
{
case "a":
case "e":
case "i":
case "o":
case "u":
amountofVowels++;
break;
case "1":
case "2":
case "3":
case "4":
case "5":
case "6":
case "7":
case "8":
case "9":
amountofDigits++;
break;
}}
}
static void Main(string[] args)
{
int amountofDigits = 0;
int amountofVowels = 0;
CountDV("this is the phrase 12",ref amountofDigits,ref amountofVowels);
Console.WriteLine("Amount of digits {0}",amountofDigits);
Console.WriteLine("Amount of vowels {0}",amountofVowels);
Console.ReadLine();
}
}
}
Output:
Problem 11:
Create a function that tells if a string is an intenger number. It should be used like this:
if (IsNumber ("1234"))
System.Console.WriteLine ("It is a numerical value");
if (IsNumber ("1234"))
System.Console.WriteLine ("It is a numerical value");
Solution:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Exercisefunc22
{
class Program
{
public static bool isnumber (string text)
{
for (int i=0;i<text.Length;i++)
if((text[i] < '0' ) || (text[i] >'9'))
return false;
return true;
}
static void Main(string[] args)
{
Console.WriteLine("isnumber {0}", isnumber("12"));
Console.ReadLine();
}
}
}
Output:
Problem 12:
Create a function named MinMaxArray, to return the minimum and maximum values stored in an array, using reference parameters:
float[] data={1.5f, 0.7f, 8.0f}
MinMaxArray(data, ref minimum, ref maximum); (after that call, minimum would contain 0.7, and maximum would contain 8.0)
Solution:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Exercisefunc25
{
class Program
{
public static void MinMaxarray(float[] number,ref float min,ref float max)
{
max = number[0];
min = number[0];
for(int i=1; i<number.Length;i++)
{
if (number[i] > max)
max = number[i];
if (number[i] < min)
min = number[i];
}
}
static void Main(string[] args)
{
float[] data = { 3.5f, 0.5f, 9.0f };
float min = 0.0f;
float max = 0.0f;
MinMaxarray(data, ref min, ref max);
Console.WriteLine("Min {0} Max {1}", min, max);
Console.ReadLine();
}
}
}
Output:
Problem 13:
Create an iterative function to say whether a string is symmetric (a palindrome). For example, "RADAR" is a palindrome.
Solution:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Exercisefunc26
{
class Program
{
public static bool isPalindrmoe(string text)
{
text = text.ToUpper();
int begin = 0;
int end = text.Length - 1;
for ( begin =0;begin < end ;begin++)
{
if (text[begin] != text[end])
return false;
end--;
}
return true;
}
static void Main(string[] args)
{
Console.WriteLine("{0} Palin:",isPalindrmoe("Radar"));
Console.ReadLine();
}
}
}
Output:










No comments:
Post a Comment