# java question

• 03-23-2011, 07:18 AM
joe98
java question
Write a method that takes 2 integer parameters, and prints to the screen the factorial of
the smallest of the two given parameter values subtract the larger integer value. Your
method should be called from the main method, which passes some specific parameter
values.
For example, if the two parameter values given to your method is 6 and 4, then the
smallest of the parameters is 4, and the biggest is 6, so your method should calculate and
print to the screen the value of 4! – 6, which is equal to 18

Heres my solution so far.ny1 have any clues thnx

class ....
{
public static void main(String[] args)
{
int a=2;
int b=1;
calculateFactorial(a,b);
}
public static void calculateFactorial(int a, int b)
{
if (a<b)
for (int i=0; i<a; a=a-1)
}
}
• 03-23-2011, 07:25 AM
JosAH
Quote:

Originally Posted by joe98
Write a method that takes 2 integer parameters, and prints to the screen the factorial of
the smallest of the two given parameter values subtract the larger integer value. Your
method should be called from the main method, which passes some specific parameter
values.
For example, if the two parameter values given to your method is 6 and 4, then the
smallest of the parameters is 4, and the biggest is 6, so your method should calculate and
print to the screen the value of 4! – 6, which is equal to 18

Heres my solution so far.ny1 have any clues thnx

class ....
{
public static void main(String[] args)
{
int a=2;
int b=1;
calculateFactorial(a,b);
}
public static void calculateFactorial(int a, int b)
{
if (a<b)
for (int i=0; i<a; a=a-1)
}
}

What does your program do if a >= b? No factorial will be calculated at all. In the Math class there's are methods that find the minimum and maximum of two values. Before you start your calculations in your method do this:

Code:

int min= Math.min(a, b);
int max= Math.max(a, b);

... after that you calculate the factorial of variable 'min' and subtract the value of 'max' from the result.

kind regards,

Jos
• 03-23-2011, 07:32 AM
sanjeevtarar
You need to subtract value after completing the for loop

check the below code

Code:

public class TestFactorial
{
public static void main(String[] args)
{
int a=4;
int b=6;
calculateFactorial(a,b);
}

public static void calculateFactorial(int a, int b)
{
if (a<=b){
for (int i=1; i<=a; a=a-1)
{
}

}else if (b<a)
{
for (int i=1; i<=b; b=b-1)
{
}

}

}
}

• 03-23-2011, 07:46 AM
joe98
but it doesnt work..i need to get 4!-6=18....ie (4x3x2x1)-6=18 also for a negative result it should not be printed
• 03-23-2011, 07:53 AM
sanjeevtarar
I think it is working

what is error or result when you run the program
• 03-23-2011, 08:03 AM
joe98
its working but,
when i put int a=3 b=3 i get a negative answer, i dont want a negative answer just 0..
ie. If both parameters have the same value, or if the smaller parameter value is negative, then
nothing should be printed to the screen. You should write this (and more, such as
describing what the method does in general) as a method header comment, above your
method.
• 03-23-2011, 08:06 AM
JosAH
Quote:

Originally Posted by sanjeevtarar
Code:

for (int i=1; i<=a; a=a-1)
{
}

[ ... ]
for (int i=1; i<=b; b=b-1)
{
}

Change those two multiplication lines to:

Code:

kind regards,

Jos
• 03-23-2011, 08:09 AM
JosAH
Quote:

Originally Posted by joe98
its working but,
when i put int a=3 b=3 i get a negative answer, i dont want a negative answer just 0..
ie. If both parameters have the same value, or if the smaller parameter value is negative, then
nothing should be printed to the screen. You should write this (and more, such as
describing what the method does in general) as a method header comment, above your
method.

You should (and can) write those simple tests yourself; also it's you who has to write the method descriptions; it's your homework, not ours.

kind regards,

Jos