[go: up one dir, main page]

100% found this document useful (1 vote)
407 views44 pages

Lab 8 C++ Loops

The document provides an introduction to different types of loops in C++ including while, do-while, and for loops. It explains the basic structure and flow of each loop type with examples. It then provides 12 problems for students to practice implementing different loops to solve problems like calculating factorials, finding maximum values, and calculating products of user-entered positive numbers. The solutions for each problem are also presented using the various loop structures.

Uploaded by

ahmed barakat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
407 views44 pages

Lab 8 C++ Loops

The document provides an introduction to different types of loops in C++ including while, do-while, and for loops. It explains the basic structure and flow of each loop type with examples. It then provides 12 problems for students to practice implementing different loops to solve problems like calculating factorials, finding maximum values, and calculating products of user-entered positive numbers. The solutions for each problem are also presented using the various loop structures.

Uploaded by

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

Introduction to Computers

Lab 8
First Year (2019– 2020)

C++ Loops

Introduction to Computers Lab First Year 2019 - 2020


Agenda
• Introduction
o While Loop
o Do…While Loop
o For Loop

• Examples
o Loops
o Break and continue
Introduction
• In most software, the statements in the program
may need to repeat for many times.

• Loop is a control structure that repeats a group of


steps in a program.
• Loop body stands for the repeated statements.

• There are three C++ loop control statements:


o while, for, and do-while.
Any loop contains the following:
1- Initialization.
2- Condition Checking.
3- Increment/Decrement the counter.
While Loop (Flowchart)
Initialize the counter

NO

Condition

YES

while (condition)
Body of Loop END
{
//statements;
}
Increment
/Decrement the
counter
Example (While Loop)
Implement a program that prints from 1 to 10.

Flowchart C++ Code

start

int i = 1;
i=1
While (i <= 10)
NO {
i <= 10
cout << i;
YES i++;
Print i }
END
Add 1 to i
Do…While Loop (Flowchart)
Initialize the counter

Body of Loop

do
{
Increment //statements;
/Decrement the } while(condition);
counter

YES NO
Condition

END
Example (Do…While Loop)
Implement a program that prints from 1 to 10.
Flowchart C++ Code

start

int i = 1;
i=1
do
Print i {
cout << i;
Add 1 to i
i++;
YES NO
} while(i <= 10);
i <= 10

END
For Loop (flowchart)
Initialize the counter

for(initialization;
NO
Condition condition checking;
increment)
YES
{
//statements;
Body of Loop
}
END

Increment
/Decrement the
counter
Example (For Loop)
Implement a program that prints from 0 to 9.
Flowchart C++ Code

start

for(int i=0 ; i<10 ; i++)


i=0
{
NO
cout<< i << endl;
i < 10 }
YES

Print i

END
Increment i
Problem (1)
Write a C++ program that calculate the square of
n given numbers.

Enter count of numbers: 3


Enter Numbers:
5 -> 25
2 -> 4
10 -> 100
Solution (using For-Loop)
Solution 2(using While-Loop)
Solution 3(using Do-While-Loop)
Problem (2)
• Write a temperature-conversion program that
converts from Celsius to Fahrenheit .

9.0 5.0
Tf  Tc  32 Tc  (Tf  32)
5.0 9.0

Then ask the user if he/she needs more operations or


not.
Solution
Problem (3)
• There are 9870 people in a town whose
population increases by 10% each year.

• Implement a C++ program that determines how


many years it would take for the population to
exceed 30,000.
Solution
Break and continue
• Break
• The break statement provides way for
terminating the loop to terminate early.

• Continue
• The continue statement provides a
convenient way to jump back to the top of
a loop earlier than normal, which can be
used to bypass the remainder of the loop
for an iteration
Problem (4) (use break)
• Write a program that accepts numbers from the
user and counts the positive and negative numbers.
The results should be displayed when the user enters
0. A sample run of the program should be like:

Enter numbers (0 to end): -1


4
-2
44
-5
0
You entered 2 positive numbers and 3 negative
ones.
Solution
Solution – Another Solution
Problem (5)
• Write C++ program that allows the user to type
characters. If the user hits enter, the program
should display the number of entered
characters.
Solution
Problem (6) (use continue)
• Write C++ program that prints all of the numbers
from 0 to 19 that are not divisible by 4.
Solution
Problem 7
• Write a program that asks the user to enter 10
integers. The program must output the largest
element and the index at which that element was
entered.

