1. Member Join Date
Mar 2011
Posts
16
Rep Power
0

## 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)
}
}  Reply With Quote

2. ##  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:

Java 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  Reply With Quote

3. ## You need to subtract value after completing the for loop

check the below code

Java 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)
{
}

}

}
}```  Reply With Quote

4. Member Join Date
Mar 2011
Posts
16
Rep Power
0

## 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  Reply With Quote

5. ## I think it is working

what is error or result when you run the program  Reply With Quote

6. Member Join Date
Mar 2011
Posts
16
Rep Power
0

## 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.  Reply With Quote

7. ##  Originally Posted by sanjeevtarar Java 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:

Java Code:
`answer= answer*i;`
kind regards,

Jos  Reply With Quote

8. ##  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  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•