-
Notifications
You must be signed in to change notification settings - Fork 0
/
4 chapter4.c
278 lines (163 loc) · 4.6 KB
/
4 chapter4.c
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
#include<stdio.h>
int main(){
//-------- Loop Control Instructions --------//
/*
1. for loop
Syntax:
for(initialization; condition; updation){
do something;
};
The code goes like -> initialized,
then checks condition,
if it is true then performs code,
then updation ,
after that it again checks the condition,
and if it is true then again performs code and then updation,
the intial condition is checked only once.
Eg:
for(i=1; i<=5;i++){
printf("Hello World");
}
--- If you want to print numbers from 1 to 100 then ----
for(i=1; i<=100 ; i++){
printf("%d", i);
};
Note; The loop can also run in reverse:
for(i=10; i<=1; i--){
printf("%d", i);
}
<------ i is called as iterator or counter variable ----->
Increment and Decrement Operators
1. i++ (Post Increment Operator)
-> First use the value of i and then increment it
Eg: int i = 1;
printf("%d", i++); --- it will print 1
printf("%d", i); ---- it will print 2 as i value is incremented
2. ++i (Pre increment operator)
-> First increment the value of i and then use it
Eg: int i = 1;
printf("%d", ++i); --- it will print 2
printf("%d", i); ---- it will print 2 again
3. --i (Pre decrement Operator)
4. i-- (Post decrement Operator)
Loop Counter can be float or even Character
Eg:
float i;
for(i = 1.0 ; i<=5.0; i++){
printf("Hello World");
}
Eg:
char let;
for (let = 'a' ; let <='z' ; let++){
printf("%c", let);
}
Infinite Loops
The condition in between is left blank or is always false then this kind of loop occurs.
Eg:
for(i=1; i>1; i++){
printf("I am Infinite");
}
OR
for(i=1; ; i++){
printf("I am Infinite");
}
2. while loop
Syntax:
while(condition){
do something;
}
Eg;
int i=1; ------> We intialize the variable outside the while loop
while(i<=5){ ----------> here we write only the condition
printf("My name is Yash \n");
i++; ---------> Updation is done inside the while loop
}
Q. Print the numbers from 0 to n if the value of n is given by user.
int i=0;
int n;
printf("Enter the value of n");
scanf("%d", &n);
while(i<=n){
printf("%d", i);
i++;
}
3. do while loop
do{
code
}while(condition)
Eg:
int i=1;
do{
printf("%d",i);
i++; //very important to write or the loop will be infinite
}while(i<=5);
Q. Print the sum of first n natural numbers where n =4
int n; int sum=0;
printf("Enter the number ");
scanf("%d"n);
for(i=1;i<=n;i++){
sum=sum+i;
}
printf("The value of sum is %d",sum);
Q. Print the numbers in reverse order
for(i=n;i>=1 (or) i==0;i++){
sum=sum+i;
}
Q. Print the sum as well as the numbers in the same loop
for(i=1; j=n; i<=n; j==0; i++; j--){
sum=sum+i;
printf("%d",j);
}
printf("The sum is %d",sum);
Q. Print the table of 2
for(i=1; i<=10 ; i++){
n = n*i;
printf("%d",n);
}
//-------- Break and Continue --------//
1. Break
Get out of all the loops no matter what (exit from loop)
Q. Keep taking numbers as input until the user enters an odd number
int num;
do
{
printf("Enter a number ");
scanf("%d", &num);
if (num%2 != 0)
{
break;
}
} while (1);
printf("You entered an odd number");
2. Continue
If we want to skip some lines and then continue the loop then it can be used
Eg:
for(i=1; i<=5 ; i++){
if(i==3){
continue; //dont run further and continue loop
}
printf("%d",i);
}
Q. Print all the numbers from 1 to 10 except 6
Q. Print all the odd numbers between 5 and 50
for (int i = 5; i < 50; i++)
{
if (i%2!=0)
{
printf("%d \n",i);
}
}
Q. Print the factorial of the given number n
int fact=1;
int n;
printf("Enter the value of n");
scanf("%d",&n);
for (int i = 1; i <= n; i++)
{
fact=fact*i;
}
printf("The factorial is %d",fact);
Q. Write a program to print prime numbers in range
*/
return 0;
}