-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogram.cpp
77 lines (74 loc) · 1.43 KB
/
program.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <sstream>
#include <string>
#include <cmath>
using namespace std;
bool isInt(double num)
{
return floor(num) == num;
}
bool isRational(const string &input)
{
return input.find('/') != string::npos;
}
bool isNatural(double num)
{
return isInt(num) && num > 0;
}
bool isWhole(double num)
{
return isInt(num) && num >= 0;
}
bool isIrrational(double num)
{
return !isInt(num) && !isRational(to_string(num));
}
int main()
{
string input;
double num;
cout << "Enter a Number : ";
cin >> input;
if (isRational(input))
{
istringstream ss(input);
double numerator, denominatior;
char slash;
ss >> numerator >> slash >> denominatior;
if (slash == '/' && denominatior != 0)
{
num = numerator / denominatior;
}
else
{
cout << " Invalid input " << endl;
return 1;
}
}
else
{
num = stod(input);
}
cout << " The number " << input << " is: " << endl;
if (isNatural(num))
{
cout << "- A Natural number" << endl;
}
if (isWhole(num))
{
cout << "- A Whole number" << endl;
}
if (isInt(num))
{
cout << "- An Integer" << endl;
}
else if (!isIrrational(num))
{
cout << "- A Rational number" << endl;
}
else
{
cout << "- An Irrational number" << endl;
}
return 0;
}