C Program : Let us understand the program shared on 19/07/2023

in c-programming •  last year 

Welcome back,
In today's blog we are going to understand the program shared yesterday. It was about fibonacci Series so let us get into the explanation about it.

Here is the program itself,

Screenshot 2023-07-19 081512.png

Screenshot 2023-07-19 081524.png

Screenshot 2023-07-19 081538.png

You can copy the program from below :

#include <stdio.h>

// Function to generate Fibonacci sequence iteratively
void fibonacciIterative(int n) {
int prev = 0, current = 1, next;
printf("Fibonacci Sequence (Iterative):\n");
printf("%d ", prev);
for (int i = 1; i <= n; i++) {
printf("%d ", current);
next = prev + current;
prev = current;
current = next;
}
printf("\n");
}

// Function to generate Fibonacci sequence recursively
int fibonacciRecursive(int n) {
if (n <= 1)
return n;
return fibonacciRecursive(n - 1) + fibonacciRecursive(n - 2);
}

int main() {
int num;
printf("Enter the number of terms to generate in the Fibonacci sequence: ");
scanf("%d", &num);

// Generate Fibonacci sequence iteratively
fibonacciIterative(num);

// Generate Fibonacci sequence recursively
printf("Fibonacci Sequence (Recursive):\n");
for (int i = 0; i <= num; i++) {
printf("%d ", fibonacciRecursive(i));
}
printf("\n");

return 0;
}

The above program starts by including the necessary header file, stdio.h, which provides input and output functionality.

Next, the program defines a function called fibonacciIterative which takes an integer parameter n. This function generates the Fibonacci sequence iteratively and prints it.

Inside the fibonacciIterative function, three variables are declared: prev to store the previous term, current to store the current term, and next to calculate the next term in the sequence.

The program then prompts the user to enter the number of terms they want to generate in the Fibonacci sequence.

In the fibonacciIterative function, the first term (prev) is initialized as 0, the second term (current) as 1, and the next term (next) is calculated as the sum of the previous two terms.

The program uses a loop to iterate from 1 to n and calculates the Fibonacci sequence by updating the values of prev, current, and next in each iteration. It also prints each term of the sequence.

After the loop ends, the program prints a newline character to move to the next line.

The program defines another function called fibonacciRecursive which takes an integer parameter n. This function generates the Fibonacci sequence recursively.

Inside the fibonacciRecursive function, the base case is checked. If n is less than or equal to 1, the function simply returns n.

If the base case is not met, the function recursively calls itself with arguments n - 1 and n - 2, and returns the sum of the two recursive function calls.

In the main function, a variable num is declared to store the user's input for the number of terms in the Fibonacci sequence.

The user is prompted to enter the number of terms they want to generate.

The fibonacciIterative function is called with the num as an argument to generate and print the Fibonacci sequence iteratively.

The program then prints a message indicating that the following sequence is generated using the recursive method.

A loop runs from 0 to num (inclusive), and in each iteration, the fibonacciRecursive function is called with the current iteration number as an argument. The returned value is printed, which represents each term of the Fibonacci sequence.

Finally, the program returns 0 to indicate successful execution.

If you also want to learn basics and start your C programming journey then find a teacher today for free on Youtube.

I learnt alot from a teacher who teaches c in hindi and if you are finding someone who teaches in hindi then this person will definitely help you.

Source

I am happy to call myself a trader and small programmer at the same time now.

Happy trading and keep learning what you love.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE BLURT!