0% found this document useful (0 votes)
23 views20 pages

Unit-2 Nested For Loops D3NMQtjPl7

basics of computer science engineering in cpp or c++ undergrad courtesy SVKM's NMIMS

Uploaded by

nishantparwani
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
0% found this document useful (0 votes)
23 views20 pages

Unit-2 Nested For Loops D3NMQtjPl7

basics of computer science engineering in cpp or c++ undergrad courtesy SVKM's NMIMS

Uploaded by

nishantparwani
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/ 20

Programming Constructs

Nested for loop

By Prof.Avani Bhuva 1
Nested Loop (loop inside loop):
-A loop within another loop is known as nested loop.
Combinations of any loops are possible
Syntax:

Nested For Nested While Nested Do While


for (initialization; condition; update) while (condition) do
{ { {
for (initialization; condition; update) while (condition) do
{ { {
// body of inner loop // body of inner loop // body of inner loop
} } }while (condition);
// body of outer loop // body of outer loop // body of outer loop
} } }while (condition);

By Prof.Avani Bhuva 2
By Prof.Avani Bhuva 3
By Prof.Avani Bhuva 4
By Prof.Avani Bhuva 5
A
BC
DEF
GHIJ
KLMNO
#include<iostream>
using namespace std;
int main()

{
int i,j;
char ch='A';
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
cout<<ch++;
}
cout<<"\n";
}
return 0;
}
By Prof.Avani Bhuva 6
*****
****

Lab Exercise ***


**
*
#include <iostream>
using namespace std;
int main()
{
int i, j, n;
cout << "Enter number of rows: ";
cin >> n;
for(i = n; i >= 1; i--)
{
for(j = 1; j <= i; j++)
{
cout << "* ";
}
// ending line after each row
cout << "\n";
}
return 0;
}
By Prof.Avani Bhuva 7
54321 #include<iostream>
????*
4321 using namespace std;
???**
321 int main()
??***
21 {
?****
1 int n,i,j;
*****
cout<<"Enter the number: ";
#include<iostream>
cin>>n;
using namespace std;
for(i=n;i>=1;i--)
int main()
{
{
for(j=i;j>=1;j--)
int i,j,k;
{
for(i=1;i<=5;i++)
cout<<j;
{
}
for(k=1;k<=5-i;k++)
cout<<"\n";
{
}
cout<<"?";
}
}
for(j=1;j<=i;j++)
{
cout<<"*";
}
cout<<endl;
}
}
By Prof.Avani Bhuva 8
#include<iostream> A #include<iostream> A
using namespace std; using namespace std; bc
A B int main()
int main() DEF
{ A B C { ghij
char i,j; int n,i,j; KLMNO
C B A
int n; cout<<"Enter the number: ";
cout<<"Enter a number: "; B A cin>>n;
cin>>n; A char ch='A';
for(i='A';i<='A'+n-1;i++) for(i=1;i<=n;i++)
{ {
for(j='A';j<=i;j++) for(j=1;j<=i;j++)
{ {
cout<<j; if(i%2!=0)
} {
cout<<"\n"; cout<<ch++;
} }
for(i='A'+n-1;i>='A';i--) else
{ {
for(j=i;j>='A';j--) char C= tolower(ch);
{ cout<< C;
cout<<j; ch++;
} }}
cout<<"\n"; cout<<"\n";
}} }}
By Prof.Avani Bhuva 9
#include<iostream> 1
using namespace std; 10
int main() 101
{ 1010
int n,i,j; 10101
cout<<"Enter the number: ";
cin>>n;

for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
if(j%2==1)
{
cout<<"1";
}
else
{
cout<<"0";
}
}
cout<<"\n";
}
By Prof.Avani Bhuva 10
}
By Prof.Avani Bhuva 11
#include<iostream> for(j=1;j<=i;j++)
using namespace std; {
int main() cout<<"*";
{ }
int i,j,n; cout"\n";
cout<<“enter rows”; }
cin>>n;
return 0;
for(i=1;i<=n;i++)
{
}
for(j=1;j<=n-i;j++)
{
cout<<" ";
}
for(j=1;j<=i;j++)
{
cout<<"*";
}
cout<<"\n";
}
for(i=n-1;i>=1;i--)
{
for(j=1;j<=n-i;j++)
{
cout<<" ";
}
By Prof.Avani Bhuva 12
*
**
***
****
*****
#include<iostream>
using namespace std;
int main()
{
int i,j,n;
cout<<"Enter the number of lines:";
cin>>n;
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
{
cout<<" ";
}
for(j=1;j<=i;j++)
{
cout<<"*";
}
cout<<"\n";
}
return 0;
}
By Prof.Avani Bhuva 13
By Prof.Avani Bhuva 15
By Prof.Avani Bhuva 16
By Prof.Avani Bhuva 17
By Prof.Avani Bhuva 18
By Prof.Avani Bhuva 19
#include<iostream>
using namespace std;
int main() {
int remainders, fact, sum = 0, i, num, copynum;
cout << "enter the number:";
cin >> num;
copynum = num;
while (num != 0)
{
remainders = num % 10;
fact = 1;
/* find the factorial remainders*/
for (i = 1; i <= remainders; i++)
{
fact = fact * i;
}
/* Add factorial of individual digit */
sum = sum + fact;
num = num / 10;}
if (sum == copynum) {
cout << copynum << " is strong numbers";
} else {
cout << copynum << " is not strong numbers";
} By Prof.Avani Bhuva 20
return 0;}
Print Multiplication Table from 1 to 10 LAB EXCERCISE

using namespace std;


int main()
{
int num, i, j, res;
cout<<"-----------Table from 1 to 10--------\n\n";
for(i=1; i<=10; i++)
{
num = i;
cout<<"Table of "<<num<<": ";
for(j=1; j<=10; j++)
{
res = num*j;
cout<<res<<" ";
}
cout<<endl;
}
cout<<endl;
return 0;
}

By Prof.Avani Bhuva 21

You might also like