how can I output 123454321, if i input 5. and 1234321 if i input 4?

Recommended Answers

All 12 Replies

use a loop that counts from 1 to the number you enter, then another loop that counts backwards back down to 1. Print the loop counter.

how can I output 123454321, if i input 5. and 1234321 if i input 4?

you could do that by following below

read count;

for(start = 1; start <= conut; start++)
          putchar( start + '0' );
for(start = start -2; start > 0; start--)
          putchar( start + '0' );

Haven't tested it yet, but it could be similar to this :

int M = 5;
int i = 1;
while(i <= M) cout << i++ <<" ";  //print from 1 to 5
while(--i > 0) cout << i << " "; //print from 4 to 1

you could do that by following below

read count;

for(start = 1; start <= conut; start++)
          putchar( start + '0' );
for(start = start -2; start > 0; start--)
          putchar( start + '0' );

this is what I have so far, but does not work:
#include <stdio.h>
int main()
{
int n
printf ("Enter digit: ");
scanf ("%d",&n);

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


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

return 0;
}

Ok here we go:

- you're missing the semi-colon after your declaration of the variable n.

- you haven't declared the variables i and count. And you don't need the count variable. Just print the value of i.

- remove the semi-colons from the end of your for statements - you have empty for statements here and that is why you are not getting the desired output.

- when counting back, initialize i to n-1.

- the code to print the required output should be part of an else clause - that way, if invalid data is entered, you don't end up printing erroneous values after printing your error message.

- in the future, use code tags when posting code - read the information at this link to learn how to do this:
http://www.daniweb.com/forums/thread93280.html

How can i print 123454321 (on input 5) in a single loop without any conditional statement...???

How can i print 123454321 (on input 5) in a single loop without any conditional statement...???

Is this a joke?

> in a single loop without any conditional statement.
But a loop without any conditional statement either runs zero times, or forever.

Anyway, here's one answer - have fun explaining it ;)
Or learn something, by trying to unravel it :)

#include <stdio.h>
int main ( ) {
  int num = 5;
  int i = 1, j = 1;
  while ( i > 0 ) {
    putchar(i|(0x60>>(i/i)));
    i+=i/i-((i<<(i/i))/i)*(j++-num>=0);
  }
  return 0;
}

I know I did, obfuscating it :twisted:

thanks dude... :)
this is really great...

commented: No problem - you didn't learn anything -4
public static void main(String a[]) {
		int num = 20;
		int sequence;
		for (int i = 1; i < 2*num; i++) {
			sequence = i>num ? 2*num - i : i;
			System.out.print(sequence);
		}
	}

Good job man! After only 10 months you came up with an answer in the wrong language. :icon_wink: Closed.

commented: LOL +34
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.