Docsity

FOR Loop and Examples: Printing Squares, Factorial, Power, and Fibonacci Series, Exercises of Computer Programming

Partial preview of the text

Download FOR Loop and Examples: Printing Squares, Factorial, Power, and Fibonacci Series and more Exercises Computer Programming in PDF only on Docsity!

FOR Loop

General format when using for loops

for ( initialization; LoopContinuationTest; increment ) statement

Example

for( int counter = 1; counter <= 10; counter++ ) cout << counter << endl;

Prints integers from one to ten

No semicol- on after last statement

// fordemo.cpp // demonstrates simple FOR loop #include using namespace std; int main() { int j;//define a loop variable for(j=0; j<15; j++)//loop from 0 to 14 cout << j * j << โ€œ โ€œ;//displaying the square of j cout << endl; return 0; }

Lab Task 1

Write a program that calculates Factorial up to the

number entered by the user.?

Lab Task 3

Generate first n Fibonacci series using for loop.

Lab Task 4

๎€Š Assume you want to generate a table of multiples of any given

number. Write a program that allows the user to enter the

number, and then generates the table, formatting it into 10

columns and 20 lines. Interaction with the program should look

like this (only the first three lines are shown):

Enter a number: 7