Digits reverse

This is a discussion on Digits reverse within the C++ Programming forums, part of the General Programming Boards category; i have write a program digits reverse.......but prob here....how to gets output with negatif number.... e.g input : -123456 output ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    8

    Question Digits reverse

    i have write a program digits reverse.......but prob here....how to gets output with negatif number....

    e.g
    input : -123456
    output : -654321

    this my program

    Code:
    #include <iostream>
    #include <stdlib.h>
    
    using namespace std;
    
    int main()
    {
    int a,b,c,d,e,f,sum,find=0;
    
    cout << "Enter an integer digits: ";
    cin >> sum;
    
    
    a = (sum % 10);
    
    b = (sum % 100);
    b = b / 10;
    
    c = (sum % 1000);
    c = c / 100;
    
    d = (sum % 10000);
    d = d / 1000;
    
    e = (sum % 100000);
    e = e / 10000;
    
    f = (sum % 1000000);
    f = f / 100000;
    
    
            if (sum<10)
              {  
                cout << "The Reverse Order is   : " << endl;
                cout << a << endl;
               } 
            else if (sum<100)
             {
                cout << "The Reverse Order is   : ";
                cout << a << b << endl;
              } 
            else if (sum<1000)
              {
                cout << "The Reverse Order is   : ";
                cout << a << b << c << endl;
              }
             else if (sum<10000)   
              {
                cout << "The Reverse Order is   : ";
                cout << a << b << c << d << endl;  
               } 
             else if (sum<100000)
               {        
                cout << "The Reverse Order is   : ";
                cout << a << b << c << d << e <<  endl;  
               }
              else if (sum<1000000)   
              {        
                cout << "The Reverse Order is   : ";
                cout << a << b << c << d << e << f << endl;  
              }
              
              else if ((sum<0) && (sum<10))
              {
                cout << "The Reverse Order is   : ";
                cout <<"-"<< a << endl;
              }
      
           
             
      cout << "\n\n\n\n";       
    
     system("PAUSE");
     return 0;
     
     }

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    Code:
    /* ...Before the parsing... */
    
    if (sum < 0) {
       sum *= -1;
       isNegetive = true;
    
    /* ... After the parsing ... */
    
    if (isNegetive)
       sum *= -1;
       // Or if you wish...
       // cout << a * -1 << b ...
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    i have write a program digits reverse
    You can do the whole program in only a few lines that will handle any size number if you turn the number into a string type and then reverse the string.

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    8
    Quote Originally Posted by SlyMaelstrom
    Code:
    /* ...Before the parsing... */
    
    if (sum < 0) {
       sum *= -1;
       isNegetive = true;
    
    /* ... After the parsing ... */
    
    if (isNegetive)
       sum *= -1;
       // Or if you wish...
       // cout << a * -1 << b ...
    input :-123456
    output:-6-5-4-3-2-1

    7stud....
    im a beginner....so i write the program how i understand...
    u have any tips??...i appreciate it...thanks

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    Quote Originally Posted by mufanz
    input :-123456
    output:-6-5-4-3-2-1
    Congratulations, all that means is you implemented it wrong. You're only supposed to multiply the leading digit a by -1.
    Sent from my iPadŽ

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    8
    thanks....program success

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Code:
    #include <iostream>
    #include <string>
    #include <sstream> //ostringstream
    using namespace std;
    
    int main() 
    {
    	int num = 1234;
    
    	ostringstream myOutputString; //creates a special kind of variable(see below)
    
    	myOutputString<<num; 
    	//myOutputString works just like 'cout'
    	//except you're outputting 'num' into a variable
    	//instead of to the console window.
    	//You can output any type into the variable.
    
    
    	string myString = myOutputString.str();
    	//the str() function returns a string that contains
    	//whatever is inside myOuputString
    	
    	cout<<"This is now a string: "<<myString<<endl;
    
    	//strings have many functions for manipulating them,
    	//but you can just use a string's length() function and a for 
    	//loop to reverse a string.  A string can be treated just like
    	//and array of characters.
    
    	string reversed;
    
    	for(int position = myString.length() - 1; position >= 0; position--)
    	{
    		reversed = reversed + myString[position];
    	}
    
    	cout<<reversed<<endl;
    
      	
        return 0;
    }
    Last edited by 7stud; 03-24-2006 at 08:31 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. decimal to number if digits in different bases
    By jorgejags in forum C Programming
    Replies: 21
    Last Post: 09-24-2008, 12:55 PM
  2. Problem with my reverse function and its output!
    By Matus in forum C Programming
    Replies: 4
    Last Post: 04-29-2008, 08:33 PM
  3. reverse a number digits
    By tootoo in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2007, 11:24 AM
  4. Hex digits stored in char array.
    By Kevinmun in forum C Programming
    Replies: 8
    Last Post: 11-18-2005, 03:05 PM
  5. gethostbyaddr() reverse lookups failing (???)
    By Uncle Rico in forum C Programming
    Replies: 9
    Last Post: 08-19-2005, 09:22 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21