• 2 days ago
Transcript
00:00Hello everyone, today I am going to explain this program with the use of for loop that
00:09is to display the square of first three odd numbers 1, 9 and 25.
00:16The last video I have explained with the use of while loop.
00:23So let me open the code blocks.
00:29So in a code block I am going to create a new file and I am going to save it also, so
00:37I am going to file and save file, I will give a name to prg2c, prg2b something like this
00:492c.
00:50Let us give the name to it and click on save, use hash include stdio.h, inside it I am going
01:01to use int main and I am going to take the variables int sq, od and then what i, I will
01:12initialize the value of od and I will apply the loop i equals to 1, i less than equal
01:20to 3 and then what i, plus plus.
01:25Now what should I do, I use what sq equal to od into od, so the square is going to store
01:33inside sq and I will print it that is slash n square of odd number is percent d where
01:46it is inside sq, so it's going to display and then I need the next odd number, so I
01:54will write down od equals to od plus 2 okay and in this way the loop will carry on and
02:02get the result, so I will just choose here return zero and we will see, I will click
02:10on build and run it, so it displayed 1, 9 and 25.
02:16So how the things work, let us understand I have taken three variables sq, od and i,
02:22the value of od is 1, this loop will carry on for three times, what is inside i, value
02:28of i is 1, 1 less than 3, the condition is true.
02:34Now what is inside od, it is 1, so 1 into 1, so the resulted value is 1 which is going
02:39to store inside sq and then it's going to display, so first of all 1 is going to display
02:46then 1 plus 2, a new value inside od is 3 okay, the value of i is going to increment
02:53by 1 that is what 2, so now the new value of i is 2, 2 less than 3, the condition is
03:00true, inside od it is 3, so 3 into 3 that is what 9, so 9 is going to store inside sq,
03:08it will display square of odd number is that is what 9 okay, then what 3 plus 2, a new
03:17value inside od it is 5, again it's going to increment the loop, so the value of i will
03:24be what 3, it will come to the decision part, 3 equal to 3, the condition is true, what
03:31inside od it is 5, so 5 into 5, 25, a new value inside sq is what 25, it's going to
03:38display what 25, then 5 plus 2, 7 okay, the value of i increment by 1, from 3 it will
03:48be what 4, 4 less than 3, condition false, this loop will terminate, the for loop will
03:54terminate okay, so we will get what 1, 9 and 25.

Recommended