Pages

Showing posts with label Basic Programmes. Show all posts
Showing posts with label Basic Programmes. Show all posts

Wednesday, April 3, 2013

Armstrong Number Program in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ArmstrongNumber
{
    class Program
    {
        static void Main(string[] args)
        {int n,m,s=0,r;
            Console.WriteLine("Enter a Number");
            n=Convert.ToInt16(Console.ReadLine());
            m = n;
            while (n > 0)
            {
                r = n % 10;
                s = s + r * r * r;
                n = n / 10;
            }
            if (m == s)
                Console.WriteLine("Armstrong Number");
            else
                Console.WriteLine("Not an Armstrong Number");
            Console.ReadKey();
        }
    }
}

Palindrome Program in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PrimeNumber
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, j = 2, flag = 1;
            Console.WriteLine("Enter a Number");
            i =Convert.ToInt16(Console.ReadLine());
            while (j <= i / 2)
            {
                if (i % j == 0)
                {
                    flag = 0;
                    break;
                }
                else
                {
                    j++;
                }
            }
            if (flag == 0)
                Console.WriteLine("Given Number is not a Prime");
            else
                Console.WriteLine("Given Number is Prime");
            Console.ReadKey();
        }
    }
}

Prime Number program in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PrimeNumber
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, j = 2, flag = 1;
            Console.WriteLine("Enter a Number");
            i =Convert.ToInt16(Console.ReadLine());
            while (j <= i / 2)
            {
                if (i % j == 0)
                {
                    flag = 0;
                    break;
                }
                else
                {
                    j++;
                }
            }
            if (flag == 0)
                Console.WriteLine("Given Number is not a Prime");
            else
                Console.WriteLine("Given Number is Prime");
            Console.ReadKey();
        }
    }
}

Factorial Program in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Factorial
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, n, fact = 1;
            Console.WriteLine("Enter a Number");
            n = Convert.ToInt16(Console.ReadLine());
            for (i = 1; i <= n; i++)
            {
                fact = fact * i;
            }
            Console.WriteLine("Factorial of {0} is {1}",n,fact);
            Console.ReadKey();
        }
    }

}