/* * Author: AGHATISE OSAZUWA * Website: www.cscprogrammingtutorials.com * * Program to separate the digits of a five-digit number. * * Program Algorithm * ----------------- * declare variables to store user input(num) and each digits(dig1, dig2, dig3, dig4, dig5) * read the user input eg 42139 * assign the last digit of the number to dig5 ie 9 * divide the number by 10 ie 42139/10 = 4213 * calculate the remainder and assign the value to dig4 ie 3 * divide the number by 10 ie 4213/10 = 421 * calculate the remainder and assign the value to dig4 ie 1 * divide the number by 10 ie 421/10 = 42 * calculate the remainder and assign the value to dig4 ie 2 * divide the number by 10 ie 42/10 = 4 * calculate the remainder and assign the value to dig4 ie 4 * print the digits separated by three spaces ie 4 2 1 3 9 * */ #include <stdio.h> int main ( ){ int num, dig1, dig2, dig3, dig4, dig5; printf("Enter a 5 digit whole number: "); scanf("%d", &num); dig5 = num % 10; num = num / 10; dig4 = num % 10; num = num / 10; dig3 = num % 10; num = num / 10; dig2 = num % 10; num = num / 10; dig1 = num % 10; printf("%d %d %d %d %d\n", dig1, dig2, dig3, dig4, dig5); return 0; }

## Wednesday, September 17, 2014

# C Program to Separate Digits of a Number

Question: Write a program that inputs one five-digit number, separates the number into its individual digits and prints the digits separated from one another by three spaces each. [Hint: Use combinations of integer division and the remainder operation.] For example, if the user types in 42139, the program should print 4 2 1 3 9

Click here to see other solutions to Deitel C How To Program exercises. Here is the same program to separate digits of a number written in Java using another algorithm.

#### 3 comments:

**DISCLAIMER**: Opinions expressed in comments are those of the comment writers alone and does not reflect or represent the views of the post author. We reserve the right to delete any post deemed inappropriate or offensive and/or spammy. **Please do not use abusive words/hate speech**.

I Appreciate your valuable Feedback. So, Please **DO NOT SPAM** - Spam comments will be deleted immediately.

Don't use brand name in name field and you're not allowed to use links in comments unless it's necessary. Such comments will be removed immediately. To get notified of replies or follow-up comments, click the box next to notify me.

Thanks.

How will the digits be seperated when user inputs any digits number?

ReplyDeletewhats dig5 = num % 10; stands for?

ReplyDeletedig5 is a variable to hold the value of the remainder when the number is divided by 10. For example, if the number is 42139, dig5 = 9.

Delete