Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Practicing For Loop | Loops
C# Basics

Practicing For LoopPracticing For Loop

A factorial of a number is the product of all the numbers from 1 up till that number. For-example the factorial of 5 is the product of all numbers from 1 to 5 (1 x 2 x 3 x 4 x 5) which gives 120.

Did You Know?

The mathematical notation for a factorial is x! where x is any integer. Hence 3! is 6, 4! is 24, 5! is 120 and so on. The factorial of 0 is 1 by definition therefore 0! is 1.

cs

main.cs

1. Initialize the loop variable i with a value of 2 and continue the loop till i is equal to x, so the loop condition will be i <= x.

using System;

namespace ConsoleApp
{
    internal class Program
    {
        static void Main(string[] args)
        {
			int x = 5;
			int result = 1;

			if(x == 0)
			{
			    result = 0;
			} 
			else
			{
			    for (int i = 2; i <= x; i++)
			    {
			        result *= i;
			    }
			}

			Console.WriteLine($"Factorial of {x} is {result}");

        }
    }
}
      

Все було зрозуміло?

Секція 4. Розділ 2
course content

Зміст курсу

C# Basics

Practicing For LoopPracticing For Loop

A factorial of a number is the product of all the numbers from 1 up till that number. For-example the factorial of 5 is the product of all numbers from 1 to 5 (1 x 2 x 3 x 4 x 5) which gives 120.

Did You Know?

The mathematical notation for a factorial is x! where x is any integer. Hence 3! is 6, 4! is 24, 5! is 120 and so on. The factorial of 0 is 1 by definition therefore 0! is 1.

cs

main.cs

1. Initialize the loop variable i with a value of 2 and continue the loop till i is equal to x, so the loop condition will be i <= x.

using System;

namespace ConsoleApp
{
    internal class Program
    {
        static void Main(string[] args)
        {
			int x = 5;
			int result = 1;

			if(x == 0)
			{
			    result = 0;
			} 
			else
			{
			    for (int i = 2; i <= x; i++)
			    {
			        result *= i;
			    }
			}

			Console.WriteLine($"Factorial of {x} is {result}");

        }
    }
}
      

Все було зрозуміло?

Секція 4. Розділ 2
some-alt