Respuesta :
Answer:
number = float(input("Please input a decimal number: "))
number = int (number * 100)
second_digit = number % 10
first_digit = int(number / 10) % 10
print("Answer: " + str(first_digit) + " " + str(second_digit))
Explanation:
Ask the user to enter a decimal number (number = 57.8934)
Multiply it by 100 (number = 5789.34) and typecast to int (number = 5789)
Use the module to get the second digit (9)
Divide the number by 10 (578.9), typecast to int (578) and use module operator to get the first digit (8)
Print the numbers as requested
The code is:
int main(){
double n;
int v, d;
scanf("%lf\n", &n);
n = n*10;
v = (int)n;
d = v%10;
printf("The first digit is %d\n", d);
n = n*10;
v = (int)n;
d = v%10;
printf("The second digit is %d\n", d);
return 0;
}
----------------------
- To get each decimal digit, we multiply by 10 until the digit, then calculate the remainder of the operation(mod) of the integer part relative to 10.
- For example:
57.8934 x 10 = 578.934
The integer part is 578.
The remainder of the division of 578 by 10 is 8.
Now, we have to generalize it, thus:
int main(){
double n;
int v, d;
scanf("%lf\n", &n); -> Reads the double;
/*First digit*/
n = n*10; -> Multiplies by 10.
v = (int)n; -> Casts the integer part
d = v%10; 0 -> Remainder relative to 10 to get the first digit.
printf("The first digit is %d\n", d);
/*Second digit -> Does the same thing*/
n = n*10; -> Multiplies by 10.
v = (int)n; -> Casts the integer part
d = v%10; 0 -> Remainder relative to 10 to get the first digit.
printf("The second digit is %d\n", d);
return 0;
}
A similar problem is given at https://brainly.com/question/18340665