Assignment: Module 03- Part 01
![]() |
| Assignment: Module 03- Part 01 |
10. Write a class FibonacciExample to accept a number and find its Fibonacci series value using do .. while
Input:
Enter the no n 4
Output :
0
1
1
2
3
class FibonacciExample {
void fibonacci(int n) {
int a = 0, b = 1, c = 0;
System.out.print(a + " " + b);
do {
c = a + b;
System.out.print(" " + c);
a = b;
b = c;
n--;
} while (n > 0);
}
}
public class Main {
public static void main(String[] args) {
FibonacciExample obj = new FibonacciExample();
Scanner sc = new Scanner(System.in);
System.out.print("Enter the no n: ");
int n = sc.nextInt();
obj.fibonacci(n);
}
} In the above code, we create a class 'FibonacciExample' with a method 'fibonacci' which accepts an integer and prints the Fibonacci series of the number passed. In the main method, we create an object of the 'FibonacciExample' class, take input from the user for the number, then call the fibonacci method with the number passed as an argument.
The output of the above code when the input is 4 is "0 1 1 2 3"
The Fibonacci sequence is a series of numbers in which each number is the sum of the two preceding ones, usually starting with 0 and 1.
In this example, I used a do-while loop to generate the Fibonacci sequence. The do-while loop runs at least once and then checks the condition, which means that it will always execute the loop body at least once.
To make the explanation more complete, we can also include how the sum of the Fibonacci series is calculated along with the sequence generation. Adding sum-related logic helps in understanding how values accumulate during iteration and gives a more practical use case of the program.
After generating the Fibonacci sequence using the do-while loop, we can introduce a variable (for example, sum) that keeps track of the total of all generated numbers. Initially, this variable is set to 0. As each Fibonacci number is produced inside the loop, we simply add it to the sum variable. This way, by the time the loop ends, the sum will contain the total of all Fibonacci numbers generated up to the given input.
For example, if the input is 4, the generated Fibonacci sequence is:
0 1 1 2 3
Now, if we calculate the sum:
0 + 1 + 1 + 2 + 3 = 7
So, along with printing the sequence, the program can also display:
Sum of Fibonacci series: 7
To implement this, inside the fibonacci method, declare:int sum = 0;
Then, inside the do-while loop, after printing the current number, add:sum = sum + currentNumber;
At the end of the loop (after it terminates), print the sum:System.out.println("Sum of Fibonacci series: " + sum);
This enhancement improves the usefulness of the program by not only showing the sequence but also providing a cumulative result.
Additionally, this demonstrates an important programming concept: performing multiple operations within a loop. While one part of the loop handles generating and displaying the Fibonacci numbers, another part simultaneously calculates the sum. This avoids the need for a separate loop, making the program more efficient.
Another key point is that using a do-while loop ensures the sequence starts correctly even for small inputs like 0 or 1. Since the loop executes at least once, it guarantees that the base values of the Fibonacci sequence are always printed.
In summary, by adding sum calculation:
You enhance the program’s functionality
You demonstrate efficient looping
You provide better insight into how values accumulate in iterative processes
This makes the FibonacciExample class more practical and educational, especially for beginners learning loops, variables, and basic algorithm design.

.jpg)