Question

Write a C program which calculates how many digits a number has and prints each digit in reserve order with space in between(

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Basic:-

We know that any number%10 gives the last digit of that number. eg 467%10 will give 7, 4532%10= 2 etc.

Also, any number/10 removes the last digit feom the number, eg. 467/10 will give 46, 4532/10= 453 etc.

Algorithm for reversing the number:-

  1. Loop while number > 0 and perform steps 2 and 3
  2. Print the number%10, this gives the last digit of number
  3. Do number = number/10. This reduce the number by removing last digit.

Algorithm for finding number of digits in number:-

  1. Initialize variable count = 0 . This will store number of digits.
  2. Loop while number > 0 and perform steps 3 and 4
  3. Do number = number/10. This reduce the number by removing last digit.
  4. Increment count. (as we have seen one digit and removed it).

Input format:-

  • Enter the number

Code:-

#include<stdio.h>

//function to reverse the digits of a number
void reverseNumber(int num)
{
while(num > 0) //we will perform the loop while the number > 0
{
printf("%d ", num%10); //doing %10 gives the last digit of the number
num = num/10; //doing num/10 removes the last digit from the number
}
}

//function for counting digits of number
void CountDigits(int num)
{
int count=0; //for storing count of digits
while(num > 0) //we will perform the loop while the number > 0
{
num = num/10; //doing num/10 removes the last digit from the number
count++; //we will increse the count variable as we have seen one digit
}
printf("%d", count); //print the count variable
}
  
/*Driver program to test reversDigits*/
int main()
{
int num; //for storing the number entered by user
scanf("%d", &num); //taking input from user
reverseNumber(num); //calling function to reverse digits
printf("\n"); //for line break
CountDigits(num); //calling function to count digits in number
getchar();
return 0;
}

Output:-

754834 Copy > Run Time(sec): 0 Memory(MB): 1.56€ Output: 4 3 8 4 5 7 6

Code snippet:-

4 6 9 10 1 #include<stdio.h> 2 3 //function to reverse the digits of a number void reverseNumber(int num) 5-{ while(num > 0)

Add a comment
Know the answer?
Add Answer to:
Write a C program which calculates how many digits a number has and prints each digit...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Digit to WordsWrite a C program that asks the user for a two-digit number, then prints...

    Digit to WordsWrite a C program that asks the user for a two-digit number, then prints the English wordfor the number. Your program should loop until the user wants to quit.Example:Enter a two-digit number: 45You entered the number forty-five.Hint: Break the number into two digits. Use one switch statement to print the word for the firstdigit (“twenty,” “thirty,” and so forth). Use a second switch statement to print the word for thesecond digit. Don’t forget that the numbers between 11...

  • Write a program in C that calculates how many digits a number contains: Enter a number:...

    Write a program in C that calculates how many digits a number contains: Enter a number: 374 The number 374 has 3 digits You may assume that the number has no more than four digits. Hint: Use if statements to test the number. For example, if the number is between 0 and 9, it has one digit. If the number is between 10 and 99, it has two digits.

  • Write a program that prints to cout, separated by a space, the digits of any positive...

    Write a program that prints to cout, separated by a space, the digits of any positive three digit integer. You will need one variable, named, for example, value, for the number (int value) to be broken down into its digits. You might also need other variables to store the digits as you get them. This is c++. Cant use loops, if else statements etc.

  • Write a program that produces the following output for a random number of digits on a...

    Write a program that produces the following output for a random number of digits on a row and a random number of rows. 1****** 12***** 123**** 1234*** 12345** 123456* 1234567 Write a program that calculates the following series: i=1n(12)n=12+14+18+...+12n (hint: we need to talk about integer division) Use for loops to write a code segment that prints the following output. 1 2 3 4 2 4 6 8 3 6 9 12 4 8 12 16 5 10 15 20

  • Write a C Program that inputs an array of integers from the user along-with the length...

    Write a C Program that inputs an array of integers from the user along-with the length of array. The program then prints out the array of integers in reverse. (You do not need to change (re-assign) the elements in the array, only print the array in reverse.) The program uses a function call with array passed as call by reference. Part of the Program has been given here. You need to complete the Program by writing the function. #include<stdio.h> void...

  • Write an application (SpaceDigits) that: (in java) a. inputs one number consisting of five digits from...

    Write an application (SpaceDigits) that: (in java) a. inputs one number consisting of five digits from the user, separates the number into its individual digits and prints the digits separated from one another by three spaces each. For example, if the user types in the number 42339, the program should print “4 2 3 3 9.” (Hint: The number input is five digits long, dividing it by 10000 gives the leftmost digit. digit1 = number / 10000 ) b. determines...

  • Write a C++ program that inputs a single letter and prints out the corresponding digit on the...

    Write a C++ program that inputs a single letter and prints out the corresponding digit on the telephone.The letters and digits on a telephone are grouped this way:2 = ABC 4 = GHI 6 = MNO 8 = TUV3 = DEF 5 = JKL 7 = PRS 9 = WXYNo digits correspond to either Q or Z. For these two letters, your program should print a messageindicating that they are not used on a telephone. If a letter in lower...

  • C program--write a program to find whether the given number of three digits is an integer...

    C program--write a program to find whether the given number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is since 3**3 + 7**3 + 1**3 = 371. Output: Enter a number, or * to quit : 432 4**3 + 3**3 + 2**3 is not = 432 Enter a number, or * to quit : 371 3**3 + 7**3 + 1**3 is = 371...

  • C++ Write a program that asks for a number and then prints as many lines as...

    C++ Write a program that asks for a number and then prints as many lines as the user inputs. Each line contains as many pairs of characters ("*#") as the number of this line. It should look like the right half of a pyramid. Your version of the program must print the same result as the expected output. To to this lab, you must use two do-while loops. Two exceptions: When the user inputs a number less than 1, then...

  • How to write this code in C++???? using fstream Write a program which: Asks the user...

    How to write this code in C++???? using fstream Write a program which: Asks the user to enter a positive integer greater than 0 Validates that the entry is a positive integer Outputs the digits in reverse order with a space separating the digits Outputs the even digits not in reverse order with a space separating the digits (consider zero to be even) If there are no even digits, the an appropriate message should be displayed: There are no even...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT