-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlooping.html
More file actions
417 lines (354 loc) · 14.9 KB
/
looping.html
File metadata and controls
417 lines (354 loc) · 14.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
<link rel="stylesheet" href="style.css">
<title>CodingElite</title>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark navm">
<div class="container-fluid">
<a class="navbar-brand" href="index.html">CodingElite</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse"
data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false"
aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link " aria-current="page" href="index.html">Home</a>
</li>
<li class="nav-item">
<a class="nav-link " href="/codingelite.org/blog.html">Blog</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link active dropdown-toggle" href="#" id="navbarDropdown" role="button"
data-bs-toggle="dropdown" aria-expanded="false">
C Tutorials
</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdown">
<li><a class="dropdown-item" href="/codingelite.org/introduction.html">introduction</a></li>
<li><a class="dropdown-item" href="/codingelite.org/branching.html">Branching Statements</a></li>
<li><a class="dropdown-item" href="/codingelite.org/looping.html">looping statements</a></li>
<li><a class="dropdown-item" href="/codingelite.org/function.html">Function</a></li>
<li><a class="dropdown-item" href="/codingelite.org/array.html">Array</a></li>
<li><a class="dropdown-item" href="/codingelite.org/string.html">string</a></li>
<li><a class="dropdown-item" href="/codingelite.org/pointer.html">pointers</a></li>
<li><a class="dropdown-item" href="/codingelite.org/structure.html">structures</a></li>
<li>
<hr class="dropdown-divider">
</li>
<li><a class="dropdown-item" href="/codingelite.org/blog.html">More Topics</a></li>
</ul>
</li>
<li class="nav-item">
<a class="nav-link" href="/codingelite.org/book.html">Books</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/codingelite.org/about.html">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/codingelite.org/contact.html">Contact us</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="bg-light">
<div class="container bg-c">
<h1 class="hed hedi-h1"><strong> Looping statements</strong></h1>
<h3>Looping statements in C</h3>
<p>Looping statement are the statements execute one or more statement again and again several number of times. <br> Lopping sattements are used when check same condition number of times.
</p>
<h5><b>Looping statements are as follows:</b></h5>
<ol>
<li><b> while </b></li>
<li><b> do while</b></li>
<li><b> for </b></li>
</ol>
<h4><b>1. while loop</b></h4>
<h5>syntax of while loop:</h5>
<div class="row">
<div class="col-md-6">
<pre class="pre-bl">
while( condition )
{
statements to be executed //Body of loop;
}
</pre>
</div>
</div>
<p>in "while loop" check the condition if the condition is true then the body of the while loop is executed, and again check the condition. the while loop is executd until the condition gets false.</p>
<h5>Flowchart for the while loop:</h5>
<div class="col-md-6">
<img src="img/While loop.png" alt="" class="img-fluid img-p">
</div>
<h5>Sample program</h5>
<h6>1.Print the sum of the frist 10 numbers.</h6>
<div class="col-md-8">
<pre class="pre-bl" title="">
<span class="hljs-meta">
<span class="hljs-meta-keyword">#include<stdio.h></span>
<span class="hljs-meta-keyword">#include<conio.h></span>
</span>
void main()
{
int n,sum;
n=10;
sum=0;
while(n<=10)
{
sum=sum+n;
}
printf("Sum of frist 10 natural numbers is %d”, sum);
getch();
}
</pre>
<p><b>Output-</b></p>
<pre class="pre-bl" title="">
sum of frist 10 natural numbers is 55
</pre>
</div>
<h6>2.Implement a program to find sum of digits of a number using while loop.</h6>
<div class="col-md-8">
<pre class="pre-bl" title="">
<span class="hljs-meta">
<span class="hljs-meta-keyword">#include<stdio.h></span>
<span class="hljs-meta-keyword">#include<conio.h></span>
</span>
void main()
{
int n,sum;
sum=0;
printf("Enter the number\n");
scanf("%d",&n;)
while(n!=0)
{
rem=n%10;
sum=sum+rem;
n=n/10;
}
printf("Sum of digit of number is %d”, sum);
getch();
}
</pre>
<p><b>Output-</b></p>
<pre class="pre-bl" title="">
Enter the number
123
Sum of digit of number is 6
</pre>
</div>
<h4><b>2. do while loop</b></h4>
<h5>syntax of do while loop:</h5>
<div class="row">
<div class="col-md-6">
<pre class="pre-bl">
do
{
Statements //Body of loop;
} while ( condition );
</pre>
</div>
</div>
<p>in "do while loop" frist time the body of loop get executed and then check the condition if condition is true the body of loop again executed. body of loop get executed until yhe condition get false. the difference between while and do while loop is in do while loop body of loop executed at least once even als condition is false.</p>
<h5>Flowchart for the do while loop:</h5>
<div class="col-md-6">
<img src="img/do While loop.png" alt="" class="img-fluid img-p">
</div>
<h5>Sample program</h5>
<h6>1.Program to print table for the given number using do while loop</h6>
<div class="col-md-8">
<pre class="pre-bl" title="">
<span class="hljs-meta">
<span class="hljs-meta-keyword">#include<stdio.h></span>
<span class="hljs-meta-keyword">#include<conio.h></span>
</span>
void main()
{
int i=1,n;
printf("Enter a number\n ");
scanf("%d",&n);
printf("The table of %d is\n"n);
do{
printf("%d \n",(n*i));
i++;
}while(i<=10);
getch();
}
</pre>
<p><b>Output-</b></p>
<pre class="pre-bl" title="">
Enter a number
5
The table of 5 is
5
10
15
20
25
30
35
40
45
50
</pre>
</div>
<h6>2.Write a program to calculate the sum of digits in given number using do while loop. </h6>
<div class="col-md-8">
<pre class="pre-bl" title="">
<span class="hljs-meta">
<span class="hljs-meta-keyword">#include<stdio.h></span>
<span class="hljs-meta-keyword">#include<conio.h></span>
</span>
void main()
{
int num,r,sum=0;
clrscr();
printf("enter any number");
scanf("%d",&num);
do{
r=num%10;
sum=sum+r;
num=num/10;
}while(num!=0);
printf("sum of digits =%d",sum);
getch();
}
}
</pre>
<p><b>Output-</b></p>
<pre class="pre-bl" title="">
enter eny number
123
Sum of digits=6
</pre>
</div>
<h4><b>3. for loop</b></h4>
<h5>syntax of for loop:</h5>
<div class="row">
<div class="col-md-6">
<pre class="pre-bl">
for(initialization; condition; increment/decrement statement)
{
Body of for loop;
}
</pre>
</div>
</div>
<p>In "for loop" The initialization statement is executed only once.Then, the condition is evaluated. If condition is false (0), for loop is terminated. But if the condition is true (nonzero), codes inside the body of for loop is executed and then increment or decrement statement is executed and again condition is checked untill the condition is false body of for loop get executed. All expressions or statement in the for loop are optional. at the end of the for loop semicolon is there then for loop not executed. </p>
<h5>Flowchart for the for loop:</h5>
<div class="col-md-6">
<img src="img/for loop.png" alt="" class="img-fluid img-p">
</div>
<h5>Sample program</h5>
<h6>1.Print the frist 10 even numbers.</h6>
<div class="col-md-8">
<pre class="pre-bl" title="">
<span class="hljs-meta">
<span class="hljs-meta-keyword">#include<stdio.h></span>
<span class="hljs-meta-keyword">#include<conio.h></span>
</span>
void main()
{
int i,n;
printf("frist 10 even natural numbers are\n");
for(i=1;i<21;i++)
{
if(i%2==0)
{
printf("%i\n",i);
}
}
getch();
}
</pre>
<p><b>Output-</b></p>
<pre class="pre-bl" title="">
frist 10 even numbers are
2
4
6
8
10
12
14
16
18
20
</pre>
</div>
<h6>2.Implement program to check the entered number is prime or not prime.</h6>
<div class="col-md-8">
<pre class="pre-bl" title="">
<span class="hljs-meta">
<span class="hljs-meta-keyword">#include<stdio.h></span>
<span class="hljs-meta-keyword">#include<conio.h></span>
</span>
void main()
{
int n,i,k;
k=0;
printf("Enter the number for checking prime or not prime\n");
scanf("%d",&n;)
for(i=2;i<n/2;i++)
{
if(n%i==0)
{
k=1;
}
}
if(k==0)
{
printf("The entered number is prime");
}
else
{
printf("The entered number is not prime");
}
printf("Sum of digit of number is %d”, sum);
getch();
}
</pre>
<p><b>Output-</b></p>
<pre class="pre-bl" title="">
Enter the number for checking prime or not prime
37
The entered number is prime
</pre>
</div>
<h5>Examples For Practice</h5>
<ol>
<li>Write a c program to print "Hello" 10 times on console using do while loop.</li>
<li>Write a c program to print sum of first 10 odd natural numbers using do while loop.</li>
<li>Write a program to check number is Armstrong or not using for loop.</li>
<li>Write a c program to reverse given number using while loop.</li>
</ol>
<a href="/codingelite.org/branching.html"> <button type="button" class="btn btn-primary btn-m"> <<Previous</button> </a>
<a href="/codingelite.org/function.html"> <button type="button" class="btn btn-primary btn-m btn-r">Next >></button></a>
</div>
</div>
<footer class="page-footer font-small blue navbar-dark bg-light footer-ptt">
<div class="footer-copyright text-center py-3">
© Copyright 2021. All Right Reserved. | Designed by the <a href="/codingelite.org/about.html">CodingElite team</a>. |
<a class="color-b" href="/codingelite.org/privacyPolicy.html">Privacy policy</a> | <a class="color-b" href="/codingelite.org/contact.html">Contact us</a>
</div>
</footer>
<!-- Optional JavaScript; choose one of the two! -->
<!-- Option 1: Bootstrap Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.bundle.min.js"
integrity="sha384-ygbV9kiqUc6oa4msXn9868pTtWMgiQaeYH7/t7LECLbyPA2x65Kgf80OJFdroafW"
crossorigin="anonymous"></script>
<!-- Option 2: Separate Popper and Bootstrap JS -->
<!--
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.5.4/dist/umd/popper.min.js" integrity="sha384-q2kxQ16AaE6UbzuKqyBE9/u/KzioAlnx2maXQHiDX9d4/zp8Ok3f+M7DPm+Ib6IU" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.min.js" integrity="sha384-pQQkAEnwaBkjpqZ8RU1fF1AKtTcHJwFl3pblpTlHXybJjHpMYo79HY3hIi4NKxyj" crossorigin="anonymous"></script>
-->
</body>
</html>