Experiment 05
Aim: Programming using nested loops.
Task 1:Write a program to print following pattern using nested
A
BC
DEF
GHIJ
KLMNO
#include <iostream>
using namespace std;
int main()
{
int n, i, j, c = 65;
cout << "Enter the number of rows(5): ";
cin >> n;
for (i = 1; i <= n; i++)
{
for (j = 1; j <= i; j++, c++)
{
cout << (char)c;
}
cout << endl;
}
return 0;
}
Task 2: Write a program to print following pattern using nested loop.
*****
****
***
**
*
#include <iostream>
using namespace std;
int main()
{
int n, i, j;
cout << "Enter the number of rows: ";
cin >> n;
for (i = n; i >= 1; i--)
{
for (j = 1; j <= i; j++)
cout << "*";
cout << endl;
}
return 0;
}
Task 3: Write a program to print following pattern using nested loop, read number of lines to be displayed from user.
*
**
***
****
*****
#include <iostream>
using namespace std;
int main()
{
int n, i, j;
cout << "Enter the number of rows: ";
cin >> n;
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n - i + 1; j++)
cout << " ";
for (j = 1; j <= i; j++)
cout << "*";
cout << endl;
}
return 0;
}
Task 4: Write a program to print following pattern using nested loop.
$
$ $
$ $ $
#include <iostream>
using namespace std;
int main()
{
int n, i, j;
cout << "Enter the number of rows: ";
cin >> n;
for (i = 1; i <= 3; i++)
{
for (j = 1; j <= 3 - i; j++)
cout << " ";
for (j = 1; j <= i; j++)
cout << "$ ";
cout << endl;
}
return 0;
}
Task 5: Write a program to print following pattern using nested loop.
1
1 2
1 2 3
3 2 1
2 1
1
#include <iostream>
using namespace std;
int main()
{
int n, i, j;
cout << "Enter the number: ";
cin >> n;
for (i = 1; i <= n; i++)
{
for (j = 1; j <= i; j++)
cout << j;
cout << endl;
}
for (i = n; i >= 1; i--)
{
for (j = i; j >= 1; j--)
cout << j;
cout << endl;
}
return 0;
}
Task 6: Write a C++ program to print Armstrong numbers between N1 to N2, where N2>N1.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int no1, no2, i, y, n, x, j, sum = 0;
cout << "Enter the number from where to start: ";
cin >> no1;
cout << "Enter the number where to end: ";
cin >> no2;
for (i = no1; i <= no2; i++)
{
n = i;
x = 0;
while (n != 0)
{
n = n / 10;
x++;
}
n = i;
sum = 0;
for (j = 1; j <= x; j++)
{
y = n % 10;
sum = sum + pow(y, x);
n = n / 10;
}
if (sum == i)
{
cout << i << " is an Armstrong number." << endl;
}
}
return 0;
}
Task 7: Write a C++ program to print prime numbers between N1 to N2, where N2>N1.
#include <iostream>
using namespace std;
int main()
{
int n1, n2, i, j;
cout << "Enter the numbers N1 & N2:" << endl;
cin >> n1 >> n2;
cout << endl;
if (n1 > n2)
cout << "Invalid Input";
else
{
for (; n1 < n2; n1++)
{
if (n1 == 1)
continue;
for (i = 2, j = 0; i <= (n1 / 2); i++)
{
if (n1 % i == 0)
{
j = 1;
break;
}
}
if (j == 0)
cout << n1 << endl;
}
}
return 0;
}
Task 8: WAP to generate all combinations of 1, 2 & 3 using for loop.
#include <iostream>
using namespace std;
int main()
{
int i, j, k;
for (i = 1; i <= 3; i++)
for (j = 1; j <= 3; j++)
for (k = 1; k <= 3; k++)
cout << i << j << k << " ";
return 0;
}