Search This Blog

This website completely moved to new domain. For latest content, visit www.programmingposts.com

C# PROGRAM TO GET FILE PATHS IN A GIVEN DIRECTORY


C# PROGRAM TO GET ALL FILE PATHS IN A GIVEN DIRECTORY

using System;
using System.IO;

namespace GetFileNames
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                string FolderPath ;
                string[] Files;
                Console.WriteLine(" Enter the Directory Path to get File Names in it : ");
                FolderPath=Console.ReadLine();
                Files = Directory.GetFiles(FolderPath);
                Console.WriteLine("\n The FilePaths in given Directory are : \n\n");
                foreach (string FileName in Files)
                {
                    Console.WriteLine(FileName);
                }
                Console.ReadLine();
            }
            catch (DirectoryNotFoundException ex)
            {
                Console.BackgroundColor = ConsoleColor.Red;
                Console.WriteLine("\n\n Directory Not Found..Press any key to exit...");
                Console.ReadKey();
            }
        }

    }
}

Sample Output1 :

Sample Output2 :



C# Program to Find Factorial of Number

C# Program to find Factorial of Given Number

using System;

namespace FactorialOfNumber
{
    class Program
    {
        static void Main(string[] args)
        {
            int num, i, fact = 1;
            //clrscr();
            Console.WriteLine("\n >>C# PROGRAM TO FIND FACTORIAL OF GIVEN NUMBER <<\n");
            Console.Write("\n Enter the Number whose Factorial you want: ");
            num=Convert.ToInt32(Console.ReadLine());
            for (i = num; i >= 2; i--) //i is intializing with num value and  is decremented till 2
            {
                fact = fact * i; //fact is updating with changing value of i
            }

            Console.WriteLine("\n The factorial of {0} is {1}.\n\n", num, fact);
            Console.ReadLine();
        }
    }
}
Output:


C# Program to find the Number Palindrome or not



C# Program to find whether the Number is Palindrome or not

using System;


namespace NumberPalindrome
{
    class Program
    {
        static void Main(string[] args)
        {
            int num, rem, sum = 0, temp;
            //clrscr();
            Console.WriteLine("\n >>>> To Find a Number is Palindrome or not <<<< ");
            Console.Write("\n Enter a number: ");
            num = Convert.ToInt32(Console.ReadLine());
            temp = num;
            while (num>0)
            {
                rem = num % 10;  //for getting remainder by dividing with 10
                num = num / 10; //for getting quotient by dividing with 10
                sum = sum * 10 + rem; /*multiplying the sum with 10 and adding
                           remainder*/
            }
            Console.WriteLine("\n The Reversed Number is: {0} \n", sum);
            if (temp == sum) //checking whether the reversed number is equal to entered number
            {
                Console.WriteLine("\n Number is Palindrome \n\n");
            }
            else
            {
                Console.WriteLine("\n Number is not a palindrome \n\n");
            }
            Console.ReadLine();
        }
    }
}

Output 1:

 Output 2: