Per new school question and answer asked students to tell what they agree to is the main important concern for a student to do if they wanted to become success. The one that response stood out from the rest was practice. People who absolutely successful do not become successful by being born. They work hard and perseverance their lives to succeeding. This is how you can attain your goals. down below are one of the answer and question example that you could simply utilise to practice and further enhance your practical knowledge and also give you insights that might just assist you to maintain your study in school.
Question:
Input a double and print the first two digits after the decimal point with a space between them. Sample run: Please input a decimal number: 57.8934 Answer: 8 9 Hint – to complete the second coding activity in lesson 5 you had to learn how to get individual digits from an int value. You can reuse this method once you convert the user input to an appropriate int value (you will need both multiplication and casting for this).
Answer:
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);
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:
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.
/*Second digit -> Does the same thing*/
From the answer and question examples above, hopefully, they could help the student sort out the question they had been looking for and take notice of all things stated in the answer above. You may then have a discussion with your classmate and continue the school learning by studying the question collectively.