Skip to player
Skip to main content
Skip to footer
Search
Connect
Watch fullscreen
Like
Comments
Bookmark
Share
Add to Playlist
Report
Learn Programming Technique C to Master Skills - Loop With If Statement Program
Tutorials Arena
Follow
10/13/2024
Category
📚
Learning
Transcript
Display full video transcript
00:00
Hello everyone, today I am going to explain program of loop with if statement and I am
00:09
going to make the program in C. I have explained this program in flowchart in my previous video.
00:19
The program is to input numbers from the keyboard and count total even numbers and total odd
00:26
numbers. For that I have taken the variables n, num, ev, od and i. For total counting of
00:36
even numbers I have initialized the value of ev that is what 0 for the odd what 0 and
00:43
I have taken the variable i equals to 1. For counting I have taken ev equals to 0 and od
00:49
equals to 0. Now suppose I want to carry on the loop for 3 times so I have given the value
00:54
of n equals to 3 and this loop is going to carry on for how many times? 3 times, 1 less
01:04
than 3 the condition is true. As the condition will be true it will ask me to input any number.
01:13
Suppose I input 60 and then the if statement is applied if 60 is moduled by 2, if the remainder
01:22
of it is what 0 so 0 equals to 0 that means it is even number so the even number is going
01:30
to count how value of ev which is what 0 so 0 plus 1 equal to 1 so the first even number
01:37
it is counted. So the number which is input that is what it is even as the remainder is
01:45
0. Now this is linked with the equation i equal to i plus 1 so 1 plus 1 that is what 2 and here
01:56
if you see this loop here is going to carry on 2 less than what is inside and it is 3 the condition
02:05
is true. The next value inside now is 5, 5 modulation by 2 so yes it will module and the
02:15
modulation will take place so there will be a remainder inside that will come so here the
02:22
condition will get false it will come to the what no part and it is going to count the odd so 0
02:29
plus 1 equal to 1 and it is going to add with the increment of the counter variable so 2 plus 1
02:39
that will be what 3. So in this way the loop will carry on and at last we will get the count of
02:45
total even numbers and total odd numbers. So let us make the program, I am going to make it
02:58
in code blocks, file, new, empty file and save it. Write a name c loop5b like this,
03:18
hash include, stdio.h, I will use what int main, inside it I will start taking the variables.
03:36
Let us think about the variables int, I will take to count even number e to count odd number o to
03:47
carry on the loop i to input the term n and to input the number num. I will initialize the values
03:58
also e equals to 0, o equals to 0, this I have initialized. What should I do? I should give a
04:09
message here intep input term that how many times you are going to carry on the loop and then what
04:20
is scanf inside it what percent d, comma and n. The n times the loop is going to carry on.
04:32
Now we will apply the for loop for i equals to 1, 1 less than equals to n and then i plus plus.
04:44
Now what I have to do? I have to first write down a statement here that is what printf
04:51
input any number. I will input any number from the keyboard, I will use scanf that is
05:04
percent d, comma the value will read by the scanf and going to store inside num variable.
05:14
Now I will use the if statement, if num will use the modulation by 2 that is equal equals to 0
05:24
when the modulation will take place and if the remainder is what 0 in that case it will count
05:30
the even. So e equals to e plus 1 and if the condition is false, if the remainder is not 0
05:39
so it means it is odd. For odd we have taken o, so o equal to o plus 1 like this.
05:51
So when the loop will complete at the end of the loop will display printf
05:58
printf slash n total count of even numbers
06:09
that is percent d where it is inside ev and then what printf
06:16
printf total count of odd numbers that is percent d where it is inside odd.
06:33
Here what I will do? I should use
06:38
return 0. Let us save it and click on build, it's showing me error showing that we haven't
06:58
used semicolon here. I will again save it on the seventh row I haven't used the semicolon,
07:04
I have used it now again I will click on build. So I think we haven't used the main function,
07:15
we have used it so let us save it and again try to run it.
07:34
So it is e and here it is what o that is why it is showing the wrong result,
07:52
the error sorry. So now I have click on build, now let us give the term here suppose what 3,
08:01
so it's going to ask me three times the number which I am going to input, suppose I input here
08:12
what 5 and then again it's going to ask me input the number suppose here I input what
08:26
8 and then again I input what 1. So how many even numbers that is 1, that is 8 and two odd numbers
08:39
so it counts the two odd numbers. So despite the correct result let us quickly understand how the
08:45
things work. I have taken the variable e, o, i, n and num, e for counting the even numbers,
08:54
o for counting the odd numbers, suppose I have to carry on the loop for only two times.
09:01
So the value I will put inside n is 2, so the loop will carry on the value of i is 1,
09:08
1 less than 2, the condition is true. Suppose the first value if I input inside num that is 8,
09:16
when it will come to the if statement that is 8 moduled by 2 obviously I will get a zero
09:23
remainder. So here 0 equals to 0, so this if statement is true, so it's going to count that
09:31
is 0 plus 1, so it's going to count that the first number is even which is going to store inside e.
09:41
Then what will happen the value of i is going to increment by 1 from 1 to a 2,
09:48
2 less than equal to 2, so 2 equal to 2 the condition is true. Again this message is going
09:54
to print and I am going to input a new value inside num. Suppose this time I input what 7,
10:00
so 7 modulation will take place 7 module by 2.
10:07
So some remainder value is going to come that is not equal equals to 0,
10:12
so this condition is what false it will come to the else part and it's going to count the odd
10:19
numbers, so 1 plus 1 that is 1. So two numbers we have entered one is what even another one is odd
10:28
then the value of i is going to increment to 3, 3 less than 2 condition will get false
10:37
this loop will terminate and at the end it will display total count of even number which
10:45
is there inside e and total count of odd number which is there inside o. Thank you.
Recommended
12:06
|
Up next
Learn Programming Technique C to Master Skills - Continuous If Statement Program
Tutorials Arena
10/8/2024
8:07
Learn Programming Technique C to Master Skills - Loop with if Statement Second Program
Tutorials Arena
10/17/2024
7:56
Learn Programming Technique C to Master Skills - Nested If Statement Program
Tutorials Arena
10/12/2024
8:01
Learn Programming Technique C to Master Skills - Loop Within Loop Program
Tutorials Arena
10/17/2024
5:14
Learn Programming Technique C to Master Skills - Single If Statement Program Explanation
Tutorials Arena
10/12/2024
4:57
Learn Programming Technique C to Master Skills - Loop With If Statement Program ( Flow Chart )
Tutorials Arena
10/13/2024
5:02
Learn Programming Technique C to Master Skills - Third Program of Loop
Tutorials Arena
10/17/2024
8:32
Learn Programming Technique C to Master Skills - If .... else if Statement Program
Tutorials Arena
10/8/2024
3:51
Learn Programming Technique C to Master Skills - First Program of Loop (For Loop)
Tutorials Arena
10/13/2024
3:55
Learn Programming Technique C to Master Skills - First Program of Loop (While Loop)
Tutorials Arena
10/13/2024
4:57
Learn Programming Technique C to Master Skills - Loop with If Statement Second Program Continue...
Tutorials Arena
10/17/2024
4:07
Learn Programming Technique C to Master Skills - Second Program of Loop (While Loop)
Tutorials Arena
10/17/2024
12:25
Learn Programming Technique C to Master Skills - Call By Reference Program
Tutorials Arena
11/8/2024
5:33
Learn Programming Technique C to Master Skills - Fourth Program of Loop ( While Loop )
Tutorials Arena
10/13/2024
9:20
Learn Programming Technique C to Master Skills - Union Program
Tutorials Arena
11/10/2024
9:12
Learn Programming Technique C to Master Skills - Program of Recursion
Tutorials Arena
11/6/2024
2:58
Learn Programming Technique C to Master Skills - Nested If Statement Program (Flow Chart)
Tutorials Arena
10/12/2024
4:22
Learn Programming Technique C to Master Skills - Pointer And Arrays Program
Tutorials Arena
11/9/2024
4:03
Learn Programming Technique C to Master Skills - Second Program of Loop ( For Loop )
Tutorials Arena
10/17/2024
4:28
Learn Programming Technique C to Master Skills - Fourth Program of Loop ( For Loop )
Tutorials Arena
10/13/2024
3:40
Learn Programming Technique C to Master Skills - Array of Pointer Program
Tutorials Arena
11/9/2024
4:54
Learn Programming Technique C to Master Skills - Single If Statement Program (Flow Chart)
Tutorials Arena
10/12/2024
3:51
Learn Programming Technique C to Master Skills - First Program of Loop (Flowchart)
Tutorials Arena
10/13/2024
8:18
Learn Programming Technique C to Master Skills - Loop Within Loop Program Explanation in Flow chart
Tutorials Arena
10/17/2024
10:48
Learn Programming Technique C to Master Skills - Structure and Functions Program
Tutorials Arena
11/10/2024