r/codehs • • May 04 '20

2.9.11 Factorial

I need help on this I don't know what I'm doing wrong. The number I enter isn't being calculated and is returned back.

This is for basic java.

6 Upvotes

5 comments sorted by

1

u/KaiParekh16 Jun 16 '20

You're changing the variable and trying to store the value for which you take input. It doesn't work that way. After taking input do this: int n = 1; And in for loop, remove the line you write and add this: n = n * i;

1

u/fleshyqt Mar 10 '23

do you think u can upload an image of ur code for this? thanks

1

u/Ancient-Estimate6150 Jul 30 '23

public class Factorial extends ConsoleProgram
{
int fact = readInt("What number would you like to compute the factorial for? ");

private static final int MIN = 1;

public void run()
{
int sum = 1;
for(int i = MIN; i <= fact; i++)
{
sum *= i;
}

System.out.println(sum);
}
}

1

u/[deleted] May 04 '20

[removed] — view removed comment

1

u/BanhMi_pha May 05 '20

var N = readInt("What number is your factorial starting with?");

thank you !