Pages

Write A PROGRAMME TO FIND THE FECTORIAL NUM OF THE GIVEN INPUT.(USING FOR LOOP)

Solution:

Pseudo Code:

  1. Initialize variable "num" to 1
  2. Initialize  variable "factorial" to 1
  3. Print message on console to ask user for a number to know its factorial
  4. Read user's input and convert to Integer and store that input into variable "num" that is necessary for factorial calculation 
  5. Start While loop till variable "num" value is grater then 1 (because initial value would be the user's input number)
  6. In each while loop  add the product of   "factorial" and "number" into "factorial"
  7. In each while after adding the factorial value , reduce the variable "num" by 1 so that once variable "num" value would reach to 1 then while loop will end. 
  8. Print the variable "factorial" on console to show the calculated factorial..

Code:

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

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            int num, factorial = 1;
            Console.WriteLine("please enter the num  to whom u want to know the factorial");
            num = Convert.ToInt32(Console.ReadLine());
            while (num > 1)
            {
                factorial = factorial * num;

                num = num - 1;
            }

            Console.WriteLine("the factorial of the num is {0}", factorial);
        }

    }
}


OUTPUT:


No comments:

Post a Comment