C Sharp Basic Program

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 5

odd...........even............................

using System;
namespace sample
{
class MainClass
{
public static void Main(string[] args)
{
int i = 1;
if( (i % 2) == 0 )
Console.WriteLine( "i is Even" );
else
Console.WriteLine( "i is Odd" );
i = 2;
if( (i % 2) == 0 )
Console.WriteLine( "i is Even" );
else
Console.WriteLine( "i is Odd" );
}
}
}

febonaci........................
static int Fibonacci (int x)
{
Console.WriteLine ("x = {0}", x);
if (x <= 1)
{
return 1;
}
return Fibonacci (x-1) + Fibonacci (x-2);
}
static void Main( )
{
Console.WriteLine ("Fibonacci no. = {0}", Fibonacci (5));
Console.ReadKey();
}

prime number................

using System;
class g9{
public static void Main(){
int num;
Console.Write("Enter the number till you want Prime Numb
er : ");
num=Convert.ToInt32(Console.ReadLine());
for(int i=2;i<=num;i++){
if(i<=5 && i!=4){
Console.Write(i+" ");
}
if(i%2!=0 && i%3!=0 && i%5!=0)
Console.Write(i+" ");
}
}
}

perfect number.....................

using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class PerfectNumbers
{
static void Main(string[] args)
{
}
public string Perfect(int value)
{
}
public void FindPerfects()
{
int sum = 0, x = 0;
for (int num = 1; num < 1000; num++)
{
for (int factor = 1; factor < num; factor++)
{
x = num % factor;

factorial of a number......................

public class test1


{
public static void Main(string[] args){
int fact=0;
Console.WriteLine("Enter A Number:");
fact = Convert.ToInt32(Console.ReadLine());
for (int i=1; i<=fact; i++){
if (fact%i == 0){ //It works
System.Console.WriteLine(i);
}
}
Console.Read();
}
}

armstrong number.............................

using System;
using System.Collections.Generic;
using System.Text;
namespace recursive_amstrong
{
class Program
{
static void Main(string[] args)
{
string str;
Console.WriteLine(" Enter the number ");
str = Console.ReadLine();
int convert = int.Parse(str);
int armstrong_no = armstrong(convert);
Console.WriteLine(armstrong_no);
Console.ReadLine();
}
static int armstrong(int value)
{
int factor, remainder,total = 0;
while (value > 0)
{
factor = value / 10;
remainder = value % 10;
total = total + remainder * remainder * remainder;
value = factor;
//return value;
}
return 1;

}
}
}

// Write a Program in C# to Check whether a number is Palindrome or not.


using System;
using System.Collections.Generic;
using System.Text;
namespace Lab_Ex_1
{
class PalindromeProgram
{
static void Main(string[] args)
{
Console.Write(" Enter any number: ");
int num = int.Parse(Console.ReadLine());
int rev = 0, n, rem;
n = num;
while (num != 0)
{
rem = num % 10;
rev = rev * 10 + rem;
num = num / 10;
}
Console.WriteLine(" \n Original number is " + n);
Console.WriteLine(" \n Reversed number is " + rev);
if (n == rev)
{
Console.WriteLine("\n Original number = Reversed number.");
Console.WriteLine("\n So, {0} is Palindorme ", n);
Console.ReadLine();
}
else
{
Console.WriteLine("\n Original number != Reversed number.");
Console.WriteLine("\n So, {0} is not a Palindorme ", n);
Console.ReadLine();
}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace pyramid
{
class Program
{
static void Main(string[] args)
{
Console.Write("IN PUT YOUR NUMBER :");
int myInt = int.Parse(Console.ReadLine());
for (int iCount1 = 1; iCount1 <= myInt; iCount1++)
{
for (int iCount2 = 1; iCount2 <= iCount1; iCount2++)
{
Console.Write(iCount2 + " ");
}
for (int iCount3=(iCount1-1);iCount3 >= 1; iCount3--)
{
Console.Write(iCount3 + " ");
}
Console.WriteLine("");
}
}
}
}

You might also like