1. 程式人生 > >C 判斷一個數裡有幾個相同的數字問題

C 判斷一個數裡有幾個相同的數字問題

問題:

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;
}