hello! I am having problems with a palindrome program. I have to input an integer between 1 and 9. and then the output has to be:

for example if I input 4:
output: 1234321

if I input 5:
output: 123454321

if 1
output: 1

Recommended Answers

ANd the problem is?

Jump to Post

Well, what to you have so far? Code wise?

Jump to Post

Ok. Consider using two for loops. One to count up to a number and one to count down. Print the looping variable.

Jump to Post

All 10 Replies

ANd the problem is?

hello! I am having problems with a palindrome program. I have to input an integer between 1 and 9. and then the output has to be:

for example if I input 4:
output: 1234321

if I input 5:
output: 123454321

if 1
output: 1

the problem is that I don't know how to do it. I tried but I don't get it. can somebody help me, my mind is going to blow!!!

ANd the problem is?

that I have been trying but I don't get it. my mind is going to explode!!!

Well, what to you have so far? Code wise?

Well, what to you have so far? Code wise?

#include <stdio.h>
int main()
{
int n
printf ("Enter digit: ");
scanf ("%d",&n);

if ((n < 0) || (n > 9))
{
printf("number not in range\n");
}
else
printf("n-1...n...n-1")

Ok. Consider using two for loops. One to count up to a number and one to count down. Print the looping variable.

Ok. Consider using two for loops. One to count up to a number and one to count down. Print the looping variable.

something like:
for (count = 1; count <=n; count = count + 1)
for(count=1; count>=n; count = count - 1)
printf(ā€œ%dā€, count);

?

Sorta. But you don't want to nest the loops. printf() after each for loop.
for ( counting up )
printf();

for ( counting down )
printf()
The second loop will be going from the upper limit to 1, remember. So limits of count=n; count>=1; count-- would make more sense.

Sorta. But you don't want to nest the loops. printf() after each for loop.
for ( counting up )
printf();

for ( counting down )
printf()
The second loop will be going from the upper limit to 1, remember. So limits of count=n; count>=1; count-- would make more sense.

like:
#include <stdio.h>
int main()
{
int n
printf ("Enter digit: ");
scanf ("%d",&n);

if ((n < 0) || (n > 9))
{
printf("number not in range\n");
}
else


for (count = 1; count <=n; count = count + 1);
pintf("%d",count);
for(count=n; count>=1; count = count - 1)
printf(ā€œ%dā€, count);

return 0;
}

?

Does that work?
I think the limits of the loops need work, and that you need to consider the input=1 case, but it looks fine. ANd yer missing a ; in places. And you don't need the else. And you have a ; where you don't need one (end of the first for line).

Be a part of the DaniWeb community

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