diff --git a/funzioni/descrizione_numero.cpp b/funzioni/descrizione_numero.cpp new file mode 100644 index 0000000..11851b5 --- /dev/null +++ b/funzioni/descrizione_numero.cpp @@ -0,0 +1,95 @@ +/* +AUTORE: Manuel Vichi +Descrizione di un numero +*/ +#include +#include +#include +using namespace std; +//Function to calculate the square of a number (because why not??) +int square(int x) { + return x * x; +} + +//Boolean function to determinate if a number is automorphic +bool isAutomorphic(int num) { + int temp = num; + int digits = 0; + while (temp > 0) { + digits++; + temp /= 10; + } + int lastDigits = square(num) % static_cast(pow(10, digits)); + return lastDigits == num; +} + +//Function to check if a number is prime +bool isPrimeNumber(int num) { + if (num <= 1) + return false; + for (int i = 2; i * i <= num; i++) { + if (num % i == 0) + return false; + } + return true; +} + +//Function to check if a number is perfect +bool isPerfectNumber(int n) { + if (n <= 1) + return false; + int sum = 0; + for (int i = 1; i <= n / 2; ++i) + if (n % i == 0) + sum += i; + return sum == n; +} + +//Function to reverse the number +int reverseNumber(int number) { + int original = number; + int reversed = 0; + while (number > 0) { + int digit = number % 10; + reversed = reversed * 10 + digit; + n /= 10; + } + return reversed; +} + +//Function to check if a number is palindrome +bool isPalindrome(int n) { + int original = n; + int reversed = reverseNumber(n); + return original == reversed; +} + + +//Main function, asks in input the number and prints if it's automorphic or not +int main() { + int numero; + cout << "Inserisci il numero: "; + cin >> numero; + cout << endl; + cout << "Il numero " << numero << " fornito in input ha le seguenti caratteristiche:" << endl; + if (isAutomorphic(numero)) + cout << "Il numero e' automorfo" << endl; + else + cout << "Il numero non e' automorfo" << endl; + if (isPrimeNumber(numero)) + cout << "Il numero e' primo" << endl; + else + cout << "Il numero non e' primo" << endl; + if (isPerfectNumber(numero)) + cout << "Il numero e' perfetto" << endl; + else + cout << "Il numero non e' perfetto" << endl; + if (isPalindrome(numero)) + cout << "Il numero e' palindromo" << endl; + else + cout << "Il numero non e' palindromo" << endl; + cout << endl; //Just to separate the output of the program + cout << "Il numero " << numero << " invertito e': " << reverseNumber(numero) << endl; + system("pause"); //Needed on Windows to display the console output if the executable is not runned from a console + return 0; +}