27
Solution
int main(){ if(index==1)
int num,index=-1, max=-1; cout<<"The max value is"<<max
for(int i=1;i<=10;i++) << "in the "<<index<<" st
place"<<endl;
{
else if(index==2)
cin>>num;
cout<<"The max value is"<<max
if(num>max) << "in the "<<index<<" nd
place"<<endl;
{
else
max=num;
cout<<"The max value is"<<max
index=i; << "in the "<<index<<" th
} place"<<endl;
} return 0;
}

28
Problem 8
• Write a program that accepts numbers from the
user and then tells the maximum two numbers of
them. The results should be displayed when the
user enters a negative number.
• A sample run of the program should be like:

Enter numbers (less than 0 to end): 5


33
7
53
52
14
12
45
-1
The maximum two numbers are 53 and 52.
29
int main()
{
Solution
int x, max_Num1=0, max_Num2=0;
cout<<"Enter numbers (less than 0 to end): "<<endl;
while (true)
{
cin>> x;
if(x<0)
break;

if(x>max_Num1)
{
max_Num2=max_Num1;
max_Num1=x;

}
else if(x>max_Num2)
max_Num2=x;

}
cout<<"Max 1= "<<max_Num1<<endl;
cout<<"Max 2= "<<max_Num2<<endl;
return 0;
}

30
Problem 9
•Write C++ program for a fast food restaurant. The
user enters meal components one by one and when he
finishes he enters ‘f’. Then the program displays the
price according to the following menu.
Sample Run:
What do you want to buy: s
What do you want to buy: i
What do you want to buy: p
What do you want to buy: f
Price: 50

31
int main(){
Solution
case'i':
int sum=0;char ch='\0';
sum+=15;
while(ch!='f')
break;
{
case'f':
cout<<"What do you want
break;
to buy?"<<endl;
default:
cin>>ch;
cout<<"invalid
switch(ch)
choice"<<endl;
{case's':
}
sum+=25;
}
break;
cout<<"Price="<<sum;
case'p':
return 0;
sum+=10;
}
break;
case'j':
sum+=10;
32
break;
Problem 10
• Write a program that displays the first N Fibonacci
sequence values after 0 and 1
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …..

33
Solution
int N, first = 0, second = 1, next;
cout << "How many numbers: ";
cin >> N;

cout << first << " " << second << " ";

for (int i = 2; i < N; i++)


{
next = first + second;
cout << next << " ";
first = second;
second = next;
}

34
Problem (11)
• Write C++ Program that calculate the factorial for
number that is entered by the user.

Note:
The factorial of a number is calculated by the given
equation.

𝒏! = 𝒏 ∗ 𝒏 − 𝟏 … 𝟐 ∗ 𝟏
Solution
Problem 12
• Write a program that repeatedly collects positive
integers from the user, stopping when the user
enters a negative number or zero. After that,
output the product of all positive entries.
• A sample run should appear on the screen like the
text below.

Enter a number: 3
Enter a number: 10
Enter a number: 2
Enter a number: -13
The product of all your positive numbers is 60.
37
Solution
#include <iostream>
using namespace std;
int main( )
{
int x = 1;
int product = 1;
while ( x > 0 )
{
product *= x;
cout << "Enter a number: ";
cin >> x;
}
cout << "The product of all your positive numbers
is " << product << endl;
return 0;
} 38
Another Solution:
using break
#include <iostream>
using namespace std;
int main( )
{
int x = 1, product = 1;
while (true)
{
cout << "Enter a number: ";
cin >> x;
if (x <= 0)
break;
product *= x;
}
cout << "The product of all your positive numbers is " <<
product << endl;
return 0;
} 39
Problem 13
• Write a program that accepts from the user a list
of positive integers and displays the number of
even and odd values in the list. The program
accepts numbers from the user until he enters a
negative value.
• A sample run of the previous program is given
below:
Enter the numbers:
1
6
3
4
8
-1
The number of even values = 3
The number of odd values = 2
40
#include <iostream>
using namespace std;
int main() Solution
{
int x;
int odd=0, even=0;
cout<<"Enter positive numbers (negative number to terminate):"<<endl;
while(true)
{
cin>>x;
if(x<0)
break;

if(x%2==0)
even++;
else
odd++;

}
cout<<"Even Numbers= "<< even <<endl;
cout<<"Odd numbers= "<< odd <<endl;
return 0;
41
}
Problem 14
• Write a program that asks the user to enter an
integer and reports all divisors in ascending order.
• An example is shown below, where the user
entered 30.

Enter a number: 30
The divisors are: 1 2 3 5 6 10 15 30

42
Solution
#include <iostream>
using namespace std;
int main()
{
cout << "Enter a number: ";
int num;
cin >> num;
cout << "The divisors are: ";
for (int i=1; i<=num; i++)
{
if (num%i == 0)
cout << i << " ";
}
return 0;
} 43
Thank You 

44

You might also like