Home > Computer Programming, Computer Science, Problem Solving/Puzzles > An interesting small programming puzzle.

An interesting small programming puzzle.

Abubakar Sultan who was my senior at NetSol and the best Microsoft.NET programmer I have seen so far in Pakistani industry, has made this programming question and asked many to solve this.

For any given n(positive integer only), you have to print integers from 1 to n and then print from n-1 to 1 using only a loop without any explicit if-else or ternary operators and not using any other memory location except loop variable.

Example: if n = 7 then out put should be 1 2 3 4 5 6 7 6 5 4 3 2 1

He claimed no one have been able to solve this in first try. However I solved this problem and come up with the follwoing solution. I am pasting the C++ source to print the same.

int n = 7 ; // set any integer value here

for( int i = 1; i  <= (2* n) ; i++)
     cout << (( i <=n) * i) + ((i > n) * (i – ( i – n ) * 2));

This is really an interesting programming puzzle and I enjoyed working on it.

Haroon

Advertisements
  1. Muhammad Rizwan Asghar
    August 17, 2006 at 7:17 am

    Firstly, as stated in description “print integers from 1 to n and then print from n-1 to 1” but your code is also printing 0 that is not required. So change for loop condition as
    “i n)*(i-n)

  2. Muhammad Rizwan Asghar
    August 17, 2006 at 7:27 am

    I am replying again because post is not viewable correctly.

    Firstly, as stated in description “print integers from 1 to n and then print from n-1 to 1” but your code is also printing 0 that is not required. So change for loop condition as

    “i n)*(i-n)

  3. Muhammad Rizwan Asghar
    August 17, 2006 at 7:31 am

    I am replying 3rd time because post is not viewable correctly.

    Firstly, as stated in description “print integers from 1 to n and then print from n-1 to 1” but your code is also printing 0 that is not required. So change for loop condition as
    “i n)*(i-n)

  4. Zaheer Ahmad
    August 30, 2006 at 5:08 am

    Slaam,
    Haroon bhai I was just reading your blog, suddenly I got this programming puzzle. I found it interesting and after almost half an hour, I had my own solution. I am sending you my code and I did not see your code until I had my own solution. One thing about my solutoin is that it contains only arithmetic operators and no comparison operators.

    for(int i =1;i

  5. Zaheer Ahmad
    August 30, 2006 at 5:09 am

    I think there is some problem. I did paste my code , but that is not viewable 😦
    Anyways, I also did it in my first attempt.

  6. Bob
    November 15, 2006 at 4:19 am

    It’s going to sound nuts but I’m trying to locate a Win CE programmer named Zaheer Ahmad out of India. Would that be you? If so, please go to the web site for Giraffe Publications (add .com) about a project if you’re interested. Sorry for being off subject here everyone… Small world, big internet… -B.

  7. kuldip
    November 22, 2006 at 11:45 am

    main(){
    int n=7,i;
    for(i=1;in)*(2*n -i));
    }

    }

  8. Rahul
    February 13, 2007 at 3:35 am

    Is the abs() function allowed?

    You can just do:
    for i = 1 to 2n-1
    print (n – abs(n-i));

    Good question though!

  9. Muhammad Rizwan Asghar
    March 15, 2007 at 12:16 pm

    int i, n = 7;
    for( i = 1; i n)*(i-n)

  10. blackishbubble
    March 15, 2007 at 12:20 pm
    int i, n = 7;
    for( i = 1; i n)*(i-n)
    

    Haroon bhai, you can delete this comment, i was just checking a thing.

  11. blackishbubble
    March 15, 2007 at 12:21 pm


    int i, n = 7;
    for( i = 1; i n)*(i-n)

    Haroon bhai, you can delete this comment, i was just checking a thing.

  12. Muhammad Rizwan Asghar
    March 15, 2007 at 1:02 pm

    int i, n = 7;
    for( i = 1; i n)*(i-n)

  13. Muhammad Rizwan Asghar
    March 15, 2007 at 1:14 pm

    Just analysing the problem in blog viewer window
    int i, n = 7;
    for( i = 1; i “”n)*(i-n)”

  14. Neil
    February 18, 2008 at 11:34 pm

    This is what I came up with, I haven’t tested it…

    int i, n=7;
    for( i=-(n-1); i <= (n-1); ++i)
    printf( “%d “, n-abs(i) );

  15. bruno
    March 21, 2008 at 7:29 pm

    Hi.

    I do not think your answer is correct or maybe I’m being to strict about “using only a loop without any explicit if-else or ternary operators and not using any other memory location except loop variable.” …

    because in c# you just could use

    for (int i = 1; i < 2 * n ; i++) {
    Console.WriteLine(“{0}”, (i <= 7)?i:(2*n-i));
    }

    i think it would be the same that when u compare, please let me know if u can.
    btw, loved ur blog, several interesting things,
    cheers.

    bruno from Lima, Peru

  16. cs
    May 5, 2008 at 9:38 pm

    Haroon is definitely wrong — he used a ternary operator that is explicitly disallowed.

    Neil is also wrong. No fair using abs() which just hides the ternary operator inside a subroutine.

    The solution you want is more like:

    #include
    int main() {
    int i, n = 7;
    for (i = 1; i < 2*n; i++) {
    printf(“%d “, i + (((i%(n+1))-i) * 2 * (i-n) / (n+1)));
    }
    }

  17. June 10, 2008 at 8:25 pm

    // pseudo code
    for i = (-n+1) to (n-1)
    print (n+i)*(i0) // prints n-1 to 1

    //example:
    for n = 5:

    for i = -4 to +4
    print (5+i)*(i0) // prints 4 to 1

    But I think using (i0) is like using conditional statements. This does not work in Java for example.

    Can be done is abs() but could be done without that too.

  18. June 10, 2008 at 8:28 pm

    // pseudo code
    for i = (-n+1) to (n-1)
    print (n+i)*(i less than or equals 0) // prints 1 to n
    + (n-i)*(i greater than 0) // prints n-1 to 1

  19. September 1, 2008 at 3:41 am

    Hi!,

  20. yakup
    April 17, 2009 at 12:26 pm

    for(int i=1; i<n*2; i++)
    System.out.println(i-2*((i-n)*(i/n)));

  21. Someguy
    June 18, 2009 at 8:41 am

    abs(i) === (i^2)^(1/2)

  22. April 4, 2010 at 1:44 pm
  23. kuhoo
    September 20, 2013 at 5:45 pm

    hello

  24. Goriamorp
    November 28, 2013 at 7:20 am

    ジョン·アブラハムとアビシェーク·バッチャンの両方が彼の服のほとんどあまりにとプラダラウンドサングラス任意のぎこちなさは心から自分のキャラクターを採用せず、ジョンの場合には、その阻害をドロップ [url=http://www.support-m-and-m.com/chanluu1.php]chanluu 松潤[/url]
    彼女はいつも映画に仕事したかったので、ビュフォードは、しばしば、ヴォーグを中心に映画をやって思った [url=http://www.ohmasa.jp/blog/data/chanluu2.php]チャンルー ブレスレット 激安[/url]
    私はすべての34ヶ月かそこらの安い財布を購入する傾向がある、とプラダVITELLO Dainoの彼らは月の罰金見て、その後、彼らはバラバラにし始める [url=http://www.support-m-and-m.com/chanluu1.php]chanluu ブレスレット[/url]
    パーマ道は、私が今、亜鉛カーゴに社名を変更し、航空券を提供する過程でもありましたを参照してくださいロンドンフレイトフォワーダーの到着を見ました [url=http://www.ohmasa.jp/blog/data/chanluu2.php]chan luu ブレスレット[/url]

  1. October 31, 2006 at 4:23 am
  2. February 5, 2007 at 1:46 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: