Learn Programming Technique C to Master Skills - Fourth Program of Loop ( While Loop )

  • 22 hours ago
Transcript
00:00Hello everyone, today I'm going to make the program of user choice, that is to display
00:08the odd numbers 1, 3 and 5 or more than that, it will work according to our choice.
00:18So I have explained the flowchart of it, now I'm going to implement the program, okay.
00:25So I will save it first of all, give a name to it, that is prg something like this, okay
00:40and save.
00:41First thing, I will use the header file, okay, then I will use what int mean, now let us
00:53think about the variables, so variables will be od for the odd number, i to carry on the
00:59loop and I take one extra variable n, okay, then I will initialize the value of od, okay
01:09and I will initialize the value of i also, okay like this.
01:13Now I will give a message here, that is input term as I have to carry on the loop, I will
01:22use the scanf and inside it, I will write down %d, and n.
01:32So I'm going to input the value of n from the keyboard, then I will apply the loop i
01:40while i less than equals to n, so I'm comparing i with n, okay.
01:48If the condition is true, then I will display the odd number, should be int f and here should
02:00I write what inside the bracket, odd number is %d, where it is inside od like this, okay
02:14and od equals to od plus 2 and i equal to i plus 1, here I use what return 0 for exit
02:30status.
02:32So this is the program friends, only the difference is that this time I have to give the value
02:41from the keyboard that how many times the loop is going to work.
02:45Let us click on build up and see if there is any error, so there is not any error.
02:51I'm going to click on run, it will ask me that how many odd numbers I want to display,
02:58suppose 5, so it's going to display 5, it is, okay, there is a minute mistake, I have
03:11to use here 1, okay, for the odd number and here I should use what slash n, so the numbers
03:24will display row by row, let me save it, click on build, let us again input suppose 5, so
03:38it is going to display 5 odd numbers, 1, 3, 5, 7 and 9, okay.
03:45If I again run it and suppose I input the term 3, so it is going to work for 3 times,
03:50so it is going to display 3 odd numbers, 1, 3 and 5, okay, so we are giving the choice
03:56and that many times the loop is going to work, okay.
04:02I have taken 3 variables od, i and n, I have given the value of od that is 1, I have given
04:10the value of i that is 1, okay, input term, so we are going to input the value inside
04:15and suppose 3, so this loop is going to work for how many times, 3 times, okay, first of
04:22all 1 less than 3, so 1 is going to print, then 1 plus 3, 1 plus 2 that is what 3, so
04:29inside od it will be 3 and 1 plus 1, 2, the loop will carry on, 2 less than 3, the condition
04:36is true, so 3 is going to print and 3 plus 2, 5, 2 plus 1, 3, now the new value inside
04:44i is 3, again the loop will carry on, 3 equal to 3 as the value inside n is 3 and the value
04:50inside i is 3, so 3 equals to 3, the condition is true, what is going to print, 5 and 5 plus
04:572, 7 and what here, 3 plus 1, 4, 4 less than 3, condition will get false, this loop will
05:05terminate, it will get 1, 3 and 5, I am going to input that term, suppose 6, so it is displayed
05:16as 6, okay, odd numbers, so it is up, it is according to our choice, whatever that value
05:26will going to give inside n, that much time this loop is going to work, okay.

Recommended