際際滷

際際滷Share a Scribd company logo
Object Oriented
Programming (Lab)
Lecture 1-> LOOPS
Semester- Spring 2024
LAHORE GARRISON UNIVERSITY
1
Instructor name: Munawar Ahmad
 How Many type of loops are in C++ ?
Lahore Garrison University
2
Loops
There are 3 types of loops in C++.
 for loop
 while loop
 do...while loop
Lahore Garrison University
3
 What is the difference between For and While Loop?
Lahore Garrison University
4
for vs while loops
Lahore Garrison University
5
A for loop is usually used when the number of
iterations is known. For example,
// This loop is iterated 5 times
for (int i = 1; i <=5; ++i) {
// body of the loop
}
Here, we know that the for-loop will be
executed 5 times.
However, while and do...while loops are usually
used when the number of iterations is unknown.
For example,
while (condition) {
// body of the loop
}
C++ while
C++ while Loop
The syntax of the while loop is:
while (condition) {
// body of the loop
}
Lahore Garrison University
6
A while loop evaluates the condition
If the condition evaluates to true, the code inside the while
loop is executed.
The condition is evaluated again.
This process continues until the condition is false.
When the condition evaluates to false, the loop terminates.
Flowchart of while Loop
Lahore Garrison University
7
Example 1: Display Numbers from 1 to 5
Iteration Variable i <= 5 Action
1st i = 1 true 1 is printed and i is increased to 2.
2nd i = 2 true 2 is printed and i is increased to 3.
3rd i = 3 true 3 is printed and i is increased to 4
4th i = 4 true 4 is printed and i is increased to 5.
5th i = 5 true 5 is printed and i is increased to 6.
6th i = 6 falseThe loop is terminated
Lahore Garrison University
8
#include <iostream>
using namespace std;
int main() {
int i = 1;
while (i <= 5) {
cout << i << " ";
++i;
}
return 0;
}
Lahore Garrison University
9
Sum of Positive Numbers Only
// program to find the sum of positive numbers
// if the user enters a negative number, the loop ends
// the negative number entered is not added to the sum
#include <iostream>
using namespace std;
int main() {
int number;
int sum = 0;
// take input from the user
cout << "Enter a number: ";
cin >> number;
while (number >= 0) {
// add all positive numbers
sum += number;
// take input again if the number is positive
cout << "Enter a number: ";
cin >> number;
}
// display the sum
cout << "n The sum is " << sum << endl;
return 0;
}
In this program, the user is prompted to enter a
number, which is stored in the variable number.
In order to store the sum of the numbers, we
declare a variable sum and initialize it to the
value of 0.
The while loop continues until the user enters a
negative number. During each iteration, the
number entered by the user is added to the sum
variable.
When the user enters a negative number, the
loop terminates. Finally, the total sum is
displayed.
C++ do...while Loop
Lahore Garrison University
10
The do...while loop is a variant of the
while loop with one important
difference: the body of do...while loop
is executed once before the condition
is checked.
Its syntax is:
do {
// body of loop;
}
while (condition);
 The body of the loop is executed at first. Then the
condition is evaluated.
 If the condition evaluates to true, the body of the loop
inside the do statement is executed again.
 The condition is evaluated once again.
 If the condition evaluates to true, the body of the loop
inside the do statement is executed again.
 This process continues until the condition evaluates to
