Self-Test Program 1
Demonstration Self-Test Program 1 written in ANSI Standard C++
/****************************************************/
/* Source: prereq01.cpp */
/* */
/* Purpose: Demonstration self-test program #1. */
/* Written in ANSI Standard C++ */
/****************************************************/
#include <iostream>
using namespace std;
int main(int argc, char **argv)
{
int x;
int i; /* Loop counter */
/* Input an integer from the user between 5 and 25 */
x = 0; /* Initialize the variable */
while((x < 5) || (x > 25)) /* Make the user input a number in the correct range */
{
cout << "Please input a number from 5 to 25: ";
cin >> x;
if((x < 5) || (x > 25))
cout << "\nNumber must be from 5 to 25. Try again\n";
}
cout << "\n\n"; /* Print a couple of blank spaces */
/* Print in for loop */
for(i=0; i < x; i++)
{
cout << "Printing with for..." << i << "\n";
}
/* Print in while loop */
i = 0;
while(i < x)
{
cout <<"Printing with while..." << i << "\n";
i++;
}
/* Print in do...while loop */
i = 0;
do
{
cout << "Printing with do...while..." << i << "\n";
i++;
}
while(i < x);
return 0;
}
Demonstration Self-Test Program 1 written in ANSI Standard C
/****************************************************/
/* Source: prereq01.c */
/* */
/* Purpose: Demonstration self-test program #1. */
/* Written in ANSI Standard C */
/****************************************************/
#include <stdio.h>
int main(int argc, char **argv)
{
int x;
int i; /* Loop counter */
/* Input an integer from the user between 5 and 25 */
x = 0; /* Initialize the variable */
while((x < 5) || (x > 25)) /* Make the user input a number in the correct range */
{
printf("Please input a number from 5 to 25: ");
scanf("%d", &x);
if((x < 5) || (x > 25))
printf("\nNumber must be from 5 to 25. Try again\n");
}
printf("\n\n"); /* Print a couple of blank spaces */
/* Print in for loop */
for(i=0; i < x; i++)
{
printf("Printing with for...%d\n", i);
}
/* Print in while loop */
i = 0;
while(i < x)
{
printf("Printing with while...%d\n", i);
i++;
}
/* Print in do...while loop */
i = 0;
do
{
printf("Printing with do...while...%d\n", i);
i++;
}
while(i < x);
return 0;
}