Menu
Topics Index
...
`

HarmonicSeries


If you need explanation Read this topic

If you need Answer Take test on this topic

HarmonicSeries
What will be the output of the following program? (Approximately)
public class HarmonicSeries
{
    public static void main(String args[])
    {
        int num = 5;
        double result = 0.0;
        while (num > 0)
        {
            result = result + (double) 1 / num;
            num--;
        }
        System.out.println(result);
    }
}

A. 2.28
B. 1.8
C. 0
D. 1.0
E. Compilation Error or Runtime Error
Topic: while Loop In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

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

Ans is A.
result = 0.0
for 5 > 0 => result = 0.0 + 0.2 = 0.2
for 4 > 0 => result = 0.2 + 0.25 = 0.45
for 3 > 0 => result = 0.45 + 0.33 = 0.78
for 2 > 0 => result = 0.78 + 0.5 = 1.28
for 1 > 0 => result = 1.28 + 1 = 2.28

Posted by Gopinath Manchikanti    2014-09-12 08:36:09


ans a.
1/5+1/4+1/3+1/2+1=result get printd

Posted by Anand Kumar    2014-09-12 08:38:22


Answer is a
There is perfect logic and there is casting in the loop which make it run

Posted by Yash Mittal    2014-09-12 08:42:34


A ,,'

Posted by Raju VarshnEy    2014-09-12 09:24:40


A...
due to looping int=5 value got decreamented..nd their is regular addition in the 'result'

Posted by Rohit Baranwal    2014-09-12 10:03:26


a

Posted by Ankur Verma    2014-09-12 10:32:51


a

Posted by Pranavi Prakash    2014-09-12 11:35:29


Congratulations Anand Kumar. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-09-15 04:49:41


© meritcampus 2019

All Rights Reserved.

Open In App