false. Then the loop stops.
Flowchart of do...while Loop
Lahore Garrison University
11
Display Numbers from 1 to 5
Lahore Garrison University
12
// C++ Program to print numbers from
1 to 5
#include <iostream>
using namespace std;
int main() {
int i = 1;
// do...while loop from 1 to 5
do {
cout << i << " ";
++i;
}
while (i <= 5);
return 0;
}
Iteration Variablei <= 5 Action
i = 1 not checked 1 is printed and i is increased to 2
1st i = 2 true 2 is printed and i is increased to 3
2nd i = 3 true 3 is printed and i is increased to 4
3rd i = 4 true 4 is printed and i is increased to 5
4th i = 5 true 5 is printed and i is increased to 6
5th i = 6 false The loop is terminated
Lahore Garrison University
13
Sum of Positive Numbers Only
// program to find the sum of positive numbers
// If the user enters a negative number, the loop ends
// the negative number entered is not added to the sum
#include <iostream>
using namespace std;
int main() {
int number = 0;
int sum = 0;
do {
sum += number;
// take input from the user
cout << "Enter a number: ";
cin >> number;
}
while (number >= 0);
// display the sum
cout << "nThe sum is " << sum << endl;
return 0;
}
For Loop
Lahore Garrison University
14
The syntax of for-loop is:
for (initialization; condition; update)
{
// body of-loop
}
initialization 
initializes variables and is executed only once
condition 
if true, the body of for loop is executed
if false, the for loop is terminated
update 
updates the value of initialized variables and again checks the condition
Flowchart of for Loop in C++
Lahore Garrison University
15
Printing Numbers From 1 to 5
Lahore Garrison
University
16
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; ++i) {
cout << i << " ";
}
return 0;
}
Display a text 5 times
Lahore Garrison University
17
// C++ Program to display a text 5 times
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; ++i) {
cout << "Hello World! " << endl;
}
return 0;
}
Program to get the multiplication table of a given
number in the m*n format using nested for loop.
 #include <iostream>
 int main () {
 using namespace std;
 int m;
 int n;
 for (m = 10; m <= 11; m++)
 {
 cout << "Table of " << m << endl;
 for (n = 1; n <= 10; n++)
 {cout << m << "*" << n << "=" << (m*n) << endl;}
 }
 return 0;
 } Lahore Garrison University
18
Sum of Positive Numbers Only by using
break
 #include <iostream>
 using namespace std;
 int main() {
 int number = 0, sum = 0;
 for (;;) {
 // take input from the user
 cout << "Enter a number: ";
 cin >> number;
 // exit loop if number is negative
 if (number < 0) {
 break; }
 sum += number;
 }
 // display the sum
 cout << "nThe sum is " << sum << endl;
 return 0;
 }
Lahore Garrison University
19
Sum of Positive Numbers (Only lesser than 50) by
using For Loop with Continue
 // program to calculate positive numbers till 50 only
 // if the user enters a negative number,
 // that number is skipped from the calculation
 // negative number -> loop terminate
 // numbers above 50 -> skip iteration
 #include <iostream>
 using namespace std;
 int main() {
 int sum = 0;
 int number = 0;
 while (number >= 0) {
 // add all positive numbers
 sum += number;
 // take input from the user
 cout << "Enter a number: ";
 cin >> number;
 //continue condition
 if (number > 50) {
 cout << "The number is greater than 50 and
won't be calculated." << endl;
 number = 0; // the value of number is made 0
again
 continue;
 }
 }
 // display the sum
 cout << "The sum is " << sum << endl;
 return 0;
 } Lahore Garrison University
20
Write a program in C++ to find the sum of the
series 1 + 1/2^2 + 1/3^3 + ..+ 1/n^n.
 #include <iostream>
 #include <math.h>
 using namespace std;
 int main()
 { double sum = 0, a;
 int n, i;
 cout << " Input the value for nth term: ";
 cin >> n;
 for (i = 1; i <= n; ++i)
 {a = 1 / pow(i, i);
 cout << "1/" << i << "^" << i << " = " << a << endl;
 sum += a; }
 cout << " The sum of the above series is: " << sum << endl;
 } Lahore Garrison University
21
Home Task
 Write a program that uses nested for loop to find the prime numbers from 1 to 100.
 Write a program that uses a for loop to print out the numbers from 1 to 100. For every
