-
Notifications
You must be signed in to change notification settings - Fork 11
/
e.cpp
33 lines (33 loc) · 1.71 KB
/
e.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/****************
*Finding e to the Nth Digit
*By Tom Biju
****************/
#include <iostream>
#include <iomanip> //for setting precision
#include <stdlib.h>
using namespace std;
//Program to find PI to a specified number of decimal places
int main(int argc, char* argv[]){
double e=2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251664274; //e to 100 digits
int decimalPlaces; //integer variable to store the number of decimal places
char* commandLine=argv[1]; //allows for user input from the command line
if(commandLine==NULL){ //if nothing is entered on the command line, then...
cout<<"Enter how many decimal places of e you would like to see (limit 30): "<<endl; //prompt the user for the number of decimal places, up to 30
cin>>decimalPlaces; //store the user input into the variable decimalPlaces
while(decimalPlaces<0||decimalPlaces>30){ //while loop to prompt user for new integer if a valid one was notentered
cout<<"Please enter an integer from 0-30"<<endl;
cin>>decimalPlaces;
}
decimalPlaces+=1; //add one to the decimal places digits since the program starts with the one number before the decimal place
}
else{
decimalPlaces=atoi(commandLine); //if something was entered on the command line, we will convert the string input into an integer
while(decimalPlaces<0||decimalPlaces>30){ //while loop if the input on the command line was not in the valid range
cout<<"Please enter an integer from 0-30"<<endl;
cin>>decimalPlaces;
}
decimalPlaces+=1; //add one to account for the number before the decimal place
}
cout<<setprecision(decimalPlaces)<<e<<endl; //display e to the number stored in decimalPlaces
return 0; //END
}