Hey frndz...cn any1 giv me code for dis o/p :
----1
---121
-12312
1234123
-12312
---121
----1
i tried alot bt i didn't gt dis o/p
Hey frndz...cn any1 giv me code for dis o/p :
----1
---121
-12312
1234123
-12312
---121
----1
i tried alot bt i didn't gt dis o/p
Could you please post one of your trials?(the one you think it's best)
bt m tring it in visual studio s/w....i send u code frm tht...u ok wid it na?
also till dis m jst tring to print upper triangle only....
Ok,try to write one post at the time
Copy the code you have in VS and paste it like this
[key]HERE YOU PASTE YOUR CODE[/key]
in order this to work replace key with code
Code:class Program { static void Main(string[] args) { int p = 1; int sp = 3; int v = 1; for (int i = 1; i <= 4; i++) { int c = v; for (int j = 1; j <= sp; j++) Console.Write(" "); for (int k = 1; k <= p; k++) { Console.Write(c); if (k <= (p / 2)) { c++; } else { c--; } } Console.WriteLine(); p = p + 2; sp = sp - 1; } Console.ReadLine(); } }
dis code will giv o/p lik dis:
----1
---121
-12321
1234321
bt i wnt o/p lik dis:
----1
---121
-12312
1234123
If i were you i would use a flag.Below i will show you how i use it.The problem was that c should not be decremented but set again to 1.
The flag was used because we want c to set again to 1 only once.The increment of c by one,is now done without any condition because we want it to be executed at every loop.If you have a question ask !Code:class Program { static void Main(string[] args) { int p = 1; int sp = 3; int v = 1; bool flag; for (int i = 1; i <= 4; i++) { int c = v; for (int j = 1; j <= sp; j++) Console.Write(" "); flag = true; for (int k = 1; k <= p; k++) { Console.Write(c); c++; if(k>(p/2) && (flag)){ c=1; flag=false; } } Console.WriteLine(); p = p + 2; sp = sp - 1; } Console.ReadLine(); } }
oh thnx....it shows correct o/p... now how to print remaining part?
At first you need set line 9 to execute the loop 8 times,not 4
Then we have to think that all we have to do is follow the reverse way that brought us up to here(this way was found by you so bravo!!).So we should focus on lines 26-27.We have to do the opposite thing now.But when should we reverse our method?When sp becomes zero,because this means that the big 1234321 o/p has been produced.So
But i would suggest to use another bool variable in order to use it again as a flag to inform us wich method should we follow.The reverse or the other one?Code:p = p + 2; sp = sp - 1; //the reverse p-=2;// p = p-2; sp++;
So what i am saying is this
Code:class Program { static void Main(string[] args) { int p = 1; int sp = 3; int v = 1; bool flag; bool mirror=false; for (int i = 1; i <= 4; i++) { int c = v; for (int j = 1; j <= sp; j++) Console.Write(" "); flag = true; for (int k = 1; k <= p; k++) { Console.Write(c); c++; if(k>(p/2) && (flag)){ c=1; flag=false; } } Console.WriteLine(); if(sp==0) mirror=true; if(mirror) { p-=2; sp++; } else { p = p + 2; sp = sp - 1; } } Console.ReadLine(); } }
OMG..... gr888888.....thnxxxx alot....finally i got d solution for dis... & bt offcures bcz of YOU only!!!!! thnx once again....
The important thing is that you understand why this works
PS -> welcome to the forum