number that is divisible by 3, print "Fizz" instead of the number. For every number that
is divisible by 5, print "Buzz" instead of the number. For every number that is divisible
by both 3 and 5, print "Fizz Buzz" instead of the number.
 Write a program that asks the user to enter a number, and then uses a while loop to
print out the multiplication table for that number, from 1 to 10.
 Write a program that asks the user to enter a password, and then uses a do-while loop
to keep asking for the password until the correct password is entered.
 Write a program that asks the user to enter a positive integer, and then uses a do-while
loop to keep asking for a number until the user enters a number that is divisible by 3.
Lahore Garrison University
22
Ad

Recommended

Lec7 - Loops updated.pptx
Lec7 - Loops updated.pptx
NaumanRasheed11
Cs1123 6 loops
Cs1123 6 loops
TAlha MAlik
Iteration
Iteration
Liam Dunphy
Loops
Loops
abdulmanan366
Chapter 3
Chapter 3
Amrit Kaur
Counting and looping
Counting and looping
dcorliss08
12-Lec - Repetition For Loop.pptx
12-Lec - Repetition For Loop.pptx
AqeelAbbas94
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
rohassanie
Iterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop working
Neeru Mittal
Loops c++
Loops c++
Shivani Singh
Computational Physics Cpp Portiiion.pptx
Computational Physics Cpp Portiiion.pptx
khanzasad009
Cse115 lecture08repetitionstructures part02
Cse115 lecture08repetitionstructures part02
Md. Ashikur Rahman
04a intro while
04a intro while
hasfaa1017
MUST CS101 Lab11
MUST CS101 Lab11
Ayman Hassan
C++ Loops General Discussion of Loops A loop is a.docx
C++ Loops General Discussion of Loops A loop is a.docx
humphrieskalyn
Chapter 5 Looping
Chapter 5 Looping
GhulamHussain142878
C++ loop
C++ loop
Khelan Ameen
Overview of c++ language
Overview of c++ language
samt7
Control structures repetition
Control structures repetition
Online
Programming Fundamentals presentation slide
Programming Fundamentals presentation slide
mibrahim020205
SPL 8 | Loop Statements in C
SPL 8 | Loop Statements in C
Mohammad Imam Hossain
9781337102087 ppt ch05
9781337102087 ppt ch05
Terry Yoast
Loops
Loops
International Islamic University
C++ control loops
C++ control loops
pratikborsadiya
Labsheet 5
Labsheet 5
rohassanie
Ch4
Ch4
aamirsahito
C++ control structure
C++ control structure
bluejayjunior
CP Handout#5
CP Handout#5
trupti1976
MOVIE RECOMMENDATION SYSTEM, UDUMULA GOPI REDDY, Y24MC13085.pptx
MOVIE RECOMMENDATION SYSTEM, UDUMULA GOPI REDDY, Y24MC13085.pptx
Maharshi Mallela
Building Geospatial Data Warehouse for GIS by GIS with FME
Building Geospatial Data Warehouse for GIS by GIS with FME
Safe Software

More Related Content

Similar to Introduction to Loops using C++. Exploring While&Do-While Loop (20)

Iterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop working
Neeru Mittal
Loops c++
Loops c++
Shivani Singh
Computational Physics Cpp Portiiion.pptx
Computational Physics Cpp Portiiion.pptx
khanzasad009
Cse115 lecture08repetitionstructures part02
Cse115 lecture08repetitionstructures part02
Md. Ashikur Rahman
04a intro while
04a intro while
hasfaa1017
MUST CS101 Lab11
MUST CS101 Lab11
Ayman Hassan
C++ Loops General Discussion of Loops A loop is a.docx
C++ Loops General Discussion of Loops A loop is a.docx
humphrieskalyn
Chapter 5 Looping
Chapter 5 Looping
GhulamHussain142878
C++ loop
C++ loop
Khelan Ameen
Overview of c++ language
Overview of c++ language
samt7
Control structures repetition
Control structures repetition
Online
Programming Fundamentals presentation slide
Programming Fundamentals presentation slide
mibrahim020205
SPL 8 | Loop Statements in C
SPL 8 | Loop Statements in C
Mohammad Imam Hossain
9781337102087 ppt ch05
9781337102087 ppt ch05
Terry Yoast
Loops
Loops
International Islamic University
C++ control loops
C++ control loops
pratikborsadiya
Labsheet 5
Labsheet 5
rohassanie
Ch4
Ch4
aamirsahito
C++ control structure
C++ control structure
bluejayjunior
CP Handout#5
CP Handout#5
trupti1976
Iterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop working
Neeru Mittal
Computational Physics Cpp Portiiion.pptx
Computational Physics Cpp Portiiion.pptx
khanzasad009
Cse115 lecture08repetitionstructures part02
Cse115 lecture08repetitionstructures part02
Md. Ashikur Rahman
04a intro while
04a intro while
hasfaa1017
MUST CS101 Lab11
MUST CS101 Lab11
Ayman Hassan
C++ Loops General Discussion of Loops A loop is a.docx
C++ Loops General Discussion of Loops A loop is a.docx
humphrieskalyn
Overview of c++ language
Overview of c++ language
samt7
Control structures repetition
Control structures repetition
Online
Programming Fundamentals presentation slide
Programming Fundamentals presentation slide
mibrahim020205
9781337102087 ppt ch05
9781337102087 ppt ch05
Terry Yoast
C++ control structure
C++ control structure
bluejayjunior
CP Handout#5
CP Handout#5
trupti1976

Recently uploaded (20)

