I've written a n00b practice program that extracts each digit from an integer of any size, and tell you how many of those integers are the number 7. The program works great, as long as the number the user enters is 9 digits or less...once it goes over that, the amount of 7's the program counts gets weird. Haven't been able to find anyone else with this exact problem, so any understanding of this issue would be greatly appreciated. Here's what I've got:
Code:
/*This program will tell you how many digits in your number are 7*/
#include <stdio.h>
int main()
{
int number, number_counter = 0, digit, digit_counter = 0;
printf("\nPlease enter a number: ");
scanf("%d", &number);
/*So we can always refer back to the original number if needed, and work with number_counter instead*/
number_counter = number;
/*number_counter will be less than one only after the last digit is extracted*/
while(number_counter > 1){
/*This sets digit to the furthest remaining to the right digit*/
digit = number_counter % 10;
/*This removes the digit we just extracted*/
number_counter /= 10;
/*This keeps a count of how many 7's have been found so far, for some reason it will only print a single digit?*/
if(digit == 7){
digit_counter++;
}
}
/*This will only print a single digit, ie. 10 == 0, couldn't figure this out?*/
printf("\n %d of the numbers you entered were 7!\n\n", digit_counter);
return 0;
}
I had thought maybe I was running into a size limit, so I tried setting my variables to long and long long, but that had no effect on the problem, so I'm stumped from here