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);
	    }
         }
    }
}

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{
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.

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
commented: Please explain something. +0

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);
	    }
}
commented: Throwing exception on main, bad manners. Handle inside code! -4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.