1. 程式人生 > >38.找出100-999之間各位相加等於15的數及個數

38.找出100-999之間各位相加等於15的數及個數

給定程式中,函式fun的功能是:找出100至x(x<999)之間各位上的數字之和為15的所有整數,然後輸出,符合條件的整數個數作為函式返回值。

#include<stdio.h>
fun(int x)
{
	int n, s1, s2, s3, t;
	n = 0;
	t = 100;
	while (t <= x)
	{
		s1 = t % 10;
		s2 = (t / 10) % 10;
		s3 = t / 100;
		if ((s1 + s2 + s3) == 15)
		{
			printf("  %d  ", t);
			n++;
		}
		t++;
	}
	return n;
}

int main()
{
	int x = -1;
	while (x > 999 || x < 0)
	{
		printf("Please input (0<x<99):");
		scanf_s("%d", &x);
		printf("\nThe result is:%d\n", fun(x));
		getchar();
		getchar();
		return 0;
	}
}