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

Examples of using for loops in c++ programming to print the squares of numbers, calculate factorials, find the value of one number raised to the power of another, and generate the first n fibonacci series. It also includes a lab task to generate a table of multiples of a given number.

Typology: Exercises

2011/2012

Uploaded on 07/31/2012

dhairya
dhairya 🇮🇳

5

(4)

32 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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
docsity.com
pf3
pf4
pf5
pf8

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