Exponents of 2
This program uses basic loops to display values on screen.
AI
Riepilogo AI: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.
Codice sorgente
// This program outputs a list of exponents of 2 based on limits specified by user
#include <iostream.h>
#include <math.h>
#include <iomanip.h>
int main()
{
unsigned long int lowExp, highExp; // initializes limits for exponent table
unsigned long int power, answer; // initializes the variables for power and result
cout << "This program will ask you for limits, then outputs a list of powers of 2.\n\n";
// asks for input for low power
cout << "Enter low exponent from 0 to 20: ";
cin >> lowExp;
// loop that repeats as long as lowExp is under minimum 0 or over maximum 20
while ((lowExp < 0) || (lowExp > 20))
{
cout << "Try again\n";
cout << endl << "Enter low exponent from 0 to 20: ";
cin >> lowExp;
}
// asks for input of high power
cout << endl << "Enter high exponent from 0 to 20: ";
cin >> highExp;
// loop that repeats as long as highExp is under minimum 0 or maximum 20
while((highExp < 0) || (highExp > 20) || (highExp < lowExp))
{
cout << "Try again\n";
cout << endl << "Enter high exponent from 0 to 20: ";
cin >> highExp;
}
cout << endl << " " << setw(2) << "X" << " 2^X" << endl;
cout << " " << "==" << " =======" << endl;
power = lowExp;
/* loop that begins output of powers of 2,
repeats until reaches specified high limit */
while(power <= highExp)
{
answer = pow(2,power);
cout << " " << setw(2) << power << " " << setw(7) << answer << endl;
power = power + 1;
}
return 0;
}
Commenti originali (3)
Recuperato da Wayback Machine