MOVIE RECOMMENDATION SYSTEM, UDUMULA GOPI REDDY, Y24MC13085.pptx
MOVIE RECOMMENDATION SYSTEM, UDUMULA GOPI REDDY, Y24MC13085.pptx
Maharshi Mallela
Building Geospatial Data Warehouse for GIS by GIS with FME
Building Geospatial Data Warehouse for GIS by GIS with FME
Safe Software
Complete WordPress Programming Guidance Book
Complete WordPress Programming Guidance Book
Shabista Imam
SAP PM Module Level-IV Training Complete.ppt
SAP PM Module Level-IV Training Complete.ppt
MuhammadShaheryar36
Application Modernization with Choreo - The AI-Native Internal Developer Plat...
Application Modernization with Choreo - The AI-Native Internal Developer Plat...
WSO2
arctitecture application system design os dsa
arctitecture application system design os dsa
za241967
Digital Transformation: Automating the Placement of Medical Interns
Digital Transformation: Automating the Placement of Medical Interns
Safe Software
On-Device AI: Is It Time to Go All-In, or Do We Still Need the Cloud?
On-Device AI: Is It Time to Go All-In, or Do We Still Need the Cloud?
Hassan Abid
Canva Pro Crack Free Download 2025-FREE LATEST
Canva Pro Crack Free Download 2025-FREE LATEST
grete1122g
Zoho Creator Solution for EI by Elsner Technologies.docx
Zoho Creator Solution for EI by Elsner Technologies.docx
Elsner Technologies Pvt. Ltd.
IDM Crack with Internet Download Manager 6.42 Build 41 [Latest 2025]
IDM Crack with Internet Download Manager 6.42 Build 41 [Latest 2025]
pcprocore
Y - Recursion The Hard Way GopherCon EU 2025
Y - Recursion The Hard Way GopherCon EU 2025
Eleanor McHugh
Folding Cheat Sheet # 9 - List Unfolding as the Computational Dual of ...
Folding Cheat Sheet # 9 - List Unfolding as the Computational Dual of ...
Philip Schwarz
Which Hiring Management Tools Offer the Best ROI?
Which Hiring Management Tools Offer the Best ROI?
HireME
Heat Treatment Process Automation in India
Heat Treatment Process Automation in India
Reckers Mechatronics
Decipher SEO Solutions for your startup needs.
Decipher SEO Solutions for your startup needs.
mathai2
HYBRIDIZATION OF ALKANES AND ALKENES ...
HYBRIDIZATION OF ALKANES AND ALKENES ...
karishmaduhijod1
ElectraSuite_Prsentation(online voting system).pptx
ElectraSuite_Prsentation(online voting system).pptx
mrsinankhan01
Foundations of Marketo Engage - Programs, Campaigns & Beyond - June 2025
Foundations of Marketo Engage - Programs, Campaigns & Beyond - June 2025
BradBedford3
Best Practice for LLM Serving in the Cloud
Best Practice for LLM Serving in the Cloud
Alluxio, Inc.
MOVIE RECOMMENDATION SYSTEM, UDUMULA GOPI REDDY, Y24MC13085.pptx
MOVIE RECOMMENDATION SYSTEM, UDUMULA GOPI REDDY, Y24MC13085.pptx
Maharshi Mallela
Building Geospatial Data Warehouse for GIS by GIS with FME
Building Geospatial Data Warehouse for GIS by GIS with FME
Safe Software
Complete WordPress Programming Guidance Book
Complete WordPress Programming Guidance Book
Shabista Imam
SAP PM Module Level-IV Training Complete.ppt
SAP PM Module Level-IV Training Complete.ppt
MuhammadShaheryar36
Application Modernization with Choreo - The AI-Native Internal Developer Plat...
Application Modernization with Choreo - The AI-Native Internal Developer Plat...
WSO2
arctitecture application system design os dsa
arctitecture application system design os dsa
za241967
Digital Transformation: Automating the Placement of Medical Interns
Digital Transformation: Automating the Placement of Medical Interns
Safe Software
On-Device AI: Is It Time to Go All-In, or Do We Still Need the Cloud?
On-Device AI: Is It Time to Go All-In, or Do We Still Need the Cloud?
Hassan Abid
Canva Pro Crack Free Download 2025-FREE LATEST
Canva Pro Crack Free Download 2025-FREE LATEST
grete1122g
Zoho Creator Solution for EI by Elsner Technologies.docx
Zoho Creator Solution for EI by Elsner Technologies.docx
Elsner Technologies Pvt. Ltd.
IDM Crack with Internet Download Manager 6.42 Build 41 [Latest 2025]
IDM Crack with Internet Download Manager 6.42 Build 41 [Latest 2025]
pcprocore
Y - Recursion The Hard Way GopherCon EU 2025
Y - Recursion The Hard Way GopherCon EU 2025
Eleanor McHugh
Folding Cheat Sheet # 9 - List Unfolding as the Computational Dual of ...
Folding Cheat Sheet # 9 - List Unfolding as the Computational Dual of ...
Philip Schwarz
Which Hiring Management Tools Offer the Best ROI?
Which Hiring Management Tools Offer the Best ROI?
HireME
Heat Treatment Process Automation in India
Heat Treatment Process Automation in India
Reckers Mechatronics
Decipher SEO Solutions for your startup needs.
Decipher SEO Solutions for your startup needs.
mathai2
HYBRIDIZATION OF ALKANES AND ALKENES ...
HYBRIDIZATION OF ALKANES AND ALKENES ...
karishmaduhijod1
ElectraSuite_Prsentation(online voting system).pptx
ElectraSuite_Prsentation(online voting system).pptx
mrsinankhan01
Foundations of Marketo Engage - Programs, Campaigns & Beyond - June 2025
Foundations of Marketo Engage - Programs, Campaigns & Beyond - June 2025
BradBedford3
Best Practice for LLM Serving in the Cloud
Best Practice for LLM Serving in the Cloud
Alluxio, Inc.
Ad

