I want the to calculate the value of user input by multiplying the number by it the increment i.e. if input is 5 then calculation should be 5*1*2*3*4=120, I have tried but so far not successful any help will be appreciated. The code is below:

``````import java.util.Scanner;
public class Math
{
public static void main(String[] args)
{
Scanner input;
input = new Scanner(System.in);
op1 = input.nextInt();
{
for (int i = op1; i >= 1; i--)
{
Answer =  (op1 * i (op1-1));
}
}
}
}``````

## All 6 Replies

Is this "5*1*2*3*4" a requirement? I think doing 5*4*3*2*1 is much easier. Or better yet, do 5*4*3*2. The result is exactly the same and you should see how a loop works better. :)

You need to modify your loop and its content inside. By the way, you know that you need to accumulate the multiplied values, right? It is a factorial value... So you may write it down to see how it is multiplied...

``````/*
op1 = 5
the sum is 5
now multiply sum with a value lower by 1 which is 4
sum = sum * 4
the sum is 20
now multiply sum with a value lower by 1 which is 3
sum = sum * 3
the sum is 60
now multiply sum with a value lower by 1 which is 2
sum = sum * 2
the sum is 120
now multiply sum with a value lower by 1 which is 1
sum = sum * 1
the sum is 120
*/``````

Now you see how it goes in each step. Hope you can write a loop to represent the way it goes that way.

Try this....

``````answer=1;//For initializing
do{
opi--;
}while(opi>1);

This seem to me much easy. If it does not work , then ask again.
Thank you.

Try this....

``````answer=1;//For initializing
do{
opi--;
}while(opi>1);

This seem to me much easy. If it does not work , then ask again.
Thank you.

Oh! sorry. I will correct it ?

``````for (int i = op1; i > 1; i--)
{

ans=op1*(i - 1);
op1=ans;
System.out.println(ans);
}``````
commented: op1=ans obsolete can be done much better. I wouldn't be pleased to see 20 or more output because you put print method inside loop. Bad! -4

I want the to calculate the value of user input by multiplying the number by it the increment i.e. if input is 5 then calculation should be 5*1*2*3*4=120, I have tried but so far not successful any help will be appreciated.

The code is below:

``````import java.io.*;
public class Math
{
public static void main(String[] args)throws IOException
{