Click here to Skip to main content
15,901,001 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
//============================================================================
// Name        : hello.cpp
// Author      : 
// Version     :
// Copyright  f : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================


#include <iostream>

using namespace std;

int main()
{
    int number , a  ;
    int x , n ;
    int sum = 0 ;

    cout << "Enter the numbers of numbers : " << endl ;
    cin >> n ;
    cout << "Enter the numbers : " << endl ;
    for( int i = 0 ; i < n ; i++ )
    {
    cin >> number ;

    a = number ;

    while( a != 0 )
    {
        x = a % 10 ;
        sum = sum * 10  + x ;
        a /= 10 ;

    }

    }
    cout << " reverse numbers " << sum << endl ;


    if( sum = number)
    {
        cout <<"yay"<< number ; //mikhan age 2 ta adad shabih bodan 2 ta shono print kone ???
    }

    return 0 ;

}

this program reverse numbers then compare numbers and reverse numbers but it does not work !
Posted
Updated 20-Apr-11 1:04am
v2

There ere so many, many things wrong with this, that I don't even know (with your description) what it is supposed to do.

Just to start off: Where do you change "m"? The first time you use it, you overwrite the first number the user entered with a zero, having just worked out how many digits there are in a number the user didn't enter.

I strongly suggest that you need to learn to use the debugger: put a breakpoint on the
temp = num[j] ;
line and single step from there, looking at what is happening with all your variables.

You mind find it helps if you break it down into small steps and implement them one at a time, checking each is working before moving on to the next.

You also need to format the code so that the indentation makes it easier to read!
 
Share this answer
 
Comments
Sandeep Mewara 11-Apr-11 9:26am    
This surely is not written by him. Look at this answer: http://www.codeproject.com/Answers/179655/i-want-to-reverse-all-numbers-but-it-just-reverse-.aspx#answer1

Now, I am sure, he got the code from somewhere and is trying to make changes in order to reverse a set of numbers.
OriginalGriff 11-Apr-11 9:29am    
You are probably right!
You are looking for a Palindromic number. The code you provided shows a number of shortcomings, but for now I will focus on those that are required to get you the correct results.

1. In main() you defined a variable j, but then you introduced another variable j in your first for loop. Please note that the latter variable will hide the current value of the previously declared variable. Also, depending on compiler, the later defined variable may or may not continue to exist past the end of the for loop. So it is unclear what you are referring to in the last few lines of your code.

Solution: the last 4 lines before the return statement probably should be moved inside the for loop; you will not need to declare the variable j at the start of main then. Alternately, remove the declaration and initialization of j from the for loop header.

2. In a you compute the number of digits of the number you last read. But not quite: since your while loop tests ahead, the resulting number will be 1 digit short. This may or may not have been your intention.

Solution: just test while(temp>0) rather than while (temp/10>0)

3. Your digit counter a is never re-initialized after reading the next number!

Solution: move the initialization of a to immediately before the while loop.

4. Your nested for loop tries to do two things at the same time: extract the digits of your original number and calculate its reverse. This is a bad idea for at least two reasons: first it's much harder to find out which part of the loop is buggy, second you cannot calculate the reverse of your original number in the same order that you use to decompose it into digits - you have to reverse that order first!

Solution: create an array to hold the digits on the heap (you already calculated the number of digits in a, so you know the array size you need). Then extract the code for reading the digits from your number into one function. Then write a function to calculate a number from an array of decimal digits. Either make that second function read the digits from the back of the array, or use another function to first reverse its order.

5. Of course, you could avoid much of this work, if you didn't bother with numeric values at all - all you have to do is read the input as a string and reverse its characters...
 
Share this answer
 
Comments
Sandeep Mewara 11-Apr-11 9:27am    
My 5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900