Often times we use arithmetic operations, relational operators, logical operators and assignment operators together in a instruction / statement. We must know the precedence / priority of the operator so that we can write proper code.
Note: Write down above table of operator priority on a piece of paper and have it handy while writing the code. You’ll get used to it after some time – until then, keep revisiting the hierarchy of operators.
In this video tutorial we show the differences and working of post-decrement and pre-decrement operators.
Note: In pre-decrement, first the value of the variable is decremented after that the assignment or other operations are carried. In post-decrement, first assignment or other operations occur, after that the value of the variable is decremented.
Note: a- -; – -a; a = a – 1; a -= 1;
are all same if used independently.
Post-decrement and Pre-decrement Operator: C Program
In this video tutorial we show the differences and working of post-increment and pre-increment operators.
Note: In pre-increment, first the value of the variable is incremented after that the assignment or other operations are carried. In post-increment, first assignment or other operations occur, after that the value of the variable is incremented.
Post-increment and Pre-increment Operator: C Program
Today lets see how to generate Fibonacci Series using while loop in C programming.
First Thing First: What Is Fibonacci Series ? Fibonacci Series is a series of numbers where the first two Fibonacci numbers are 0 and 1, and each subsequent number is the sum of the previous two. Its recurrence relation is given by Fn = Fn-1 + Fn-2.
Below are a series of Fibonacci numbers(10 numbers): 0 1 1 2 3 5 8 13 21 34
Logic to Generate Fibonacci Series in C Programming Language
We know that the first 2 digits in fibonacci series are 0 and 1. So we directly initialize n1 and n2 to 0 and 1 respectively and print that out before getting into while loop logic. Since we’ve already printed two fibonacci numbers we decrement the value of variable count by 2.
Inside while loop We already know that C program treat any non-zero number as true and zero as false. So once the value of variable count is zero, the execution of while loop stops. So the condition while(count) is equal to writing while(count == 0). We decrement the value of count by 1 each time the loop executes. So once count is 0 the loop execution stops.
As per definition of Fibonacci series: “..each subsequent number is the sum of the previous two.” So we add n1 and n2 and assign the result to n3 and display the value of n3 to the console.
Next, we step forward to get next Fibonacci number in the series, so we step forward by assigning n2 value to n1 and n3 value to n2.
While loop keeps repeating above steps until the count is zero. If the user entered limit/count as 10, then the loop executes 8 times(as we already printed the first two numbers in the fibonacci series, that is, 0 and 1).