-
[2.0] Could this code be impossible to write??
Hello,
Let's say the user inputs the number 7. THe program will output this:
Code:
7
66
555
4444
33333
222222
1111111
The rules my teacher stated is ONLY for loops. No if statements, just for loops. You can nest a for loop but that is all. Can anyone solve this?
-
Re: [2.0] Could this code be impossible to write??
Loop backwards using a counter variable to increment the printing of the loop value.
-
Re: [2.0] Could this code be impossible to write??
Code:
For i = 7 to 1 step -1
for x = 1 to 7
conmsole.writeline(i);
next
next
Something like that.
-
Re: [2.0] Could this code be impossible to write??
rob u could rack up a triple post if u translated that to c#. nah i got it don't worry thanks i'll try it out
-
Re: [2.0] Could this code be impossible to write??
yea it doesn't work i just get this as a result:
Code:
7777777
6666666
5555555
4444444
3333333
2222222
1111111
using... this code:
Code:
for (int i = 7; i >= 1; i--) {
for (int x = 1; x <= 7; x++) {
System.out.print(i);
}
System.out.println();
}
-
Re: [2.0] Could this code be impossible to write??
lol, naw. I just thought some pseudocode would be better explaination.
The write needs to write on the same line. In VB the semicolon identifies a print continuation. Oh add a new line print after the inner loop to get it to move to the next line.
Im sure there may be better .net ways to do it but this was my first thought. :D
Edit: Should be x = 1 to z where z would be 7 - i
-
Re: [2.0] Could this code be impossible to write??
c# Code:
for(int i = 7; i >= 1; --i)
{
for(int x = 7; x >= i; --x)
{
Console.Write(i);
}
Console.WriteLine();
}
Alternatively:
c# Code:
for(int i = 7; i >= 1; --i)
{
for(int x = 0; x <= 7-i; ++x)
{
Console.Write(i);
}
Console.WriteLine();
}
-
Re: [2.0] Could this code be impossible to write??
Code:
for i = 7 to 1 step -1
z = z + 1
for x = 1 to z
conmsole.writeline(i);
next
next
-
Re: [2.0] Could this code be impossible to write??
gracias!! it works great:
Code:
for (int i = n; i >= 1; --i) {
for (int x = n; x >= i; --x) {
System.out.print(i);
}
System.out.println();
}
-
Re: [2.0] Could this code be impossible to write??
without nested loops:
Code:
int iNum = 0;
Console.WriteLine("Input A Value");
string sNum = Console.ReadLine();
if (int.TryParse(sNum, out iNum))
{
string Buffer = "";
for (int i = iNum; i > 0; --i)
{
Console.WriteLine(Buffer = new String(i.ToString()[0], iNum - i + 1));
}
}
else
{
Console.WriteLine("No Good!");
}
Console.ReadLine();
(i'm answering this as much for my benefit as the posters - so if anyone can tell me if i'm doing something i shouldn't be, I'd like to hear it)
edit: slight correction (+1)
-
Re: [2.0] Could this code be impossible to write??
Code:
Buffer = new String(i.ToString()[0], iNum - i + 1)
-
Re: [2.0] Could this code be impossible to write??
-
Re: [2.0] Could this code be impossible to write??
the connection is too slow today. :(
Furthermore, the i.ToString()[0] won't work good if number is a more than 9. It will display only the first digit.
I don't know if it matters.
-
Re: [2.0] Could this code be impossible to write??
Quote:
Originally Posted by Harsh Gupta
tFurthermore, the i.ToString()[0] won't work good if number is a more than 9. It will display only the first digit.
I don't know if it matters.
yeah, I was aware of that - I assumed the question only meant for single digit numbers, otherwise the triangle doesn't make sense.
-
Re: [2.0] Could this code be impossible to write??
My original logic code example in C3 (finally lol). :D
c# Code:
private void button1_Click(object sender, EventArgs e)
{
int z = 1;
for(int i = 7; i >= 1; --i)
{
z = 7-i;
for (int x = 0; x <= z; --z)
{
textBox1.Text = textBox1.Text + i;
}
textBox1.Text = textBox1.Text + Environment.NewLine;
}
}
-
Re: [2.0] Could this code be impossible to write??
How to do it on a WinForm ? (as there is no Print method)
-
Re: [2.0] Could this code be impossible to write??
Quote:
Originally Posted by iPrank
How to do it on a WinForm ? (as there is no Print method)
using GDI+, look for Graphics.DrawString() (+6 overloaded) functions. Works for functions having PaintEventArgs parameter.
-
Re: [2.0] Could this code be impossible to write??
Why would you want to draw this directly on a form?
-
Re: [2.0] Could this code be impossible to write??
cuz everyone loves a big number triangle as a form background picture!
-
Re: [2.0] Could this code be impossible to write??
So what grade did we get? :lol:
Maybe we should have a homework forum? :D
-
Re: [2.0] Could this code be impossible to write??
Quote:
Originally Posted by Fromethius
Hello,
Let's say the user inputs the number 7. THe program will output this:
Code:
7
66
555
4444
33333
222222
1111111
The rules my teacher stated is ONLY for loops. No if statements, just for loops. You can nest a for loop but that is all. Can anyone solve this?
Code:
Console.Write("7\n66\n555\n4444\n33333\n222222\n1111111\n");
:D
-
Re: [2.0] Could this code be impossible to write??
Bad wossname, you didn't use a for() loop.
Code:
for (;false;);
Console.Write("7\n66\n555\n4444\n33333\n222222\n1111111\n");
-
Re: [2.0] Could this code be impossible to write??
Quote:
Originally Posted by penagate
Bad wossname, you didn't use a for() loop.
Code:
for (;false;);
Console.Write("7\n66\n555\n4444\n33333\n222222\n1111111\n");
Code:
for (int i = 0; i == 0; ++i)
{
Console.WriteLine("7\n66\n555\n4444\n33333\n222222\n1111111\n");
}
there...solved taht problem! :D
-
1 Attachment(s)
Re: [2.0] Could this code be impossible to write??
I wonder if you guys can print this output without using an if statement (the ?: operator is not allowed either)...
-
Re: [2.0] Could this code be impossible to write??
Console.Write("18\n18\n16 16 14\n14\n12 12 10\n10\n8 8 6\n6\n4 4 2\n2\n0 0"); :afrog:
-
Re: [2.0] Could this code be impossible to write??
Do it properly smartarse.
-
Re: [2.0] Could this code be impossible to write??
Quote:
Originally Posted by wossname
Do it properly smartarse.
Haha..smartass says the guy that tried to pull that stunt to begin with
-
Re: [2.0] Could this code be impossible to write??
Yeah but the first time it was original.
-
Re: [2.0] Could this code be impossible to write??
Quote:
Originally Posted by RobDog888
So what grade did we get? :lol:
Maybe we should have a homework forum? :D
Twas extra credit. I now have a 105 in programming class