Home > Backend Development > C#.Net Tutorial > How to write the narcissus number in C language code

How to write the narcissus number in C language code

angryTom
Release: 2020-03-03 17:36:01
Original
22745 people have browsed it

How to write the narcissus number in C language code

How to write the narcissus number in c language code

The narcissistic number is also known as the supercomplete numerical invariant number. pluperfect digital invariant (PPDI), narcissistic number, autoexponential number, Armstrong number or Armstrong number (Armstrong number), the daffodil number refers to a 3-digit number in which each digit is raised to the third power The sum is equal to itself (for example: 1^3 5^3 3^3 = 153).

Recommended learning: c language video tutorial

The following is the code to find the number of daffodils using C language:

#include <stdio.h> 
#include <stdlib.h>
void main() 
{ 
    int i,j,k,n; 
    printf("&#39;water flower&#39;number is:"); 
    for(n=100;n<1000;n++) 
    { 
        i=n/100;/*分解出百位*/ 
        j=n/10%10;/*分解出十位*/ 
        k=n%10;/*分解出个位*/ 
        if(n==i*i*i+j*j*j+k*k*k) 
        { 
            printf("%-5d",n); 
        } 
    } 
    printf("\n"); 
}
Copy after login

Upgraded version:

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
int cube(const int n){
    return n*n*n;
}
bool
isNarcissistic(const int n){
    int hundreds=n/100;
    int tens=n/10-hundreds*10;
    int ones=n%10;
    return cube(hundreds)+cube(tens)+cube(ones)==n;
}
int main(void){
    int i;
    for(i=100;i<1000;++i){
        if(isNarcissistic(i))
            printf("%d\n",i);
    }
    return EXIT_SUCCESS;
}
Copy after login

For more C language tutorials, please pay attention to PHP Chinese website!

The above is the detailed content of How to write the narcissus number in C language code. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template