#include <stdio.h>
int numLen(int x) // returns the number of digits in x
{
int len = 0;
while (x > 0)
{
x = x / 10;
len++;
}
return len;
}
void patternChecks(int x, float* product, int* divisor_p)
/* returns the product of all digits in x
divided by the last digit in x (via *product).
checks if the first digit is divisible by the last digit and
returns 1 if true, 0 otherwise (via *divisor_p). */
{
int first = x/1000;
int last = x%10;
int num = x;
*product = 1;
while (x > 0)
{
*product = *product * (x % 10);
x = x / 10;
}
*product = *product / last;
if ( last % first == 0)
{
*divisor_p = 1;
}
else
{
*divisor_p = 0;
}
}
int main()
{
int x;
float product = 1;
int divisor_p;
printf("Enter a 4-digit number: ");
scanf("%d", &x);
int len = numLen(x);
while (len != 4)
{
printf("This number is %d digits\n", len);
printf("Enter a 4-digit number: ");
scanf("%d", &x);
len = numLen(x);
}
patternChecks(x, &product, &divisor_p);
if (divisor_p == 1)
{
printf("The product of digits in %d divided by %d is %.1f and the right most digit of %d is divisible by the left most digit\n", x, x % 10, product, x);
}
else
{
printf("The product of digits in %d divided by %d is %.1f and the right most digit of %d is not divisible by the left most digit\n", x, x % 10, product, x);
}
return 0;
}