Results 1 to 8 of 8
Thread: java question
 03232011, 07:18 AM #1Member
 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)
{
int answer=1;
if (a<b)
for (int i=0; i<a; a=a1)
{ answer = answer*a;
answer= answerb;
}
}
 03232011, 07:25 AM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,188
 Blog Entries
 7
 Rep Power
 24
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);
kind regards,
JosThe only person who got everything done by Friday was Robinson Crusoe.
 03232011, 07:32 AM #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) { int answer=1; if (a<=b){ for (int i=1; i<=a; a=a1) { answer = answer*a; } answer = answer  b; }else if (b<a) { for (int i=1; i<=b; b=b1) { answer = answer*b; } answer = answer  a; } System.out.println(answer); } }
sanjeev,संजीव
 03232011, 07:46 AM #4Member
 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
 03232011, 07:53 AM #5
I think it is working
what is error or result when you run the programsanjeev,संजीव
 03232011, 08:03 AM #6Member
 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.
 03232011, 08:06 AM #7
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,188
 Blog Entries
 7
 Rep Power
 24
 03232011, 08:09 AM #8
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,188
 Blog Entries
 7
 Rep Power
 24
Similar Threads

sorry for this question  which is best ? .NET c# or java ?
By funkygarzon in forum Forum LobbyReplies: 33Last Post: 01212011, 04:07 PM 
Need help with java question
By ccie007 in forum New To JavaReplies: 23Last Post: 05182010, 06:32 PM 
Java Question :D
By thisisIT in forum New To JavaReplies: 6Last Post: 03122010, 05:04 PM 
question about java rmi
By hakimade in forum Advanced JavaReplies: 1Last Post: 07012009, 07:15 AM 
Java Question
By Jay1.1 in forum New To JavaReplies: 11Last Post: 05012008, 04:04 PM
Bookmarks