1. 程式人生 > >列印1-999之內的“水仙花數”

列印1-999之內的“水仙花數”

列印1-999之內的“水仙花數”

思路分析:水仙花數就是一個三位數他的每一位上的數的立方之和等於它本身,例如153=13+53+3^3;則153是一個水仙花數;

完整程式碼:

#include<stdio.h>
#include<windows.h>
int main()
{
	int i, a, b, c;
	for (i = 100; i <= 999; i++)
	{
		a = (i / 100);//百位數字
		b = (i % 100 / 10);//十位數字
		c = (i % 100 % 10);//個位數字
		
		if ((a*a*a + b*b*b +
c*c*c) == i)//條件 { printf("%d\t", i); } } system("pause"); return 0; }

執行結果:在這裡插入圖片描述