How to use functions in C language to find Armstrong numbers.

The Narcissistic number is a three-digit number in which the sum of the cubes of its digits is equal to the number itself. Below is an example code using a function to find Narcissistic numbers.

#include <stdio.h>

int isArmstrongNumber(int num) {
    int originalNum, remainder, result = 0;

    originalNum = num;

    while (originalNum != 0) {
        remainder = originalNum % 10;
        result += remainder * remainder * remainder;
        originalNum /= 10;
    }

    if (result == num) {
        return 1;
    } else {
        return 0;
    }
}

int main() {
    int num;
    printf("请输入一个3位数:");
    scanf("%d", &num);

    if (isArmstrongNumber(num)) {
        printf("%d 是水仙花数\n", num);
    } else {
        printf("%d 不是水仙花数\n", num);
    }

    return 0;
}

After running the program, input a 3-digit number and the program will determine if it is an Armstrong number and display the result.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds