-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvid16.cpp
118 lines (114 loc) · 2.21 KB
/
vid16.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <iostream>
#include <string>
using namespace std;
int hexadecimalToDecimal(string n)
{
int ans = 0;
int base = 1;
int s = n.size();
for (int i = s - 1; i >= 0; i--)
{
if (n[i] >= '0' && n[i] <= '9')
{
ans = ans + base * (n[i] - '0');
}
if (n[i] >= 'A' && n[i] <= 'F')
{
ans = ans + base * (n[i] - 'A' + 10);
}
base = base * 16;
}
return ans;
}
int octaToDecimal(int n)
{
int ans = 0;
int base = 1;
while (n > 0)
{
int lastDigit = n % 10;
ans = ans + base * lastDigit;
base = base * 8;
n = n / 10;
}
return ans;
}
int binaryToDecimal(int n)
{
int ans = 0;
int base = 1;
while (n > 0)
{
int lastDigit = n % 10;
ans = ans + base * lastDigit;
base = base * 2;
n = n / 10;
}
return ans;
}
bool check(int x, int y, int z)
{
int a = max(x, max(y, z));
int b, c;
if (a == x)
{
b = y, c = z;
}
else if (a == y)
{
b = x, c = z;
}
else if (a == z)
{
b = x, c = y;
}
if (a * a == b * b + c * c)
{
return true;
}
else
{
return false;
}
}
int sum(int num)
{
int sum;
sum = (num * (num + 1)) / 2;
return sum;
}
int main()
{
// Sum of first n natural numbers
int n;
cin >> n;
cout << "The sum of first " << n << " natural numbers is " << sum(n) << "." << endl;
// Checking for Pythagorean Triplet
int x, y, z;
cout << "\nEnter 3 numbers respectively:\n";
cin >> x >> y >> z;
if (check(x, y, z))
{
cout << "Pythagorean Triplet.\n";
}
else
{
cout << "Not a Pythagorean Triplet.\n";
}
// Binary to Decimal
int binary;
cout << "\nEnter a Binary Number:\n";
cin >> binary;
cout << binaryToDecimal(binary) << endl;
// Octal to Decimal
int octal;
cout << "\nEnter a Octal Number:\n";
cin >> octal;
cout << octaToDecimal(octal) << endl;
// hexadecimal to Decimal
string hexaDeci;
cout << "\nEnter a hexadecimal Number:\n";
cin >> hexaDeci;
cout << hexadecimalToDecimal(hexaDeci) << endl;
return 0;
}