Introduction to Loops using C++. Exploring While&Do-While Loop

  • 1. Object Oriented Programming (Lab) Lecture 1-> LOOPS Semester- Spring 2024 LAHORE GARRISON UNIVERSITY 1 Instructor name: Munawar Ahmad
  • 2. How Many type of loops are in C++ ? Lahore Garrison University 2
  • 3. Loops There are 3 types of loops in C++. for loop while loop do...while loop Lahore Garrison University 3
  • 4. What is the difference between For and While Loop? Lahore Garrison University 4
  • 5. for vs while loops Lahore Garrison University 5 A for loop is usually used when the number of iterations is known. For example, // This loop is iterated 5 times for (int i = 1; i <=5; ++i) { // body of the loop } Here, we know that the for-loop will be executed 5 times. However, while and do...while loops are usually used when the number of iterations is unknown. For example, while (condition) { // body of the loop }
  • 6. C++ while C++ while Loop The syntax of the while loop is: while (condition) { // body of the loop } Lahore Garrison University 6 A while loop evaluates the condition If the condition evaluates to true, the code inside the while loop is executed. The condition is evaluated again. This process continues until the condition is false. When the condition evaluates to false, the loop terminates.
  • 7. Flowchart of while Loop Lahore Garrison University 7
  • 8. Example 1: Display Numbers from 1 to 5 Iteration Variable i <= 5 Action 1st i = 1 true 1 is printed and i is increased to 2. 2nd i = 2 true 2 is printed and i is increased to 3. 3rd i = 3 true 3 is printed and i is increased to 4 4th i = 4 true 4 is printed and i is increased to 5. 5th i = 5 true 5 is printed and i is increased to 6. 6th i = 6 falseThe loop is terminated Lahore Garrison University 8 #include <iostream> using namespace std; int main() { int i = 1; while (i <= 5) { cout << i << " "; ++i; } return 0; }
  • 9. Lahore Garrison University 9 Sum of Positive Numbers Only // program to find the sum of positive numbers // if the user enters a negative number, the loop ends // the negative number entered is not added to the sum #include <iostream> using namespace std; int main() { int number; int sum = 0; // take input from the user cout << "Enter a number: "; cin >> number; while (number >= 0) { // add all positive numbers sum += number; // take input again if the number is positive cout << "Enter a number: "; cin >> number; } // display the sum cout << "n The sum is " << sum << endl; return 0; } In this program, the user is prompted to enter a number, which is stored in the variable number. In order to store the sum of the numbers, we declare a variable sum and initialize it to the value of 0. The while loop continues until the user enters a negative number. During each iteration, the number entered by the user is added to the sum variable. When the user enters a negative number, the loop terminates. Finally, the total sum is displayed.
  • 10. C++ do...while Loop Lahore Garrison University 10 The do...while loop is a variant of the while loop with one important difference: the body of do...while loop is executed once before the condition is checked. Its syntax is: do { // body of loop; } while (condition); The body of the loop is executed at first. Then the condition is evaluated. If the condition evaluates to true, the body of the loop inside the do statement is executed again. The condition is evaluated once again. If the condition evaluates to true, the body of the loop inside the do statement is executed again. This process continues until the condition evaluates to false. Then the loop stops.
  • 11. Flowchart of do...while Loop Lahore Garrison University 11
  • 12. Display Numbers from 1 to 5 Lahore Garrison University 12 // C++ Program to print numbers from 1 to 5 #include <iostream> using namespace std; int main() { int i = 1; // do...while loop from 1 to 5 do { cout << i << " "; ++i; } while (i <= 5); return 0; } Iteration Variablei <= 5 Action i = 1 not checked 1 is printed and i is increased to 2 1st i = 2 true 2 is printed and i is increased to 3 2nd i = 3 true 3 is printed and i is increased to 4 3rd i = 4 true 4 is printed and i is increased to 5 4th i = 5 true 5 is printed and i is increased to 6 5th i = 6 false The loop is terminated
  • 13. Lahore Garrison University 13 Sum of Positive Numbers Only // program to find the sum of positive numbers // If the user enters a negative number, the loop ends // the negative number entered is not added to the sum #include <iostream> using namespace std; int main() { int number = 0; int sum = 0; do { sum += number; // take input from the user cout << "Enter a number: "; cin >> number; } while (number >= 0); // display the sum cout << "nThe sum is " << sum << endl; return 0; }
  • 14. For Loop Lahore Garrison University 14 The syntax of for-loop is: for (initialization; condition; update) { // body of-loop } initialization initializes variables and is executed only once condition if true, the body of for loop is executed if false, the for loop is terminated update updates the value of initialized variables and again checks the condition
  • 15. Flowchart of for Loop in C++ Lahore Garrison University 15
  • 16. Printing Numbers From 1 to 5 Lahore Garrison University 16 #include <iostream> using namespace std; int main() { for (int i = 1; i <= 5; ++i) { cout << i << " "; } return 0; }
  • 17. Display a text 5 times Lahore Garrison University 17 // C++ Program to display a text 5 times #include <iostream> using namespace std; int main() { for (int i = 1; i <= 5; ++i) { cout << "Hello World! " << endl; } return 0; }
  • 18. Program to get the multiplication table of a given number in the m*n format using nested for loop. #include <iostream> int main () { using namespace std; int m; int n; for (m = 10; m <= 11; m++) { cout << "Table of " << m << endl; for (n = 1; n <= 10; n++) {cout << m << "*" << n << "=" << (m*n) << endl;} } return 0; } Lahore Garrison University 18
  • 19. Sum of Positive Numbers Only by using break #include <iostream> using namespace std; int main() { int number = 0, sum = 0; for (;;) { // take input from the user cout << "Enter a number: "; cin >> number; // exit loop if number is negative if (number < 0) { break; } sum += number; } // display the sum cout << "nThe sum is " << sum << endl; return 0; } Lahore Garrison University 19
  • 20. Sum of Positive Numbers (Only lesser than 50) by using For Loop with Continue // program to calculate positive numbers till 50 only // if the user enters a negative number, // that number is skipped from the calculation // negative number -> loop terminate // numbers above 50 -> skip iteration #include <iostream> using namespace std; int main() { int sum = 0; int number = 0; while (number >= 0) { // add all positive numbers sum += number; // take input from the user cout << "Enter a number: "; cin >> number; //continue condition if (number > 50) { cout << "The number is greater than 50 and won't be calculated." << endl; number = 0; // the value of number is made 0 again continue; } } // display the sum cout << "The sum is " << sum << endl; return 0; } Lahore Garrison University 20
  • 21. Write a program in C++ to find the sum of the series 1 + 1/2^2 + 1/3^3 + ..+ 1/n^n. #include <iostream> #include <math.h> using namespace std; int main() { double sum = 0, a; int n, i; cout << " Input the value for nth term: "; cin >> n; for (i = 1; i <= n; ++i) {a = 1 / pow(i, i); cout << "1/" << i << "^" << i << " = " << a << endl; sum += a; } cout << " The sum of the above series is: " << sum << endl; } Lahore Garrison University 21
  • 22. Home Task Write a program that uses nested for loop to find the prime numbers from 1 to 100. Write a program that uses a for loop to print out the numbers from 1 to 100. For every number that is divisible by 3, print "Fizz" instead of the number. For every number that is divisible by 5, print "Buzz" instead of the number. For every number that is divisible by both 3 and 5, print "Fizz Buzz" instead of the number. Write a program that asks the user to enter a number, and then uses a while loop to print out the multiplication table for that number, from 1 to 10. Write a program that asks the user to enter a password, and then uses a do-while loop to keep asking for the password until the correct password is entered. Write a program that asks the user to enter a positive integer, and then uses a do-while loop to keep asking for a number until the user enters a number that is divisible by 3. Lahore Garrison University 22