Mastering Palindromes: Understanding C++ Programming with Goto and For Loops

  • Context: Comp Sci 
  • Thread starter Thread starter Isma
  • Start date Start date
  • Tags Tags
    C++ Program
Click For Summary

Discussion Overview

The discussion revolves around programming techniques for recognizing palindromes and converting numbers between binary and decimal formats using C++. Participants explore different methods, including the use of goto statements and for loops, while seeking assistance with coding challenges.

Discussion Character

  • Homework-related
  • Technical explanation
  • Exploratory

Main Points Raised

  • One participant expresses difficulty in understanding how to implement palindrome recognition using both goto statements and for loops, as well as converting between binary and decimal.
  • Another participant suggests that they can help fix code if the original poster shares their attempts, rather than doing the homework for them.
  • A participant shares a code snippet for recognizing palindromes using a goto method, but notes issues with their VC++ environment causing the computer to shut down when running the program.
  • Participants discuss the use of the pow() function and suggest an alternative method for palindrome recognition by reversing the number and comparing it to the original.
  • One participant mentions needing to apply the pow function and specifies that it requires including the math.h header file.
  • A participant proposes an algorithm for binary to decimal conversion, involving an array and multiplication by powers of two, but another participant questions whether it can be done using loops instead of arrays.
  • Another participant shares a code snippet for converting a number to binary and suggests finding the reverse of a number to check for palindromes.

Areas of Agreement / Disagreement

Participants generally express varying levels of understanding and approaches to the programming tasks, with no consensus on the best method for palindrome recognition or number conversion. Multiple competing views and methods are presented without resolution.

Contextual Notes

Some participants mention specific functions and methods that may not be familiar to all, indicating a potential gap in knowledge regarding certain programming concepts. There are also unresolved issues related to the original poster's coding environment and the functionality of their code snippets.

Isma
Messages
27
Reaction score
0
i've just started studying programming...nd i can't understand how to write these programs:

1:to recognize palindromes using goto condition
2:to recognize palindromes using for loop
3:to convert binary into decimal nd decimal into binary

i m seriously lost nd can't understand for loop...pleasez help
actually i ve finally done palindrome question by goto method:)
pleasez help me with for loop
nd tell me how to get output for required binary or decimal no.?doz it use for loop?
 
Last edited:
Physics news on Phys.org
We're not going to do your homework for you. You can post programs you've attempted, but which don't work, and we can try to help you fix them.

- Warren
 
sorry i wasnt asking you to do it...actually my vc++ has some problem as soon as i run program computer shuts down...i ve made codes can any1 check them

for 1.
{
int a,b,c,d,n,div,div1;
count<<"length of no. :";
cin>>n;
count<<"Number :";
cin>>a;
flag:
div=pow(10,n-1);
div1=pow(10,1);
b=a/div;
c=a%div1;
d=a%div;
a=d/div1;
n=n-2;
if (b!=c)
count<<"No. isn't a palindrome"<<endl;
else if (n==0 || n==1)
count<<"No. is palindrome"<<endl;
else
goto flag;
}

for 2.
{
int num,n,x,length,division1,a,b,y;
count<<"length :";
cin>>n;
count<<"number :";
cin>>num;

for (n=1;n<=length;n++)
{
x=num/pow(10,length-n);
division1=pow(10,n-1);
a=x%10;
y=num%division1;
b=x/pow(10,n-1);

if (n>length)
break;

if (a==b)
count<<"no. is a palendrome"<<endl;
else if (a!=b)

count<<"no. is not a palendrome"<<endl;

}
nd i m working on 3rd
 
what is the use of the pow() function please specify

another method just find reverse of a no and compare it to the orignal no
 
actually i need to apply only pow(a,b){it is a power function meaning a^b}
nd for loop nd goto conditions
pow(a,b) requires source file math.h
i ve done somethings for 3rd but can't check output
please some1 do

{
int n,a,s,m,i;
count<<"length of no.";
cin>>n;
count<<"number";
cin>>a;

s=8*n-1;
m=1<<s;

count<< setw( 10 )<<a<<"=";
for (i=1;i<=s+1;i++)
{
count<<(a & m ? "1" : "0" );
a<<=1;
if (a%8==0)
count<<" ";
}
count<<endl;
}
 
Well, I'm not too familiar with several of the functions you used in the third problem (setw(n), <<=, etc.) but for binary to decimal conversion, a rough algoithm might consist of passing each digit in the input to an array of a size equal to the length of the input, then multiplying each element by 2^n (where n represents the placement of the digit, i.e. n=0 for least significant digit, etc.), and then adding the multiplied value if the digit to the output.
 
but i vent done arrays till now...can we do it by loop?
 
As a FYI you can use a code tag so that your code is easier to read

Code:
...[slash code]

[code]
 {
int n,a,s,m,i;
cout<<"length of no.";
cin>>n;
cout<<"number";
cin>>a;

s=8*n-1;
m=1<<s;

cout<< setw( 10 )<<a<<"=";
for (i=1;i<=s+1;i++)
{
cout<<(a & m ? "1" : "0" );
a<<=1;
if (a%8==0)
cout<<" ";
}
cout<<endl;
}
 
how about finding reverse of a no by this logic and comparing it to the orignal one
int sum,a,b;
sum=0;
cin>>a;
b=a;
while(a!=0)
{
sum=(sum*10)+(a%10);
a=a/10;
}
if(sum==b)
count<<"palindrome";
 

Similar threads

Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 12 ·
Replies
12
Views
10K
  • · Replies 18 ·
Replies
18
Views
8K
  • · Replies 2 ·
Replies
2
Views
18K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K