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

Nested Loops: ND RD TH TH

This document discusses nested loops and provides examples of code using nested loops. It contains 5 examples of nested loop programs: 1. A program to print numbers 1 to 20 in a 4x5 matrix. 2. A program that prints asterisks in increasing quantities on successive lines. 3. A program that prints a single line of asterisks. 4. A program that prints the 2nd through 5th powers of numbers from 1 to N in a table. 5. A program that prints the multiplication table for numbers from 1 to N.

Uploaded by

Krit Jimenez
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Nested Loops: ND RD TH TH

This document discusses nested loops and provides examples of code using nested loops. It contains 5 examples of nested loop programs: 1. A program to print numbers 1 to 20 in a 4x5 matrix. 2. A program that prints asterisks in increasing quantities on successive lines. 3. A program that prints a single line of asterisks. 4. A program that prints the 2nd through 5th powers of numbers from 1 to N in a table. 5. A program that prints the multiplication table for numbers from 1 to N.

Uploaded by

Krit Jimenez
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

NESTED LOOPS

NESTING is the process of placing one or more loop structures inside the body of another loop structure. The inner loop is evaluated first. For one iteration of the outer loop, all iterations of the inner loop must be finished first before the outer loop can process its next iteration. When the inner loop is reentered, the inner loops variable is reset to its initial value. Ex. 1.) Write a program segment to print the numbers 1 to 20 in a 4 x 5 matrix shown below : a.) 1 6 11 16 2 7 12 17 3 8 13 18 4 9 14 19 5 10 15 20 b.) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

2.)Give the output of the following program segment : int x, y; for(x= 1; x<= 5; x++) { for (y = 1; y <=x ; y++) cout<<"*"; cout<<endl; }

3.) Write a program segment to print the following output : * * * * * * * * * * * * * * *

4.)Write a program segment that will print in tabulated format the 2nd, 3rd, 4th and 5th power of numbers, from 1 to N, where N is to be entered from the keyboard. Print first the header of each tabulated column. double x, y; int N; cout<<"Enter the value for N :"; cin>>N; cout<<"N \t N^2 \t N^3 \t N^4 \t N^5 \n"; for(x= 1; x<= N; x++) { for (y = 1; y <=5 ; y++) cout<<pow(x,y)<<" \t "; cout<<endl; }

5.) Write a program that will print the Multiplication Table for numbers from 1 to N, where N is a number to be entered from the keyboard. Sample Input/output 1 2 3 4 5 2 4 6 8 10 3 6 9 12 15 Enter the number : 5 4 8 12 16 20 5 10 15 20 25

You might also like