Answer to Question #343966 in C for Ken

Question #343966

Instructions:

  1. Input a non-zero positive integer.
  2. Using while loop, print out each digit of the inputted integer in separate lines, starting from its rightmost digit until the leftmost digit of the number.
  3. Tip #1: Use % 10 to get the rightmost digit. For example, if you do 412 % 10, then the result would be the rightmost digit, which is 2.
  4. Tip #2: On the other hand, use / 10 to remove the rightmost digit. For example, if you do 412 / 10, then the result would be 41.
  5. Tip #3: You'd have to repeat Tip #1 and Tip #2 inside the while() loop for this problem while the inputted integer is not yet 0.


Input


1. An integer

Output

The first line will contain a message prompt to input the integer.

The succeeding lines contain the digits of the integer.

Enter·n:·214
4
1
2
1
Expert's answer
2022-05-23T10:19:17-0400
#include <stdio.h>


int main()
{
	int n;

	printf("Enter n: ");
	scanf("%d", &n);
	while (n != 0)
	{
		printf("%d\n", n % 10);
		n /= 10;
	}

	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS