Untitled

mail@pastecode.io avatar
unknown
plain_text
6 months ago
1.4 kB
2
Indexable
Never
#include <iostream>
#include <cmath>

bool isPerfectSquare(int num) {
    int sqrtNum = sqrt(num);
    return sqrtNum * sqrtNum == num;
}

bool isPalindrome(int num) {
    int reversedNum = 0;
    int originalNum = num;
    while (num > 0) {
        reversedNum = reversedNum * 10 + num % 10;
        num /= 10;
    }
    return originalNum == reversedNum;
}

bool isPrime(int num) {
    if (num <= 1) {
        return false;
    }
    for (int i = 2; i <= sqrt(num); ++i) {
        if (num % i == 0) {
            return false;
        }
    }
    return true;
}

int countDigits(int num) {
    int count = 0;
    while (num > 0) {
        num /= 10;
        ++count;
    }
    return count;
}

int main() {
    int M;
    std::cout << "Nhap so nguyen duong M: ";
    std::cin >> M;

    int digitCount = countDigits(M);
    bool isPerfectSquareNumber = isPerfectSquare(M);
    bool isPalindromeNumber = isPalindrome(M);
    bool isPrimeNumber = isPrime(M);

    std::cout << "So " << M << " co " << digitCount << " chu so." << std::endl;
    std::cout << "La so chinh phuong: " << (isPerfectSquareNumber ? "Co" : "Khong") << std::endl;
    std::cout << "La so doi xung: " << (isPalindromeNumber ? "Co" : "Khong") << std::endl;
    std::cout << "La so nguyen to: " << (isPrimeNumber ? "Co" : "Khong") << std::endl;

    return 0;
}