Lab 8 C++ Loops
Lab 8 C++ Loops
Lab 8
First Year (2019– 2020)
C++ Loops
• Examples
o Loops
o Break and continue
Introduction
• In most software, the statements in the program
may need to repeat for many times.
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.
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
Print i
END
Increment i
Problem (1)
Write a C++ program that calculate the square of
n given numbers.
9.0 5.0
Tf Tc 32 Tc (Tf 32)
5.0 9.0
• 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:
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:
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 << " ";
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