# can anyone think of a more efficient way of doing this...

• June 16th, 2012, 07:41 PM
mia_tech
can anyone think of a more efficient way of doing this...
guys, this code calculate the amount of classes that a student needs to take lets say for 17 credits. It will be 5 three credits classes and 2 one credit classes, but I think the loop is kind of unnecessary. could anyone post a more efficient way? thanks

Code :

```public static void numOfClasses(int credits) { int class3 = 3, class1 = 1, numOfClass;   do { if(credits >= 3) { numOfClass = credits / class3; credits -= (numOfClass * class3); System.out.println("3 credits classes: "+numOfClass); } else { numOfClass = credits / class1; credits -= (numOfClass * class1); System.out.println("1 credit classes: "+numOfClass); } }while(credits != 0); }```
• June 16th, 2012, 08:16 PM
Norm
Re: can anyone think of a more efficient way of doing this...
Look at using the /, - and % operators.
• June 19th, 2012, 10:08 AM
mia_tech
Re: can anyone think of a more efficient way of doing this...
Quote:

Originally Posted by Norm
Look at using the /, - and % operators.

like this? or could be better?

Code :

```public static void numOfClasses(int totalCred) { int numOfClass = 0;   if(totalCred >= 3) { numOfClass = totalCred / 3; totalCred = totalCred % 3; System.out.println("3 credit classes: "+numOfClass); } if(totalCred >= 1) { numOfClass = totalCred; System.out.println("1 credit classes: "+numOfClass); } }```
• June 19th, 2012, 10:12 AM
Norm
Re: can anyone think of a more efficient way of doing this...
Does that code give the correct results?
Do you intend that both if statements be true if the first one is true?
• June 19th, 2012, 10:23 AM
mia_tech
Re: can anyone think of a more efficient way of doing this...
Quote:

Originally Posted by Norm
Does that code give the correct results?
Do you intend that both if statements be true if the first one is true?

yes, it gives correct result... now both statement are not always true, depending on the total of credits "totalCred"... have you try it?
• June 19th, 2012, 10:32 AM
Norm
Re: can anyone think of a more efficient way of doing this...
No, I haven't tried it. If you get the correct results that should be the test.