Program to find Factorial of a given number and Fibonacci Series
using System;
using System.Text;
namespace ProblemPractice
{
//FIBONACCI SERIES
class Program1
{
public static void Main(String[] args)
{
string str;
do
{
Console.WriteLine("Press 1 for Fibnacci and 2 for Factorial :");
int val = Int32.Parse(Console.ReadLine());
if (val == 1)
{
//Fibonacci
Fibonacci();
}
else if (val == 2)
{
//Factorial
Console.WriteLine("Enter Value:");
int num = Int32.Parse(Console.ReadLine());
int factNum = Factorial(num);
Console.WriteLine(factNum);
}
else
{
Console.WriteLine("Wrong option...Press any key to Exit.");
}
Console.WriteLine("Want to Continue, Press \"y\"");
str = Console.ReadLine();
} while (str == "y");
}
private static int Factorial(int num)
{
int fact;
if (num == 1)
{
return 1;
}
else
{
fact= num * Factorial(num - 1);
return fact;
}
}
private static void Fibonacci()
{
int n = Int32.Parse(Console.ReadLine());
int n1, n2, n3;
n1 = 0; n2 = 1;
int flag = 3;
Console.Write(n1 + " " + n2 + " ");
while (flag <= n)
{
n3 = n1 + n2;
Console.Write(n3 + " ");
n1 = n2;
n2 = n3;
flag++;
}
Console.WriteLine("");
}
}
}
Fibonacci Series |
using System;
using System.Text;
namespace ProblemPractice
{
//FIBONACCI SERIES
class Program1
{
public static void Main(String[] args)
{
string str;
do
{
Console.WriteLine("Press 1 for Fibnacci and 2 for Factorial :");
int val = Int32.Parse(Console.ReadLine());
if (val == 1)
{
//Fibonacci
Fibonacci();
}
else if (val == 2)
{
//Factorial
Console.WriteLine("Enter Value:");
int num = Int32.Parse(Console.ReadLine());
int factNum = Factorial(num);
Console.WriteLine(factNum);
}
else
{
Console.WriteLine("Wrong option...Press any key to Exit.");
}
Console.WriteLine("Want to Continue, Press \"y\"");
str = Console.ReadLine();
} while (str == "y");
}
private static int Factorial(int num)
{
int fact;
if (num == 1)
{
return 1;
}
else
{
fact= num * Factorial(num - 1);
return fact;
}
}
private static void Fibonacci()
{
int n = Int32.Parse(Console.ReadLine());
int n1, n2, n3;
n1 = 0; n2 = 1;
int flag = 3;
Console.Write(n1 + " " + n2 + " ");
while (flag <= n)
{
n3 = n1 + n2;
Console.Write(n3 + " ");
n1 = n2;
n2 = n3;
flag++;
}
Console.WriteLine("");
}
}
}
No comments:
Post a Comment