In: Computer Science
i have this problem write an application that evaluates the
factorials of the integeres from 1 to 5 . i have this
!
control.WriteLine( "n\tn!;n");
for (in number=1; number <=5; number ++);
{
int factorail=1:
for (int 1=1; i<=number;1++);
factorial *=1:
Console.Writeline("{0}\t{1}".number,factorial);
output
n n!
1 1
2 2
3 6
4 24
5 120
I understand how the first row is printed.
the first 1 in because the intfactorial is 1
the #2 if printed becasue in number =1; number<=5; number ++ =2
then it runs again number=2; number <=5; number ++ ) number is 3
then it runs again number=3; number <=5; number ++) number is 4
then it runs again number=4); njmber <=5; number ++) number is 5 and then ends because it hits <=5 and becomes true
so it goes to the next
for (int i=1; i<=number; i++
factorial *=1;
not sure how this works or comes up the mulipliers
control.WriteLine( "n\tn!;n");
for (in number=1; number <=5; number ++);
{
int factorail=1:
for (int 1=1; i<=number;1++);
factorial *=1:
Console.Writeline("{0}\t{1}".number,factorial);
outer loop runs for number =1 ro number =5
inner loop runs for i=1 to i=number
now lets trace
number=1
in inner loop
i=1 factorial=1
then loop breaks
hence 1 1 is printed //here number is 1 and factorial is 1
number=2
in inner loop
i=1 factorial =1
i=2 factorial =2 //2x1
then loop breaks
hence 2 2 is printed //here number is 2 and factorial is 2
number=3
in inner loop
i=1 factorial =1
i=2 factorial =2 //2x1
i=3 factorial = 6 //3x2x1
then loop breaks
hence 3 6 is printed //here number is 3 and factorial is 6
number=4
in inner loop
i=1 factorial =1
i=2 factorial =2 //2x1
i=3 factorial = 6 //3x2x1
i=4 factorial = 24 //4x3x2x1
then loop breaks
hence 4 24 is printed //here number is 4 and factorial is 24
number=5
in inner loop
i=1 factorial =1
i=2 factorial =2 //2x1
i=3 factorial = 6 //3x2x1
i=4 factorial = 24 //4x3x2x1
i=5 factorial = 120 //5x4x3x2x1
then loop breaks
hence 5 120 is printed //here number is 5 and factorial is 120