info@meritcampus.com    +91-85006-22255
...
`

Calculate compound interest
What will be the output of the following program?
`public class Interest {    public static void main(String args[]) {        double a, itr, p, r;        String array[] = {"100", "2", "3"};        int y;        p = Float.valueOf(array[0]).floatValue();        r = Float.valueOf(array[1]).floatValue();        y = Integer.parseInt(array[2]);        a = p * Math.pow((1 + 1 / r), y);        itr = a - p;        System.out.println("Compound Interest: " + itr);    }}`

 A. Compound Interest: 50.0 B. Compound Interest: 77.77777 C. Compound Interest: 125.0 D. Compound Interest: 237.5 E. Compilation Error or Runtime Error
Topic:

### User comments below.All of them might not be correct.

The answer is obviously D: since p=100.0,r=2.0,y=3 and a=100*(3/2.0)^3 i.e 100*27/8 that gives 337.5 now itr=337.5-100 gives itr = 237.5 thus prints Compound Intrest: 237.5 hence D is the output it gives !

Posted by Anchal Shetty    2013-05-03 16:44:37

the answer is d:here we are taking string values into arrays..those values are converted into float and int datatypes respectively...in math.pow((1+1/r),y) 1/r is 1/2.0 whose value is 0.5(if r would be an integer then the value would have been zero...), next 1+1/r is 1.5 and (1.5)^3 is 3.375 and next 3.375 *100=337.5 and 337.5-100=237.5

Posted by Ratan Dhoka    2013-05-03 16:51:03

One more mistake today. Actually Kaushik Yashashwy did not win any recharges this week, so he is eligible for winning today's daily dose.

Posted by Merit Campus    2013-05-03 16:59:26

So we will have two winners for today's daily dose as well. They are Kaushik Yashashwy and Anchal Shetty. Winners, please send us your mobile numbers and the operator details and we will process the recharge on Monday after considering the new likes.

Posted by Merit Campus    2013-05-03 17:00:52

the answer is but 3/2.0 would never come into picture as / has highest preceedence 1/r would be calculated first...it gives 0.5 and later 1+0.5 gives 1.5 but 3/2.0 never comes... the keypoint here is that r should be a float value, if r is not float and if r is an integer the final output would come as 0 bcoz 1+1/r gets simplified as 1..(1/r(int) never gives a fractional value.)

Posted by Samhitha Reddy    2013-05-03 17:01:18

Thanks a lot :D. Got the rc :)

Posted by Kaushik Yasaswy    2013-05-10 05:52:33

Thankq MC,got RC !

Posted by Anchal Shetty    2013-05-15 16:26:58