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

  • Comp Sci
  • Thread starter Isma
  • Start date
  • Tags
    C++ Program
In summary, Warren has just started studying programming and is struggling to understand how to write these programs. He has success with one program using a goto condition, but is lost with for loop and can't get the output he needs for required binary or decimal. He posts codes he has attempted, but they don't work. He asks someone else to check them. He also struggles with binary to decimal conversion. He has success with a rough algorithm using a for loop and pow(a,b) function, but he is not familiar with arrays. He asks someone else to do it for him. Finally,
  • #1
Isma
27
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
  • #2
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
 
  • #3
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;
cout<<"length of no. :";
cin>>n;
cout<<"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)
cout<<"No. isn't a palindrome"<<endl;
else if (n==0 || n==1)
cout<<"No. is palindrome"<<endl;
else
goto flag;
}

for 2.
{
int num,n,x,length,division1,a,b,y;
cout<<"length :";
cin>>n;
cout<<"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)
cout<<"no. is a palendrome"<<endl;
else if (a!=b)

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

}
nd i m working on 3rd
 
  • #4
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
 
  • #5
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;
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;
}
 
  • #6
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.
 
  • #7
but i vent done arrays till now...can we do it by loop?
 
  • #8
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;
}
 
  • #9
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)
cout<<"palindrome";
 

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

1. What is the best way to ask for help with a c++ program?

The best way to ask for help with a c++ program is to clearly state the issue you are having and provide any relevant code or error messages. It is also helpful to explain what you have already tried in solving the problem.

2. How do I find resources for troubleshooting my c++ program?

There are many online resources available for troubleshooting c++ programs, such as forums, online communities, and documentation. You can also reach out to other programmers or consult with a mentor or teacher for guidance.

3. What should I do if my c++ program is not running correctly?

If your c++ program is not running correctly, you should first check for any errors in your code. If you are unable to find the issue, try debugging your code or seeking help from others. It may also be helpful to break down the problem into smaller parts to identify the specific issue.

4. Can someone help me understand a specific concept in c++?

Yes, there are many resources available to help you understand specific concepts in c++, including online tutorials, documentation, and forums. You can also reach out to a more experienced programmer for guidance or take a course to improve your understanding.

5. How can I improve my c++ programming skills?

To improve your c++ programming skills, practice regularly and work on challenging projects. It is also helpful to read and study code written by experienced programmers, attend workshops or seminars, and continually seek to learn new techniques and technologies.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
4K
  • Programming and Computer Science
Replies
2
Views
761
  • Programming and Computer Science
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
9K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
18K
Back
Top