Wednesday, May 18, 2016

C# Program to solve different problems


C# Program to solve different problems
Program Statement:Write a program which will take input a character ‘a’ value from user. You have to use switch statement to decide if value of a is ‘t’ then you have to call table function, if value of a is ‘f’ then call factorial function, if value of a is ‘p’ then call prime function, if value of a is ‘s’ then call srch function.You have to write four functions in your program;Table(int n1,n2)Factorial(int n3)Prime(int n4)Srch(char n5[], char c, char choice)Table function will print the table of n1 from 1 to n2.Factorial function will print the factorial of n3 if n3 is multiple of 2.Prime function will print the n4 if n4 is prime.Srch function will take first argument n5 as an array of characters and second element a character to be srch in the array and third element a character to decide which srching aorithm to be used.i.e. if user has passed the value of c as ‘s’ then Srch function will perform the sequential srch but if value of c is something else then Srch function will perform binary srch.Structure of your program will be like this. You have to make it exactly working.Switch(a){case ‘f’:factorial();brk;case ‘p’:prime();brk;case ‘t’:table();brk;case ‘s’:srch();brk;}
Solution: class functions
{
public int fact = 1;
public void factorial(int n3)
{
if (n3 % 2 == 0)
{
for (int i = 1; i <= n3; i++)
{
fact = fact * i;
}
Console.WriteLine("\n\t\tFactorial is : {0}\n\n", fact);
}
else
{
Console.WriteLine("\n\t\tIts not multiple of 2!\n\t\t");
}
}
public void prime(int n4)
{
int i = 2;

if (n4 == 1)
{
Console.WriteLine(" is not prime!");
}
for (i = 2; i <= n4 - 1; i++)
{
if (n4 % i == 0)
{
Console.WriteLine(" is not prime!");
brk;
}
}
if (n4 == i)
{
Console.WriteLine("{0} is a prime !", n4);
}
}
public void table(int n1, int n2)
{

for (int i = 1; i <= n2; i++)
{
Console.WriteLine("{0} * {1} = {2}", n1, i, n1 * i);
}
}
public void srch(char[] array, char s)
{
int p = 0;
forch (char c in array)
{
if (c == s)
{
Console.WriteLine("Character {0} found!", c);
p++;
brk;
}
}
if (p == 0)
Console.WriteLine("character not found.");
}
}

No comments:

Post a Comment