Uploaded by Tawki Baki

Tropia 4BLoopingStatements

advertisement
UNIVERSITY OF THE EAST
Computer Engineering Department
4B: LOOPING
STATEMENTS
Machine Problem No. 4B
Name: Jairo A. Tropia
Student No. 20181107926
Date Submitted: October 27, 2020
MACHINE PROBEM 4.1 A & B
Write a code for the following display using for loops.
a.
9
b.
117
12
104
15
91
18
78
21
65
24
52
27
39
30
26
33
13
36
0
MACHINE PROBLEM 4.1 A
Codes(Program):
#include<stdio.h>
#include<conio.h>
main()
{
int i;
for(i = 9; i <= 36; i = i + 3)
{
printf("%d\n", i);
}
getch();
OUTPUT:
MACHINE PROBLEM 4.1 B
Codes(Program):
#include<stdio.h>
#include<conio.h>
main()
{
int i;
}
for(i = 117; i >= 0; i = i - 13)
{
printf("%d\n", i);
}
getch();
OUTPUT:
MACHINE PROBLEM 4.2 B & C
Design a code for the following display using while loops.
b.
2
c.
1
6
1
12
2
20
3
30
5
42
8
56
13
72
21
90
34
100
55
MACHINE PROBLEM 4.2 B
Codes(Program):
#include<stdio.h>
#include<conio.h>
main()
{
int ctr = 1, num = 2, multi = 2;
while(ctr<=10)
{
printf("%d\n", num);
num = num + (multi * 2);
ctr++;
multi++;
}
getch();
}
OUTPUT:
MACHINE PROBLEM 4.2 C
Codes(Program):
#include<stdio.h>
#include<conio.h>
main()
{
int ctr = 1, num1 = 0, num2 = 1, sum = 0;
while(ctr <= 10)
{
sum = num1 + num2;
num1= num2;
num2 = sum;
printf("%d\n", num1);
ctr++;
}
getch();
}
OUTPUT:
MACHINE PROBLEM 4.3 A & C
Create a code for the following display using do-while loops
a.
100
c.
64
82
32
66
16
52
8
40
9
30
27
22
81
16
243
12
729
10
2187
MACHINE PROBLEM 4.3 A
Codes(Program):
#include<stdio.h>
#include<conio.h>
main()
{
int ctr = 1, num = 100, multi = 9;
do
{
printf("%d\n", num);
num = num -(multi * 2);
ctr++;
multi--;
} while(ctr <= 10);
}
getch();
OUTPUT:
MACHINE PROBLEM 4.3 C
Codes(Program):
#include<stdio.h>
#include<conio.h>
main()
{
int ctr = 1, num1 = 64, num2 = 9;
do
{
if(num1 >= 8)
{
printf("%d\n", num1);
num1 = num1 / 2;
ctr++;
}
else if(num2 >= 9)
{
printf("%d\n", num2);
num2 = num2 * 3;
ctr++;
}
}while(ctr <= 10);
}
getch();
OUTPUT:
MACHINE PROBLEM 4.4 D
Build a code for the following display using nested loops.
d.
1
1
1
1
1
2345
234
23
2
MACHINE PROBLEM 4.4 D
Codes(Program):
#include<stdio.h>
#include<conio.h>
main()
{
int out, inn;
for(out = 5; out>= 1; out--)
{
for(inn = 1; inn<=out; inn++)
printf("%d", inn);
printf("\n");
}
getch();
}
OUTPUT:
MACHINE PROBLEM 4.5 A
Using nested loops make a program that will do the following displays.
a.
1
2
3
2
4
6
3
6
9
MACHINE PROBLEM 4.5 A
Codes(Program):
#include<stdio.h>
#include<conio.h>
main()
{
int ctr = 1, num1 = 1, num2 = 2, num3 = 3;
for(ctr = 1; ctr <= 3; ctr++)
{
for(ctr = 4; ctr <= 6; ctr++)
{
for(ctr = 7; ctr <= 9; ctr++)
{
printf("%d\n", num1);
num1 = num1 + 1;
printf("%d\n", num2);
num2 = num2 + 2;
printf("%d\n", num3);
num3 = num3 + 3;
}
}
}
getch();
}
OUTPUT:
DISCUSSION:
In the past activity for looping statements, I learned the basic
fundamentals of a looping statement. For this activity, it enhances my skills
in using looping statements by solving the given machine problems. Most of
the given machine problems need to display the given integers accordingly.
Since in the last activity for looping statements I learned the basic
fundamentals of looping statements it becomes easier for me to create a
program that will be used to provide the proper output that is needed. The
only problem I encounter in solving the machine problem is creating a nested
loop because in the last activity for the looping statement nested loop is not
included. Since I know the basics of looping statements, I have an idea of
how to create a nested loop program. It is very important to know the basics
of looping statement because it will help you a lot in solving and creating a
program.
CONCLUSION:
The Looping statement is a program that can produce different numbers
accordingly. Also, a Nested loop is a part of a looping statement that allows
a loop inside another loop. And Nested loops have three types which are
nested for loop, nested while loop, and nested do-while loop. Nested loops
are used to cycle through a matrix, tabular data, and multi-dimensional
arrays.
Download