Write a program in Java to calculate and print average of the first ten terms of Fibonacci series using for loop
Answers
Answered by
4
public class Fibonacci {
public static void main(String[] args) {
int a = -1, b = 1, c = 0;
float sum = 0.0f;
for(int i = 0; i < 10; i++){
a = b;
b = c;
c = a + b;
sum += c;
}
System.out.println("Average of first ten terms of Fibonacci series : " + (sum/10));
}
}
Answered by
0
class Main
{
public static void main(String args[])
{
int first_num = 0;
int second_num = 1;
int i = 0;
double avg = 0;
for(i=1; i<=5; i++) //Taken 5 Because it prints two fibonacci numbers at once
{
System.out.println(first_num +"\t");
System.out.println(second_num);
avg = (first_num+second_num)/10.0;
first_num = first_num+second_num;
second_num = first_num+second_num;
}
System.out.print("\nAverge is "+avg);
}}
Similar questions