Docsity

Nested For Loop-Introduction to Programming-Lab Mannual, Exercises of Computer Programming

Partial preview of the text

Download Nested For Loop-Introduction to Programming-Lab Mannual and more Exercises Computer Programming in PDF only on Docsity!

Nested For Loop

๎€Š

Several loops can be used for a specified Task

๎€Š

for(int j = 0 ; j < 5 ; j++)

for(int k = 0 ; k < 4 ; k++)

Use Nested for Loops to Show this on the

output Screen

Lab Task

๎€Š Use for loops to construct a program that displays the following

two patterns. The number of rows for both patterns will be same

and will be taken as an input from the user. Pattern b should be

shown bellow pattern a

Pattern (a) Pattern (b)









while

Repetition Structure

Example

int product = 2; while ( product <= 1000 ) product = 2 * product; product <= 1000 (^) product = 2 * product true false

For vs While

๎€Š

The for loop does something a fixed number of

times. What happens if you donโ€™t know how

many times you want to do something before

you start the loop?

// while4.cpp // prints numbers raised to fourth power #include #include //for setw using namespace std; int main() { int pow=1;//power initially 1 int numb=1;//numb goes from 1 to ??? while( pow<9999 )//loop while power <= 4 digits { cout << setw(2) << numb;//display number cout << setw(5) << pow << endl;//display fourth power numb = numb + 1;//get ready for next power pow = numbnumbnumb*numb;//calculate fourth power } cout << endl; return 0; docsity.com

Lab Task

Write a program to find the sum of the following

series:

Lab Task

๎€Š

Write a Program to calculate the number of

digits in a user entered number.

Lab Task

๎€Š

Write a Program to make a square whose

boundaries are made of * and is empty from

inside, its length and width will be entered by

user