Write a C program that prompts the user to enter an integer, and then prints out
ID: 3623151 • Letter: W
Question
Write a C program that prompts the user to enter an integer, and then prints out its decimal digits, each one on a separate line, starting with the least signicant (rightmost) digit. Each printed line should indicate which of the digits is being printed. Note that the user can input either a positive or a negative integer: the inputs n and -n, where n is an integer, should produce exactly the same output. Here are three sample runs of this program.Enter an integer: 512
Digit (1): 2
Digit (2): 1
Digit (3): 5
Enter an integer: 0
Digit (1): 0
Enter an integer: -16
Digit (1): 6
Digit (2): 1
Hint: You can use the integer division and modulo operators to parse a positive integer into its digits.
For example, n%10 produces the least signicant digit of n, while n/10 deletes this digit.
Explanation / Answer
please rate - thanks
#include <stdio.h>
#include <conio.h>
int main()
{int n,digit,i;
printf("Enter a number: ");
scanf("%d",&n);
if(n<0)
n*=-1;
i=0;
while(n>0||i==0)
{i++;
digit=n%10;
printf("Digit (%d): %d ",i,digit);
n/=10;
}
getch();
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.