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
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("'water flower'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"); }
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; }
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!