int main()

My code works fine...i will be using const later ,hence my variable are capital letters.What i need a little help is when the NUM_VALUE is negative.If i am not wrong its call integer division error.(where one or more operand is negative.How can i avoild this?
I inserted under do

if(NUM_VALUE < 0)
   NUM_VALUE = -NUM_VALUE;
      cout<< NUM_VALUE;

but it doesnt seem to work.Any ideas?

int main()
{
   long NUM_VALUE;
   cout << "Enter any integer number" << "\n";
   cin >> NUM_VALUE;

   do                                           
   {
      long REVERSE=0;
      REVERSE = REVERSE*10+ NUM_VALUE % 10;
      cout<< REVERSE;
      NUM_VALUE = NUM_VALUE / 10;

   }
   while (NUM_VALUE != 0);

   cout<<"\n";

   return(0);
}

Edited 4 Years Ago by Valiantangel: correction

Negative numbers are integers; it's not an error to divide by them. Here's a sample run of your original code (compiled with MinGW):

Enter any integer number
-1234
-4-3-2-1

Do you get something different?

ok here is my new code.This time if i key in -1234, i give me 4321..That Grest but how can i alo get the -(minus) to be behind....like 4321-....any ideas?

int main()
{
   long NUM_VALUE;
   cout << "Enter any integer number" << "\n";
   cin >> NUM_VALUE;
   if(NUM_VALUE < 0)
   {
         NUM_VALUE = -NUM_VALUE;
   }


   do                                           
   {
      long REVERSE=0;
      REVERSE = REVERSE*10+ NUM_VALUE % 10;
      cout<< REVERSE;
      NUM_VALUE = NUM_VALUE / 10;

   }
   while (NUM_VALUE != 0);

   cout<<"\n";

   return(0);
}

Is it correct to but this so as to get a positive modulus?

do                                           
   {
      long REVERSE=0;
      REVERSE = NUM_VALUE % 10;
      if (NUM_VALUE % 10 < 0)
         NUM_VALUE=+10; // Is this correct?

      cout<< REVERSE;
      NUM_VALUE/=10;
   }

By the way how can i make REVERSE AND NUM_VALUE constant.

i tried with

constant long RESERVE=NUM_VALUE%10;
constant long NUM_VALUE /=10;

doesnt seem to be working.

ok here is my new code.This time if i key in -1234, i give me 4321..That Grest but how can i alo get the -(minus) to be behind....like 4321-....any ideas?

You could keep track of whether NUM_VALUE was negative to begin with and just output '-' at the end if it was.

Is it correct to but this so as to get a positive modulus?

Run it and see... what do you get?

By the way how can i make REVERSE AND NUM_VALUE constant.

The keyword is const.

This article has been dead for over six months. Start a new discussion instead.