What is the output of the following flowchart if n= 5? Explain with reason
- برمجة
- خوارزميات
- 2021-04-11
- daafoor
الأجوبة
Output:
2.283333333333333
Explanation:
First when the program starts it askes to enter a number, in this case the entered number n is 5, then initializing variable sum =1.0 and uses variable i as counter from 2.0 to n.
Then the program will check is i <=n when it yes it will add 1.0/i value to the sum and when it is no it will display the sum.
So at first for i=2.0 it will check is 2.0 <= 5.0 ? it is yes, so 1.0/2.0 will be added to sum which was 1 so the new sum= 1+1/2 =1.5 ,then i will be incremented by 1 so new become i =3.
Then again check is 3.0 <=5.0 ? it is yes, so 1.0/3.0 will be added to sum which was equal to 1.5 so the new sum= 1.5+1/3 =1.8333, then i will be incremented by 1 so new become i =4.
Then again check is 4.0 <=5.0 ? it is yes, so 1.0/4.0 will be added to sum which was equal to 1.8333 so the new sum= 1.8333+1/4 =2.0833, then i will be incremented by 1 so new become i =5. Then again check is 5.0 <=5.0 ? it is yes, so 1.0/5.0 will be added to sum which was 2.0833 so the new sum= 2.0833+1/5 =2.2833 ,then i will be incremented by 1 so new become i =6.
Then check is 6 <=5? it is no, then the program will display the sum and stops.
أسئلة مشابهة
القوائم الدراسية التي ينتمي لها السؤال
معلومات ذات صلة