-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharmstrong.cpp
More file actions
42 lines (40 loc) · 791 Bytes
/
armstrong.cpp
File metadata and controls
42 lines (40 loc) · 791 Bytes
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
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
bool armstrong(int n)
{
string s = to_string(n);
int potenza = s.size();
int risultato = 0;
for(int i = 0; i < potenza; i++)
{
string s2 = s.substr(i,1);
int singola_cifra = stoi(s2);
risultato = risultato + pow(singola_cifra, potenza);
}
if(risultato == n)
{
return true;
}
else
{
return false;
}
}
int main()
{
int n;
cout<<"inserisci un numero: ";
cin>>n;
bool armstrong_number= armstrong(n);
if(armstrong_number == true)
{
cout<<"il numero "<<n<<" e' un numero di Armstrong!!!";
}
else
{
cout<<"il numero "<<n<<" NON e' un numero di Armstrong!!!";
}
return 0;
}