EmbLogic's Blog

Print the no. in increasing and decreasing order with a single line in code

Hi,
Yesterday I came across a problem. The problem was to print no.(s); firstly in increasing order then decreasing order. It is not a problem at all. But the real trick was, to print it using a single line of code. I took it as challenge, and designed  this code. After getting the desired output, I realized that now I’ve better understanding of ternary operators.
If anyone has some better code, then please share.
Thank you Emblogic.

#include<stdio.h>
int main()
{
int i=-1,j=0,c = 0,n;
printf(“Enter the no.: “);
scanf(“%d”,&n);
while((j<=2*n+1)&&printf(“%d, “, c==0 ? ( i==n ? printf(“\n”), c=i–: ++i) : i– , j++));
return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>