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
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)
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)
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)
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
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.
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.
main(){
int n=7,i;
for(i=1;in)*(2*n -i));
}
}
Is the abs() function allowed?
You can just do:
for i = 1 to 2n-1
print (n – abs(n-i));
Good question though!
int i, n = 7;
for( i = 1; i n)*(i-n)
Haroon bhai, you can delete this comment, i was just checking a thing.
int i, n = 7;
for( i = 1; i n)*(i-n)
Haroon bhai, you can delete this comment, i was just checking a thing.
int i, n = 7;
for( i = 1; i n)*(i-n)
Just analysing the problem in blog viewer window
int i, n = 7;
for( i = 1; i “”n)*(i-n)”
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) );
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
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)));
}
}
// 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.
// 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
Hi!,
for(int i=1; i<n*2; i++)
System.out.println(i-2*((i-n)*(i/n)));
abs(i) === (i^2)^(1/2)
Here are a couple more programming puzzles to puzzle you…
Good Luck!!
hello
ジョン·アブラハムとアビシェーク·バッチャンの両方が彼の服のほとんどあまりにとプラダラウンドサングラス任意のぎこちなさは心から自分のキャラクターを採用せず、ジョンの場合には、その阻害をドロップ [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]