0% found this document useful (0 votes)
3 views9 pages

lab 4

Uploaded by

khushilikespak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

lab 4

Uploaded by

khushilikespak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

School of Mechanical & Manufacturing Engineering (SMME),

National University of Science and Technology (NUST),

Sector H-12, Islamabad

Program: BE-Aerospace Section: AE-02(A)

Session: Spring 2025 Semester: 2nd

Course: Fundamentals of Programming Course Code: CS-118

LAB REPORT # 4

“Control structures-II Repetition using while and for loops”

Name: Khushbakht Jahan

CMS ID: 511701


CONTENTS:

INTRODUCTION: 3

TYPES OF LOOPS: 3

For loop: 3

While loop: 4

Do-while loop: 4

MY TASKS 5

Task 1: Find out all proper factors of a number 5

Task 2: Perfect or not 6

Task 3: Prime or not 7

Task 4: Factorial of a number 8

Task 5: Multiplication Tables 9

CONCLUSION 10

Table of figures:
Figure 1: for loop.........................................................................................................................................3
Figure 2: while loop.....................................................................................................................................4
Figure 3: do-while loop................................................................................................................................5

Repetition using loops


Introduction:
In this lab, we learned to use different types of loops and their purposes. These were the for, while and
do-while loops. Loops are a fundamental concept in programming, and they are used to repeat a block of
code multiple times. They help simplify code, reduce redundancy, and make programs more efficient.

Types of loops:
 For loop
 While loop
 Do-while loop

For loop:
The for loop in C++ is a control flow statement that allows us to execute a block of code repeatedly for a
specific number of iterations.

It is used when we know how many times we want a block of code is executed.

Syntax:

for (initialization; condition; update) {

// Code to execute

Figure 1: for loop

While loop:
The while loop in C++ is a control flow statement that allows you to execute a block of code repeatedly
as long as a specified condition is true. Unlike the for loop, which is typically used when the number of
iterations is known, the while loop is more suitable when the number of iterations is unknown or
depends on a condition that may change during runtime.

Syntax:

while (condition) {

// Code to execute

}
Figure 2: while loop

Do-while loop:
The do-while loop in C++ is a control flow statement that allows you to execute a block of code
repeatedly as long as a specified condition is true. Unlike the while loop, which checks the
condition before executing the loop body, the do-while loop checks the condition after executing the
loop body. This means that the loop body is guaranteed to execute at least once, even if the condition is
initially false.

Syntax:

do {

// Code to execute

} while (condition);

Figure 3: do-while loop

My tasks
Task 1: Find out all proper factors of a number
The user was asked to enter an integer to find out its proper factors. I used for loop for this code.

Code:
#include <iostream>

using namespace std;

int main()

int n;

cout<<"enter an integer: ";

cin>>n;

cout<<"proper factors of "<<n<<" are :"<<endl;

for (int i=1; i < n; i++)

if (n%i==0)

cout<<i<<endl;

return 0;

Output:

Task 2: Perfect or not


We asked the user to enter an integer to check if it is a perfect number or not ( the sum of its factors
equate to that number). I used the for loop in this code and if-else statements.

Code:

#include <iostream>

using namespace std;


int main()

int n=0;

int sum=0;

cout<<"enter an integer: ";

cin>>n;

for (int i=1 ; i<n ; i++)

if (n%i==0)

sum+=i;

if (sum==n)

cout<<n<<" is a perfect number";

else

cout<<n<<" is not a perfect number";

return 0;

Output:

Task 3: Prime or not


In this task, we asked the user to enter an integer to check whether it is a prime number or not. We
entered a separate condition for numbers like 1 and 0 which are neither prime nor composite. We used
the Boolean operator for the condition. I used for loop in this code.

Code:

#include <iostream>

using namespace std;


int main()

int n;

cout<<"enter an integer: ";

cin>>n;

bool prime=true;

if (n<=1){

prime=false;

}else {

for (int i = 2; i * i <= n; i++) {

if (n % i == 0) {

prime=false;

break;

if (prime){

cout<<n<<" is a prime number"<<endl;

}else {

cout<<n<<" is not a prime number"<<endl;

return 0;

Output:

Task 4: Factorial of a number


In this task, the code prompted the user to enter an integer from 1 to 20. The data type ‘int’ cannot store
very large values so we used ‘unsigned long long’ data type. The unsigned long long data type can store
much larger values than int. Its maximum value is 18,446,744,073,709,551,615, which allows it to store
factorials up to 20!. I used for loop in this code.

Code:

#include <iostream>

using namespace std;

int main() {

int n;

unsigned long long factorial = 1;

cout<<"enter an integer: ";

cin>>n;

for (int i = 1; i <= n; i++) {

factorial *= i;

cout << "Factorial of " << n << " = " << factorial << endl;

return 0;

Output:

Task 5: Multiplication Tables


In this task, the code prompted the user to enter an integer and I used the while loop with condition to
give output as the integer’s table till 10. The integer and its multiplicand ‘i’ are initialized and then used
in the code. I used while loop in this code.

Code:

#include <iostream>

using namespace std;

int main ()
{

int num;

int i=1;

cout<<"enter any integer: ";

cin>>num;

cout<<"table for "<<num<<" is "<<endl;

while (i<=10) {

cout<<num<<"x"<<i<<"="<<num*i<<endl;

i++;

return 0;

Output:

Conclusion:
Loops are a fundamental concept in C++ programming that allow us to execute code repeatedly. By
understanding the differences between for, while, and do-while loops, we can choose the right tool for
the task at hand. Each one serves an important purpose depending on the type of condition we want the
code to meet.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy