+ 1
Factorial fun JavaScript
The case: A number's factorial is the product of all positive integers less than or equal to the number. Write a program that takes a number as input and outputs its factorial to the console. Sample Input 5 Sample Output 120 Explanation 5*4*3*2*1 = 120 My code: function main() { var number = parseInt(readLine(), 10) var factorial = 1; // controlle van sololearn for (; number >0; number--) { number*factorial } console.log (number); } The out put is 0 But why? What do I need to change to get the right number to show?
13 Respostas
+ 13
U make some mistakes.
U find the factorial but you didn't store in a variable. And u should print the factorial instead of number.
Corrected code:
var number = parseInt(prompt());
var factorial = 1;
for (; number >0; number--) {
factorial=number*factorial;
}
console.log (factorial);
+ 6
number*factorial does nothing because the result is not saved in a variable. In addition you want to change the variable number which is used in the loop. This make no sense here. The solution looks i.e. like as follows:
for (; number>0; number--) {
factorial *= number;
}
console.log (factorial);
+ 6
function main() {
var number = parseInt(readLine(), 10)
var factorial = 1;
// Your code here
for (i = 1; i <= number; number--) {
factorial *= number;
}
console.log (factorial);
}
+ 3
function main() {
var number = parseInt(readLine(), 10)
var factorial = 1;
for (; number >0; number--) {
factorial=number*factorial;
}
console.log (factorial);
}
+ 2
Help me!!!
Factorial
The factorial of a number N is equal to 1 * 2 * 3 * ... * N
For example, the factorial of 5 is 1* 2 * 3 * 4 * 5 = 120.
Create a program that takes a number from input and output the factorial of that number.
Use a for loop to make the calculation, and start the loop from the number 1.
//IS JAVA
+ 1
using System;
public class Program
{
static void Main(string[] args)
{
int num = Convert.ToInt32(Console.ReadLine());
int result =1;
for (int i = 1; i <= num; i++)
{
result *= i;
}
Console.WriteLine( result);
}
}
0
I feel like I hacked CIA with this for loop setup:
function main() {
var number = parseInt(readLine(), 10)
var factorial = 1;
// Your code here
for (var keeper=number;--number>0;){
keeper*=number;
}
console.log(keeper)
}
0
I'd like this variant
function main() {
var number = parseInt(readLine(), 10)
var factorial = 1;
// Your code here
for ( let x = 1; x = number; number--) {
factorial*=number;
}
console.log (factorial);
}
0
another possible solutions:
function main() {
var number = parseInt(readLine(), 10)
var factorial = 1;
// Your code here
for (var i=1;i<= number; i++) {
factorial = factorial*i;
}
console.log (factorial)
}
0
GUNASEKARA H.G.D.A.P. (CST18022) Hi Guna, would you please explain to me how you got this result?
I lost it after « number â) ». The factorial part is messing with my brain. How factorial came from 1 to whatever it became after this ?
Please help a lost soul lol
0
What da heel is Factoral Numbers????????????
0
function main() {
var number = parseInt(readLine(), 10)
var factorial = 1;
for (; number >0; number--) {
factorial=number*factorial;
}
console.log (factorial);
}
Good Luck
0
Hi guys,
I don't get why you are putting "number- -" on the code.
I did the exercise with the following code and actually got the right answer.
on the "number --" part I put "i ++".
Can someone please help me with that.
function main() {
var number = parseInt(readLine(), 10)
var factorial = 1;
var i = 1;
for(i ; i<= number; i++ ) {
factorial*=i;
}
console.log(factorial)
}