Learn Programming Technique C to Master Skills - Loop With If Statement Program

  • 2 days ago
Transcript
00:00Hello everyone, today I am going to explain program of loop with if statement and I am
00:09going to make the program in C. I have explained this program in flowchart in my previous video.
00:19The program is to input numbers from the keyboard and count total even numbers and total odd
00:26numbers. For that I have taken the variables n, num, ev, od and i. For total counting of
00:36even numbers I have initialized the value of ev that is what 0 for the odd what 0 and
00:43I have taken the variable i equals to 1. For counting I have taken ev equals to 0 and od
00:49equals to 0. Now suppose I want to carry on the loop for 3 times so I have given the value
00:54of n equals to 3 and this loop is going to carry on for how many times? 3 times, 1 less
01:04than 3 the condition is true. As the condition will be true it will ask me to input any number.
01:13Suppose I input 60 and then the if statement is applied if 60 is moduled by 2, if the remainder
01:22of it is what 0 so 0 equals to 0 that means it is even number so the even number is going
01:30to count how value of ev which is what 0 so 0 plus 1 equal to 1 so the first even number
01:37it is counted. So the number which is input that is what it is even as the remainder is
01:450. Now this is linked with the equation i equal to i plus 1 so 1 plus 1 that is what 2 and here
01:56if you see this loop here is going to carry on 2 less than what is inside and it is 3 the condition
02:05is true. The next value inside now is 5, 5 modulation by 2 so yes it will module and the
02:15modulation will take place so there will be a remainder inside that will come so here the
02:22condition will get false it will come to the what no part and it is going to count the odd so 0
02:29plus 1 equal to 1 and it is going to add with the increment of the counter variable so 2 plus 1
02:39that will be what 3. So in this way the loop will carry on and at last we will get the count of
02:45total even numbers and total odd numbers. So let us make the program, I am going to make it
02:58in code blocks, file, new, empty file and save it. Write a name c loop5b like this,
03:18hash include, stdio.h, I will use what int main, inside it I will start taking the variables.
03:36Let us think about the variables int, I will take to count even number e to count odd number o to
03:47carry on the loop i to input the term n and to input the number num. I will initialize the values
03:58also e equals to 0, o equals to 0, this I have initialized. What should I do? I should give a
04:09message here intep input term that how many times you are going to carry on the loop and then what
04:20is scanf inside it what percent d, comma and n. The n times the loop is going to carry on.
04:32Now we will apply the for loop for i equals to 1, 1 less than equals to n and then i plus plus.
04:44Now what I have to do? I have to first write down a statement here that is what printf
04:51input any number. I will input any number from the keyboard, I will use scanf that is
05:04percent d, comma the value will read by the scanf and going to store inside num variable.
05:14Now I will use the if statement, if num will use the modulation by 2 that is equal equals to 0
05:24when the modulation will take place and if the remainder is what 0 in that case it will count
05:30the even. So e equals to e plus 1 and if the condition is false, if the remainder is not 0
05:39so it means it is odd. For odd we have taken o, so o equal to o plus 1 like this.
05:51So when the loop will complete at the end of the loop will display printf
05:58printf slash n total count of even numbers
06:09that is percent d where it is inside ev and then what printf
06:16printf total count of odd numbers that is percent d where it is inside odd.
06:33Here what I will do? I should use
06:38return 0. Let us save it and click on build, it's showing me error showing that we haven't
06:58used semicolon here. I will again save it on the seventh row I haven't used the semicolon,
07:04I have used it now again I will click on build. So I think we haven't used the main function,
07:15we have used it so let us save it and again try to run it.
07:34So it is e and here it is what o that is why it is showing the wrong result,
07:52the error sorry. So now I have click on build, now let us give the term here suppose what 3,
08:01so it's going to ask me three times the number which I am going to input, suppose I input here
08:12what 5 and then again it's going to ask me input the number suppose here I input what
08:268 and then again I input what 1. So how many even numbers that is 1, that is 8 and two odd numbers
08:39so it counts the two odd numbers. So despite the correct result let us quickly understand how the
08:45things work. I have taken the variable e, o, i, n and num, e for counting the even numbers,
08:54o for counting the odd numbers, suppose I have to carry on the loop for only two times.
09:01So the value I will put inside n is 2, so the loop will carry on the value of i is 1,
09:081 less than 2, the condition is true. Suppose the first value if I input inside num that is 8,
09:16when it will come to the if statement that is 8 moduled by 2 obviously I will get a zero
09:23remainder. So here 0 equals to 0, so this if statement is true, so it's going to count that
09:31is 0 plus 1, so it's going to count that the first number is even which is going to store inside e.
09:41Then what will happen the value of i is going to increment by 1 from 1 to a 2,
09:482 less than equal to 2, so 2 equal to 2 the condition is true. Again this message is going
09:54to print and I am going to input a new value inside num. Suppose this time I input what 7,
10:00so 7 modulation will take place 7 module by 2.
10:07So some remainder value is going to come that is not equal equals to 0,
10:12so this condition is what false it will come to the else part and it's going to count the odd
10:19numbers, so 1 plus 1 that is 1. So two numbers we have entered one is what even another one is odd
10:28then the value of i is going to increment to 3, 3 less than 2 condition will get false
10:37this loop will terminate and at the end it will display total count of even number which
10:45is there inside e and total count of odd number which is there inside o. Thank you.

Recommended