Latest web development tutorials

C Exercise Examples 13-- narcissistic number

100 cases of classic C language 100 cases of classic C language

Topic: Print out all the "narcissistic number", called "narcissistic number" refers to a three-digit number, which you figures Cube and equal to the number itself. For example: 153 is a "narcissistic number" because 153 = 1 cubic cubic +5 +3 cubed.

Program analysis: the use of the control loop for the number of 100-999, each factorization of bits, ten, one hundred.

//  Created by www.w3big.com on 15/11/9.
//  Copyright © 2015年 本教程. All rights reserved.
//

#include<stdio.h>

int main()
{
	int i,x,y,z;
	for(i=100;i<1000;i++)
	{
		x=i%10;
		y=i/10%10;
		z=i/100%10;
		
		if(i==(x*x*x+y*y*y+z*z*z))
		printf("%d\n",i);
		
	}
	return 0;
}

The above example output is:

153
370
371
407 

100 cases of classic C language 100 cases of classic C language