Unit-2 Nested For Loops D3NMQtjPl7
Unit-2 Nested For Loops D3NMQtjPl7
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:
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
*****
****
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
By Prof.Avani Bhuva 21