C 判断一个数里有几个相同的数字问题

版权声明:转载请附上文章地址 https://blog.csdn.net/weixin_38134491/article/details/85341802

问题:

Write a program that reads an integer, and determines and prints how many digits in the integer are 7s

#include<stdio.h>
#include<stdlib.h>

int num;
int a, b, c, d, e;
int i = 0;

int main(void) {
	printf("Enter a 5-digit number:");

	scanf_s("%d", &num);
	//The first digit 
	int a = num / 10000;

	//The second digit
	b = num / 1000 % 10;

	//The third digit
	c = num / 100 % 10;
	
	//The fourth digit
	d = num / 10 % 10;

	//The last digit
	e = num % 10;

	if (a == 7) i++;
	if (b == 7) i++;
	if (c == 7) i++;
	if (d == 7) i++;
	if (e == 7) i++;

	printf("The number %d has %d seven(s) in it\n",num,i);

	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_38134491/article/details/85341802