Credits

Wednesday, February 11, 2009

Loop Applications

C or C++ has three (3) loop structures : for, while and do while statements. Let us dissect for statement first:

General syntax:

for (initialization;condition;increment/decrement)

To display a series of 1 to 100 using for statement, it may look like this:
int x;

for (x=1;x<=100;x++)
printf(" %d ",x);


Check this program below to display a number and its square and cube.



[+/-] show/hide




#include stdio.h /* use <> in between stdio.h*/
main()
{
int num,square,cube;
clrscr();
for(num=1;num<10;num++)
{
square=num*num;
cube=num*num*num;
printf("%d\t%d\t%d\n",num,square,cube);
}
getch();
}

Translate this into Bloodshed C++.

AddThis