Write a program using integers userNum and x as input, and output userNum divided by x three times. Using c+++ ?

The only way i know to go about it is using a loop but i dont know how to eliminate the trailing white space that comes afterwards.Anyone know how they would do this.You need to have 3 consecutive digits in this format x x x.i got an input of 2000 and 2.

1 Answer

Relevance
  • 6 days ago

    If x is a digit, and if "divide by x three times" means that with userNum=2000 and x=2 the idea is to divide 2000 by 222, then this is easier that you think.

     You can't divide by zero, so x is in the range 1-9 if its a digit.  To convert 2 into 222 or 3 into 333, etc. just multiply by 111.

        2 * 111 == 111    3 * 111 == 333    4 * 111 == 444

        5 * 111 == 555    get the idea?

    Try this, after you have read in values for userNum and x:

        std::cout << (userNum / (111 * x)) << std::endl;

    This is more trouble if x can be 2 digits or more.  The above idea only works for single-digit values of x.  For 2-digit values, though, you can convert 42 to 424242 with multiplication by 10101 instead of 111.  For 3 digits, repeated 3 times, you need to multiply by 1001001.

    After that, you're going to be out of the range of the int data type on most current implementations. The usual size for an int these days is 32-bits, and the maximum positive value is a little over 2 billion.  If all digits values are possible, 9 digit numbers are okay, but watch out with 10 digits...and 11 digits are almost always too big.

Still have questions? Get your answers by asking now.