Small Basic 2. Write a program to display first 10 numbers in the series 8, 15, 23, - The program show also display the sum of the numbers displayed.
Answers
Answer:
In C Language using For Loop
#include<stdio.h>
int main(){
int a = 8, b = 15,c,i,sum=0;
printf("%d %d",a,b);
for(i=1;i<=10-2;i++){
c = a+b;
printf(" %d ",c);
a = b;
b = c;
sum+=c;
}
printf("\nSum is %d",sum+8+15);
return 0;
}
Explanation:
Suppose the first number 8 be A.
Similarly, let the second number 15 be B.
This series is an example of the Fibonacci series in which the third number C is equal to the sum of A and B.
We print the first two numbers in the series before starting the loop so we put i<=10-2 or i<=8.
Furthermore,
8 15 23 38 61
A B C = A+B
Here, initially, A = 8 and B = 15, and C is the sum of A and B.
8 15 23 38 61
A B C=A+B
Here, A is equal to the previous value of B and B is equal to the previous value of C and we have a new C whose result will be A+B.
8 15 23 38 61
A B C=A+B
Here, A is equal to the previous B and B is equal to the previous C and we have a new C whose result is A+B.
And so on...
Also, we get the sum using,
Sum += C which is equivalent to Sum = Sum + C where initially Sum = 0 .