Thread: How to separate the number with spaces in between using while loop and power function

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    2

    How to separate the number with spaces in between using while loop and power function

    Dear all,
    How do I separate number with 5 spaces in between the numbers without reversing the number using while loop and <math.h> pow function?

    1. User will be asked to input n-number of numbers
    2. User will be asked to enter the numbers
    3. Print out the numbers without using arrays but only basic while loop and pow function.

    Hope to get some help, thank you!

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    632
    I don't understand your question. Why are you worried about the numbers possibly becoming "reversed"? And how does the pow function come into it?

    Show example input and output.
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    2
    Quote Originally Posted by john.c View Post
    I don't understand your question. Why are you worried about the numbers possibly becoming "reversed"? And how does the pow function come into it?

    Show example input and output.
    Hi John, attached to this message is my code and my output. Do take a look thank you. How to separate the number with spaces in between using while loop and power function-asl-jpg

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    632
    If I understand correctly, you want to print out the digits of the number in the correct order, using the pow function (presumably with powers of 10).

    Since you have the number of digits (which I guess we are assuming is true, since there's no guarantee that's how many digits the user enters), then number / pow(10, num_digits - 1) stored in an int would give the first digit. number / pow(10, num_digits - 2) would give a value ending in the second digit of number, so taking that % 10 would give the second digit. Etc.

    log10 could be used to determine the number of decimal digits in number without needing to trust the user.
    Last edited by john.c; 02-08-2019 at 02:16 PM.
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Largest Power of 2 below a number n
    By Blasz in forum Tech Board
    Replies: 4
    Last Post: 05-08-2011, 02:06 AM
  2. regarding Nth Power of a Number
    By Shameer in forum C++ Programming
    Replies: 8
    Last Post: 08-11-2010, 10:44 PM
  3. Replies: 31
    Last Post: 11-25-2009, 01:10 PM
  4. checking if a number if a power of 2
    By -EquinoX- in forum C Programming
    Replies: 10
    Last Post: 03-27-2009, 02:55 AM
  5. Is number power of 2?
    By Roaring_Tiger in forum C Programming
    Replies: 2
    Last Post: 08-25-2004, 11:38 AM

Tags for this Thread