First digit - Find the first digit of an integer value This program should read
ID: 3728406 • Letter: F
Question
First digit - Find the first digit of an integer value This program should read a single integer input value and then print the value of its first digit. For example, if the input value is 42, then the program should print the output Other examples: Input Output 1331 1 87433 8 462 4 90125 9 Hints Wi l while loop) . If n is an integer value, then n/10 is an integer whose decimal representation is the same as n's except that the rightmost digit is removed (e.g., if n is 373, then n/10 is 37) . Think about what loop condition will let the program know that it ha:sExplanation / Answer
#include "stdio.h"
int main(void) {
// Taking user input of a number
printf("Enter a number: ");
int number;
scanf("%d",&number);
// If a number is below greater than 9, it has 2 digits. So, we need to reduce it to less than 10.
while(number > 9)
{
// each time removing last digit by dividing it by 10
number = number / 10;
}
// printing output
printf("First digit is %d",number);
return 0;
}
/*SAMPLE OUTPUTS
Enter a number: 1331
First digit is 1
Enter a number: 87433
First digit is 8
Enter a number: 462
First digit is 4
Enter a number: 90125
First digit is 9
Enter a number: 0
First digit is 0
*/
/* HIT the thumbs up if you are happy with answer or get back to me by commenting if you didn't understand anything */
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.