Lab Report No.
Control Structures-II Repetition using the while
Loop
Submitted to:
Dr Sheraz Ali Khan
Submitted by:
HAMDAH SAYYED
2nd Semester
Registration No. 24PWMCT0936
Department of Mechatronics
Engineering,
University of Engineering & Technology,
Peshawar.
Abstract
The method and steps for using the while control structure were learned. This was done using
CodeBlocks as the IDE and C++ as the programming language. The syntax of the while loop
was simple and it was used to repeat a certain block of code several times automatically for
convenience. The control structure was implemented successfully and the program ran
successfully.
IV. Introduction
While loop
Relational operators
Nested structures
Outline:
Abstract
Introduction
Experimental Setup
Data and Methods
Results and Analysis
Conclusion
Appendix
II. Experimental Setup
1) DELL:
a) Core i5 8th Generation
b) 16 Gb Ram
2) CodeBlock Ver 20.03
III. Data and Methods
1. The variables were declared and they were assigned a value of 1.
2. The while loop was used and a condition was set that the variable should not be equal
to zero.
3. The user was asked for the variable and the input was taken from the user using the
stream extraction operators.
4. Various control structures were nested to set various conditions and get different
results from the program.
5. Then the codes are compiled, built and run.
6. The program kept running until the condition in the while loop became false.
7. The program gave the desired output and it was highly accurate.
IV. Results and Analysis
The created program uses the while looping structure to get the desired results. The results
were very accurate. No changes were needed in the code. The compiler flagged no errors.
V. Conclusion
Yes, all the objectives were achieved. The task wasn’t a fairly simple one because the
concept of looping structures was alien to us. But through trial and error, we prevailed and
achieved our objectives i.e:
1. Use the while loop to repeat the execution of a block of code.
2. Use relational operators to construct conditions.
3. Nesting other control structures within the while loop.
Appendix:
Task 1: Find out all the Proper Factors of an Integer
#include <iostream>
using namespace std;
int main() {
int x;
while (true) {
cout << "Enter an integer (0 to exit): ";
cin >> x;
if (x == 0) break;
for (int n = 1; n <= x / 2; n++)
if (x % n == 0)
cout << n << " is a proper factor of " << x << endl;
}
return 0;
}
“Output pic is available in last page”
Figure 1: Proper Factors of an Integer
Task 2: Perfect or Not?
#include <iostream>
using namespace std;
int main() {
int num, sum = 0;
cout << "Enter an integer number: ";
cin >> num;
for (int i = 1; i <= num / 2; i++) {
if (num % i == 0) {
sum += i;
}
}
if (sum == num) {
cout << num << " is a perfect number." << endl;
} else {
cout << num << " is not a perfect number." << endl;
}
return 0;
}
Task 3: Prime or Not?
int n;
int i=2;
int counter=1;
while(counter<=5);{
cout<<"Enter a number: ";
cin>>n;
if(n==0 || n==1){
cout<<n<<" is Not a prime number\n";
}
if(n%i==0){
cout<<n<<" is Not a prime number\n";
i++;
}
else{
cout<<n<<" is a prime number\n";
}
}
Task 4: Determine the factorial of a number
//Task 4: Determine the factorial of a number:
int x = 0;
int f = 1;
cout << "Enter the number you want a factorial of: \n";
cin >> x;
for (int i = 1; i <= x; i++ ){
f = i*f;
}
cout << f << " is the result \n";
Task 5: Multiplication Tables
int x=0;
int n=1;
cout << "Enter the number to print its multiplication table: ";
cin >> x;
cout << "The multiplication table of " << x << " is as follows: \n";
while (n <= 10){
cout << x << " x " << n << " = " << x * n << endl;
n++;
TASK No 1;