Name: MD Mahin Uddin B.tech - CE Semester II: 1. Write A Program To Print Even Numbers Upto 100 With While Loop

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

Name : Md Mahin Uddin

B.tech - CE
Semester II

Tutorial-6
Loop

1. Write a program to print even numbers upto 100 with while loop.

#include<stdio.h>

int main()
{ int i=0;

printf("Even numbers upto 100\n");


while(i<=100)
{
if(i%2==0)
{ printf("%d\
n",i);
} i+
+;
}

OUTPUT
Even numbers upto 100
0
2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32
34
36
38
40
42
44
46
48
50
52
54
56
58
60
62
64
66
68
70
72
74
76
78
80
82
84
86
88
90
92
94
96
98
100
2. Write a program to print odd numbers upto 500 with do while loop.

#include<stdio.h>

int main()
{ int i=1;
printf("odd numbers upto 500\n");
do
{
if(i%2==1)
{ printf("%d\
n",i);
} i+
+;
}
while(i<=500);
}

OUTPUT

Even numbers upto 100


1
3
5
7
9
11
13
15
17
19
21
23
25
27
29
31
33
35
37
39
41
43
45
47
49
51
53
55
57
59
61
63
65
67
67
69
71
73
75
77
79
81
83
85
87
89
91
93
95
97
99
101
103
105
107
109
111
113
115
117
119
121
123
125 127
129
131
133
135
137
139
141
143
145
147
149
151
153
155
157
159
161
163
165
167
169
171
173
175
177
179
181
183
185
187
189
191
193
195
197
199
201
203
205
207
209
211
213
215
217
219
221
223
225
227
229
231
233
235
237
239
241
243
245
247
249
251
253
255
257
259
261
263
265
267
269
271
273
275
277
279
281
283
285
287
289
291
293
295
297
299
301 303
305
307
309
311
313
315
317
319
321
323
325
327
329
331
333
335
337
339
341
343
345
347
349
351
353
355
357
359
361
363
365
367
369
371
373
375
377
379
381
383
385
387
389 391
393
395
397
399
401
403
405
407
409
411
413
415
417
419
421
423
425
427
429
431
433
435
437
439
441
443
445
447
449
451
453
455
457
459
461
463
465
467
469
471
473
475
477 479
481
483
485
487
489
491
493
495
497
499

3. Write a program to find the sum of first 10 natural numbers using for loop.

#include<stdio.h>

int main()
{
int i,sum=0;
printf("sum of 10 natural numbers:");
for(i=1;i<=10;i++)
{
sum=sum+i;
}
printf ("%d",sum);

OUTPUT

sum of 10 natural numbers:55

4. Write a program to display the cube of the number upto given an integer using for loop.

#include<stdio.h>

int main()
{ int n,i,cube;
printf("input numbers of terms:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
cube=1; cube=cube*i*i*i; printf ("\nNumber is %d and the cube
of the %d is :%d",i,i,cube);
}

OUTPUT

input numbers of terms:5

Number is 1 and the cube of the 1 is :1


Number is 2 and the cube of the 2 is :8
Number is 3 and the cube of the 3 is :27
Number is 4 and the cube of the 4 is :64
Number is 5 and the cube of the 5 is :125

5. Write a program to display the multiplication table vertically from 1 to n.

#include<stdio.h>

int main()
{ int n,i,j,tab;
printf("input numbers of terms:");
scanf("%d",&n);
printf("\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=10;j++)
{ tab=i*j
;
printf ("%d*%d=%d\n",i,j,tab);
}
printf("\n\n");
}
}

OUTPUT

input numbers of terms:5

1*1=1
1*2=2
1*3=3
1*4=4
1*5=5
1*6=6
1*7=7
1*8=8
1*9=9
1*10=10

2*1=2
2*2=4
2*3=6
2*4=8
2*5=10
2*6=12
2*7=14
2*8=16
2*9=18
2*10=20

3*1=3
3*2=6
3*3=9
3*4=12
3*5=15
3*6=18
3*7=21
3*8=24
3*9=27
3*10=30
4*1=4
4*2=8
4*3=12
4*4=16
4*5=20
4*6=24
4*7=28
4*8=32
4*9=36
4*10=40

5*1=5
5*2=10
5*3=15
5*4=20
5*5=25
5*6=30
5*7=35
5*8=40
5*9=45
5*10=50

6. Write different programs to display following patterns shown below. (Note:One Program
for each Pattern)

1.
#include<stdio.h>

int main()
{ int i,j;

for(i=1;i<=5;i++)
{ for(j=1;j<=i;j+
+)
{ printf
("*");
}
printf("\n\n");
}
}
OUTPUT

**

***

****

*****

2.

#include<stdio.h>

int main()
{ int i,j;
for(i=1;i<=5;i++)
{ for(j=1;j<=5;j+
+)
{ if(j>=6-i)
{ printf
("*");
}
else
{ printf("
");
} } printf("\
n");
}

OUTPUT

*
**
***
****
*****

3.

#include<stdio.h>

int main()
{ int i,j,k;
for(i=1;i<=5;i++)
{
k=1;
for(j=1;j<=9;j++)
{
if(j>=6-i && j<=4+i && k)
{ printf
("*"); k=0;
}
else
{ printf("
");
k=1;
} } printf("\
n");
}

OUTPUT

*
**
***
****
*****
4.
#include<stdio.h>

int main()
{ int i,j,k;
for(i=1;i<=5;i++)
{ for(j=1;j<=5;j+
+)
{ if(j<=6-i)
{ printf
("*");

}
else
{ printf("
");
} } printf("\
n");
}

OUTPUT

*****
****
***
**
*

5.
#include<stdio.h>
#include<conio.h>
void main()
{ int i,j,k;
for(i=1;
i<=5;i+
+)
{

for(j=1;j<=i;j++)
{
if(i%2==1)
{
if(j%2==1) printf("0");
else
printf("1");
}
else
{
if(j%2==1)
printf("1");
else
printf("0");
}}

printf("\n");
}
getchar();
}

OUTPUT

0
10
010
1010
01010

7.

#include<stdio.h>
#include<conio.h>
void main()
{ int i,j,k;
for(i=1;
i<=5;i+
+)
{

for(j=1;j<=i;j++)
{ if(j<=i)
{ printf("%d",i);
} } printf("\
n");
}
getchar();
}
OUTPUT

1
22
333
4444
55555

You might also like