0% found this document useful (0 votes)
110 views

C++ Nested Loop

This document provides examples of using for loops and nested for loops in C++. It begins with objectives and explanations of for loops and nested loops. It then provides 5 examples of using for loops to print numbers, solve a series, and get user input. It also gives 4 examples of nested loops to print multiplication tables and shapes. The full examples section combines all the previous examples into one program.

Uploaded by

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

C++ Nested Loop

This document provides examples of using for loops and nested for loops in C++. It begins with objectives and explanations of for loops and nested loops. It then provides 5 examples of using for loops to print numbers, solve a series, and get user input. It also gives 4 examples of nested loops to print multiplication tables and shapes. The full examples section combines all the previous examples into one program.

Uploaded by

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

IT college First Semester

2019/2020

C++ Lab
Nested Loop

Objectives:
Student should be able to:

1. Learn how to use the repetition structures


- For loop
2. Learn and understand the effect of break and continue Statement

 For Loop:

Example #1:

Write a c++ program to print the numbers from 0 to 10 using for loop

#include <iostream>
using namespace std;
void main(){

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


cout << "i=" << i<<endl;
}

Exercise:

Write a c++ program to print the numbers from 10 to 0 using for loop

Notes:

for (;;) Infinite Loop


for (int x=1;x==0;) Infinite Loop
for (int x=1,x>=1,x++) Error

1|Page
Example #2:

Write a c++ program to print the even numbers from 10 to 0 using for loop

#include <iostream>
using namespace std;
void main(){

for (int x = 10; x >= 1; x -= 2){


cout << "num (x) =" << x << endl;
}

Example #3:

Write a c++ program to solve the following series: 5 + 8 + 11 + .... + 50

#include <iostream>
using namespace std;
void main(){

int sum = 0;
for (int i = 5; i <= 50; i += 3) {
sum += i;
}
cout << "The sum is: " << sum << endl;

Example #4:

Write a c++ program asks the user to enter 5 characters using for loop, if the
user enters 'n' exit the loop.
#include <iostream>
using namespace std;
void main(){
char ch;
for (int i = 0; i <= 5; i++){
cout << "Enter charactar " << i <<" :";
cin >> ch;
if (ch == 'n')
break;
}

Example #5:

2|Page
Write a c++ program to print numbers from 10 to 0 using for loop except the
number 5
#include <iostream>
using namespace std;
void main(){
for (int n = 10; n>0; n--) {
if (n == 5)
continue;
cout << n << ", ";
}
}

 Nested Loop:

Example #1:

Write a c++ program to print the multiplication table of the numbers from 0 to
5

#include <iostream>
using namespace std;
void main(){
for (int multi = 0; multi <= 5; multi++){
for (int multi2 = 0; multi2 <= 10; multi2++){
cout << multi << "*" << multi2 << "= "<<multi *multi2 <<
endl;
}
cout << endl;
}

Example#2:

Write a c++ program to print the following shape

#include <iostream>
using namespace std;
void main(){

for (int x = 1; x <= 5; x++)

3|Page
{
for (int y = 1; y <= x; y++)
cout << "*";
cout << endl;
}

Example #3:

Write a c++ program to print the following shape

#include <iostream>
using namespace std;
void main(){

for (int row = 4; row >= 0; row--)


{

for (int space = row; space < 4; space++)


cout << " " << " ";
for (int i = row; i > 0; i--)
cout << " " << "*";
cout << endl;

}
}

4|Page
Example #4:

Write a c++ program to print the following shape

#include <iostream>
using namespace std;

void main(){

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


{
for (int j = 5; j>i; j--)
cout << ' ';
for (int k = 1; k<2 * i; k++)
cout << '*';
cout << endl;
}
}

In Lab work:
Using the previous example, write a c++ program to print the following shape

5|Page
 Full example:

#include <iostream>
using namespace std;

void main(){

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


cout << "i=" << i << endl;
}

cout << "--------------------------------------------------------------------" <<


endl;

for (int x = 10; x >= 1; x -= 2){


cout << "num (x) =" << x << endl;
}

cout << "--------------------------------------------------------------------" <<


endl;

int sum = 0;
for (int i = 5; i <= 50; i += 3) {
sum += i;
}
cout << "The sum is: " << sum << endl;

cout << "--------------------------------------------------------------------" <<


endl;

char ch;
for (int i = 0; i <= 5; i++){
cout << "Enter charactar " << i << " :";
cin >> ch;
if (ch == 'n')
break;
}

6|Page
cout << "--------------------------------------------------------------------" <<
endl;

for (int n = 10; n>0; n--) {


if (n == 5)
continue;
cout << n << ", ";
}
cout << endl;

cout << "--------------------------------------------------------------------" <<


endl;

for (int multi = 0; multi <= 5; multi++){


for (int multi2 = 0; multi2 <= 10; multi2++){
cout << multi << " * " << multi2 << " = " << multi *multi2 << endl;
}
cout << endl;
}

cout << "--------------------------------------------------------------------" <<


endl;

for (int x = 1; x <= 5; x++)


{
for (int y = 1; y <= x; y++)
cout << "*";
cout << endl;
}

cout << "--------------------------------------------------------------------" <<


endl;

for (int row = 4; row >= 0; row--)


{

for (int space = 4; space > row; space--)


cout << " ";
for (int i = 1; i <=row; i++)
cout << "*";
cout << endl;

cout << "--------------------------------------------------------------------" <<


endl;

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


{
for (int j = 5; j>i; j--)
cout << " ";
for (int k = 1; k<2 * i; k++)
cout << "*";
cout << endl;
}
cout << "--------------------------------------------------------------------" <<
endl;

7|Page
Good Luck 

8|Page

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