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)
    {
        	 int op1, op2, Answer =0;
        	 Scanner input;
        	 input = new Scanner(System.in);
        	 System.out.print("\nPlease enter the number: ");
        	 op1 = input.nextInt();
         {
             for (int i = op1; i >= 1; i--)
             {
	    Answer =  (op1 * i (op1-1));
	    System.out.println(Answer);
	    }
         }
    }
}

Edited 5 Years Ago by maverick420: n/a

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.

Edited 5 Years Ago by Taywin: n/a

Try this....

answer=1;//For initializing
do{
answer=answer*opi;
opi--;
}while(opi>1);
 System.out.println(Answer);

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

Try this....

answer=1;//For initializing
do{
answer=answer*opi;
opi--;
}while(opi>1);
 System.out.println(Answer);

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

Still your solution is incoherent.

Edited 5 Years Ago by peter_budo: n/a

for (int i = op1; i > 1; i--)
             {
	   
            ans=op1*(i - 1);
            op1=ans;
	    System.out.println(ans);
	    }

Edited 5 Years Ago by peter_budo: Keep It Clear - Do use code tags when posting any code

Comments
Please explain something.
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!

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
    {
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the number");
int num=Integer.parseInt(br.readLine());
for(  int i=sum=1;i<=num;sum=sum*i,i++ );
System.out.println("Sum="+sum);
	    }
}

Edited 5 Years Ago by peter_budo: Keep It Clear - Do use code tags when posting any code

Comments
Throwing exception on main, bad manners. Handle inside code!
This article has been dead for over six months. Start a new discussion instead.