I-78 Computer Fundamentals
Step 3: Read n
Step 4: While i <= n
Read num
Add num to sum
Increment i by 1
End while
Step 5: Calculate Average = Sum / n
Step 6: Display Average
Step 7: Stop
26. Write an algorithm to generate a Fibonacci series.
Ans:
Step 1: Start
Step 2: Initialize sum=0, A=0, B=1, i=3
Step 3: Read the number of terms you want in the Fibonacci
series, say n
Step 4: Display A, B
Step 5: While i <= n
Add A and B and store value in sum
Display sum
Assign the value of B to A, that is, A=B
Assign the value of sum to B, that is, B=sum
Increment i by 1
End while
Step 6: Stop
27. Write an algorithm to test whether the given number is prime or not.
Ans:
Step 1: Start
Step 2: Read num
Step 3: Initialize div = 2
Step 4: Calculate half = num/2
Step 5: While (div <= half)
Divide num by div and store remainder in rem
If rem = 0 then
Display given number is not prime
Go to Step 7
End if
Increment div by 1
End while
Step 6: Display given number is prime
Step 7: Stop
M04_ITL-ESL4791_01_SE_C04.indd 78 12/22/2012 4:53:50 PM