Sunday, 11 October 2020

Factorial of a Number

Step 1: Open Visual Studio and select console application.



Step 2: Write a Program to find factorial of number.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace NumberFactorialConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int number;
 
            Console.WriteLine("Enter a number to get factorial of number");
            number = int.Parse(Console.ReadLine());
 
            if (number > 0)
            {
                int factorial = new Program().MathFactorial(number);
                Console.WriteLine("Factorial of " + number + " is: " + factorial);
            }
            else
            {
                Console.WriteLine("Number must be greater than zero");
            }
            Console.ReadLine();
        }
 
        /// <summary>
        /// This method is used for get factorial of number
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        public int MathFactorial(int number)
        {
            return (number == 1) ? 1 : number * MathFactorial(number - 1);
        }
    }
 
}

Step 3 : Run the application then console will be open.
Step 4: Enter the string then press "Enter" key then see the output.




Monday, 5 October 2020

Program to find occurrence of character in string

 Program to find occurrence of character in string

Step 1: Open Visual Studio and select console application.

Step 2: Write a Program to find occurrence of character in string

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace CharacterOccurrenceApp
{
    class Program
    {
        static void Main(string[] args)
        {
            string Statement = string.Empty;
 
            Console.WriteLine("Enter a string to check occurrence of a character numbers");
 
            Statement = Console.ReadLine();
            Statement = Statement.ToLower();
 
            if (Statement.Length > 0)
            {
                while (Statement.Length > 0)
                {
                    int CountCharacter = 0;
                    Console.Write("Number of Occurance of '" + Statement[0] + "': ");
                   
                    //Checking the Character occurrence in the string entered by the user
                    for (int i = 0; i < Statement.Length; i++)
                    {
                        if (Statement[0] == Statement[i])
                        {
                            CountCharacter++;
                        }
                    }
 
                    //Count of each character
                    Console.Write(CountCharacter + "\n");
 
                    //Removing that character from the string
                    Statement = Statement.Replace(Statement[0].ToString(), string.Empty);
                }
            }
            Console.ReadLine();
        }
    }
}












Run the application and enter the string value then press "Enter" key then see the output.


Factorial of a Number

Recently Viewed