#include<IOSTREAM.h>
main()
{
int rohan=0;
int m=10;
int l=10;
int t=10;
int flag1 =0;
int flag=0;
int temp;
float temp1;
long double n;
double k;
cout<<"ENTER N\n";
cin>>n;
k=n;
int camp;
camp=k;
while (flag1)
{
l*=10;
camp/t;
t*=10;
if (camp == 0)
{flag1=1;}
}
if ((1>n)&&(n>=1e6))
{cout<<"there is a error";}
else
{
while(flag==0)
{
k+=1;
while(l!=m)
{
temp1=k/l;
temp=m*(k/m-k);
m*=10;
l/=10;
if(temp1!=temp)
rohan=0;
else rohan=1;
}
if(rohan==1)
flag=1;
else
flag=0;
}}
if(flag==1)
cout<<"THE PALLINDROME IS "<<k;
else
cout<<"no such pallindrome exists";
return 0;
}

Recommended Answers

All 6 Replies

THE program hangs
PLEASE HELP

First off, you should use [code] tags around your code samples in the future. I expect one of the mods will do this for you, but you should not rely on this, if only because it makes extra work for them.

Second, there are a number of issues with the code as given, such that - with a modern compiler, at least - the code should not compile. For example, the <IOSTREAM.H> is now just <iostream> - not capitalized - and most current compilers will reject the older form (and capitalized should never have worked in the first place). Another place where there is a problem is the implicit integer type for main() ; you should always declare main() as returning int explicitly, and a modern compiler would flag that as an error.

I am guessing that you are using an older compiler such as Turbo C++, in which case I would recommend moving to a newer compiler - if you have the choice to, that is - such as GCC or Visual C++ Express.

As for the specific error message, the problem presumably lies with this line:

temp=(k/m-k)m;

Here you are using an implicit multiplication by placing m next to a parenthesis; this is a common misunderstanding, as it looks like the usual mathematical notation. The problem is, despite similarities and syntactic sugar, C++ expression syntax is not mathematical notation, and doesn't work like it. The code needs an explicit multiplication operator, like so:

temp = (k / m - k) * m;

(The spacing was added solely to make the expression operators clearer, not to change the actual code.)

#include<iostream>
using namespace std;
int main(){
    int rohan=0 , m=10 , l=10 , t=10 , flag1 =0 , flag=0 , temp;
    float temp1;
    long double n;
    double k;
    
    cout<<"ENTER N\n";
    cin>>n;

    k=n;
    int camp;
    camp=k;
    
    while (flag1){
           l*=10;
           camp/t;
           t*=10;
           if (camp == 0){
               flag1=1;
               }
           }

    if ( (1 > n) && (n >= 1e6) ){
        cout<<"there is a error";
        }
    else {
          while (flag == 0){
                 k+=1;
                 while (l != m){
                        temp1=k/l;
                        temp=m*(k/m-k);
                        m*=10;
                        l/=10;
                        if(temp1 != temp)
                           rohan=0;
                        else 
                           rohan=1;
                        }
                 if(rohan == 1)
                    flag=1;
                 else
                    flag=0;
                 }
          }
   
   if(flag == 1)
      cout<<"THE PALLINDROME IS "<<k;
   else
      cout<<"no such pallindrome exists";
system("pause");
return 0;
}

what do you want exactly from this program !!

commented: this is exactly what I wanted to ask when I was looking at the first post of the thread :) +1

man yes you are right i was turbo c++ but the code works fine and the compiling displays no errors
but when i run the program it suddenly hangs and that why i have post this here
and i am trying to get the answer of the following question from the program


http://opc.iarcs.org.in/index.php/problems/PRIMEPALIN

Where in the program does it hang? Does it get any output at all?

I believe I see your problem.

Your first while loop reads "while(flag1){..." However, you've initialized flag1 to 0 so the while loop will never run.

Because of that, the "l*=10;" that you relied on to make "l" different from "m" (because you set both to 10) won't happen.

Because both l and m are 10, the third while loop (while(l!=m)) will never run. When the while loop skips, the next code to operate is "if(rohan == 1)..." which it does not, therefore, flag is set to 0.

Because rohan will never change (previous loop will never run), flag will always be set to 0. Since the loop this is in will continue as long as "flag==0", your program hangs.

As it is, I still can't interpret what your third while loop was supposed to do.

I would suggest a program redesign. There are published algorithms for finding a prime equal to or greater than N. A Google search will provide you with what you need. Once you have that, you can use specialized palindrome checking code and you don't have to worry about making sure it's prime.

ex:

int main()
{
    bool palindromeFound = false;
    int prime;

    prime = getInput();

    While(!palindromeFound && prime < 1000000)
    {
        prime = getNextPrime(prime+1);

        if(isPalindrome(prime))
        {
            palindromeFound = true;
        }
    }

    if(palindromeFound)
    {
        cout << "Palindrome is " << prime;
    }
    else
    {
        cout << "No palindrome found";
    }

    return 0;
}

This is just a basic skeleton of what I'm suggesting. You would flush it out to fit your purpose.

In this case, you could use getInput() to reject bad input (N <= 0 || N >= 1000000) and request more input. getNextPrime() would be the prime function you find and wish to implement, and isPalindrome() would be your code to determine if the number is a palindrome.

My implementation has one flaw. If the user enters a prime palindrome, my program won't catch it, so if you use my implementation, you'll have to do a special case check for it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.