Wednesday, 30 October 2013
Fibonacci Series in C#
using System;
using System.Collections.Generic;
using System.Text;
namespace Fib
{
class Fib
{
static void Main(string[] args)
{
int f0=0,f1=1,fact;
int n= Convert.ToInt16(Console.ReadLine());
Console.WriteLine("Fibonacci numbers" +f0);
Console.WriteLine("Fibonacci numbers" +f1);
for(int i=1;i<=n;i++)
{
fact=f0+f1;
f0=f1;
f1=fact;
Console.WriteLine("Fibonacci numbers" +fact);
}
Console.ReadLine();
}
}}
using System.Collections.Generic;
using System.Text;
namespace Fib
{
class Fib
{
static void Main(string[] args)
{
int f0=0,f1=1,fact;
int n= Convert.ToInt16(Console.ReadLine());
Console.WriteLine("Fibonacci numbers" +f0);
Console.WriteLine("Fibonacci numbers" +f1);
for(int i=1;i<=n;i++)
{
fact=f0+f1;
f0=f1;
f1=fact;
Console.WriteLine("Fibonacci numbers" +fact);
}
Console.ReadLine();
}
}}
Simple ways to get a Pascal Triangle in C#
using System;
using System.Collections.Generic;
using System.Text;
namespace PascalTriangle
{
class PascalTriangle
{
static void Main(string[] args)
{
System.Console.WriteLine("Pascal Triangle Program");
System.Console.Write("Enter the number of rows: ");
string input = System.Console.ReadLine();
int n = Convert.ToInt32(input);
for(int i=0; i<n; i++)
{
int c=1;
for(int j=0; j<=i; j++)
{
System.Console.Write(c);
c=c*(i-j)/(j+1);
System.Console.Write(" ");
}
System.Console.Write("\n");
}
System.Console.WriteLine();
System.Console.WriteLine();
}
}
}
using System.Collections.Generic;
using System.Text;
namespace PascalTriangle
{
class PascalTriangle
{
static void Main(string[] args)
{
System.Console.WriteLine("Pascal Triangle Program");
System.Console.Write("Enter the number of rows: ");
string input = System.Console.ReadLine();
int n = Convert.ToInt32(input);
for(int i=0; i<n; i++)
{
int c=1;
for(int j=0; j<=i; j++)
{
System.Console.Write(c);
c=c*(i-j)/(j+1);
System.Console.Write(" ");
}
System.Console.Write("\n");
}
System.Console.WriteLine();
System.Console.WriteLine();
}
}
}
Subscribe to:
Posts (Atom)