COMP2243 - Programming and Problem Solving

Syllabus Software Installation Assignments Tests
Lectures and Labs
**Back to Full Menu**

Repetitions/Loops WhileLoop.java LoopWithCounter.java LoopWithSentinel.java LoopWithFlag.java SimpleInterest.java Factorial.java Fibnonacci.java PrimeNumber.java GCD.java PopulationGrowth.java ForLoop.java DoWhileLoop.java FileIO_Tickets.java FileIO_Salary.java NestedLoop.java



Factorial Program


Description

This program inputs a positive integer n and then calculates n!.

n! = n * (n - 1)* (n - 2) * ... * 1

Associated Concepts:

while loop

long integer data type

Algorithm:

Declare variables and initialize the long integer result (64 bits) to 1

Input an integer n from the keyboard

Set the control variable i to n

Use a while to check the control variable i
(as long as i is greater than or equal to 1)

    Multiply result by i and update result to the new value

    Decrement i by 1

Print the result after the loop is ended

Source Code



Sample Run



Video