How to get a while loop in C# to print on a single line [duplicate]
This question already has an answer here:
How can I print a string to the console without a newline at the end?
4 answers
This is for a homework assignment, and while I've found some answers on getting the console to write line in the output, they've either been for a different programming language or affected how the console writes the out put vertically.
My code
int i=1
while (i<22)
{
if (i%2 ==1)
Console.WriteLine(i);
Console.WriteLine("t");
i++;
}
The tutorials I've found still have everything printing vertically. Can I get them to print horizontally
c#
marked as duplicate by mjwills, TheGeneral
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 12 at 4:15
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How can I print a string to the console without a newline at the end?
4 answers
This is for a homework assignment, and while I've found some answers on getting the console to write line in the output, they've either been for a different programming language or affected how the console writes the out put vertically.
My code
int i=1
while (i<22)
{
if (i%2 ==1)
Console.WriteLine(i);
Console.WriteLine("t");
i++;
}
The tutorials I've found still have everything printing vertically. Can I get them to print horizontally
c#
marked as duplicate by mjwills, TheGeneral
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 12 at 4:15
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
6
There isConsole.Write(...)
method
– vasily.sib
Nov 12 at 3:11
add a comment |
This question already has an answer here:
How can I print a string to the console without a newline at the end?
4 answers
This is for a homework assignment, and while I've found some answers on getting the console to write line in the output, they've either been for a different programming language or affected how the console writes the out put vertically.
My code
int i=1
while (i<22)
{
if (i%2 ==1)
Console.WriteLine(i);
Console.WriteLine("t");
i++;
}
The tutorials I've found still have everything printing vertically. Can I get them to print horizontally
c#
This question already has an answer here:
How can I print a string to the console without a newline at the end?
4 answers
This is for a homework assignment, and while I've found some answers on getting the console to write line in the output, they've either been for a different programming language or affected how the console writes the out put vertically.
My code
int i=1
while (i<22)
{
if (i%2 ==1)
Console.WriteLine(i);
Console.WriteLine("t");
i++;
}
The tutorials I've found still have everything printing vertically. Can I get them to print horizontally
This question already has an answer here:
How can I print a string to the console without a newline at the end?
4 answers
c#
c#
edited Nov 12 at 3:11
Shiladitya
9,39391830
9,39391830
asked Nov 12 at 3:09
KnightSword
61
61
marked as duplicate by mjwills, TheGeneral
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 12 at 4:15
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by mjwills, TheGeneral
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 12 at 4:15
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
6
There isConsole.Write(...)
method
– vasily.sib
Nov 12 at 3:11
add a comment |
6
There isConsole.Write(...)
method
– vasily.sib
Nov 12 at 3:11
6
6
There is
Console.Write(...)
method– vasily.sib
Nov 12 at 3:11
There is
Console.Write(...)
method– vasily.sib
Nov 12 at 3:11
add a comment |
3 Answers
3
active
oldest
votes
You could use the Console.Write(...)
method as Vasily mentions above, or you could create the output string inside a for loop then output the string as a line. I have also changed it to use an inline statement and a for loop. ie:
int i=1;
string output="";
for(int count = 0; count < 22; count++) {
output += i % 2 == 1 ? i : "t"; }
console.WriteLine(output);
The advantage if you used the Console.Write()
command in the above code would be shortened by a line, I just prefer to get my ducks in a row before outputting anything.
add a comment |
here is an example of recursive function
private void Print(int counter, int end)
{
if (counter > end)
return;
if (counter % 2 == 1)
Console.Write(counter);
Console.Write("t");
counter++;
Print(counter, end);
}
then call Print(1,22)
add a comment |
try this
int i = 1;
while (i < 22)
{
if (i % 2 == 1)
Console.Write(i+"t");
i++;
}
out put 1 3 5 7 9 11 13 15 17 19 21
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could use the Console.Write(...)
method as Vasily mentions above, or you could create the output string inside a for loop then output the string as a line. I have also changed it to use an inline statement and a for loop. ie:
int i=1;
string output="";
for(int count = 0; count < 22; count++) {
output += i % 2 == 1 ? i : "t"; }
console.WriteLine(output);
The advantage if you used the Console.Write()
command in the above code would be shortened by a line, I just prefer to get my ducks in a row before outputting anything.
add a comment |
You could use the Console.Write(...)
method as Vasily mentions above, or you could create the output string inside a for loop then output the string as a line. I have also changed it to use an inline statement and a for loop. ie:
int i=1;
string output="";
for(int count = 0; count < 22; count++) {
output += i % 2 == 1 ? i : "t"; }
console.WriteLine(output);
The advantage if you used the Console.Write()
command in the above code would be shortened by a line, I just prefer to get my ducks in a row before outputting anything.
add a comment |
You could use the Console.Write(...)
method as Vasily mentions above, or you could create the output string inside a for loop then output the string as a line. I have also changed it to use an inline statement and a for loop. ie:
int i=1;
string output="";
for(int count = 0; count < 22; count++) {
output += i % 2 == 1 ? i : "t"; }
console.WriteLine(output);
The advantage if you used the Console.Write()
command in the above code would be shortened by a line, I just prefer to get my ducks in a row before outputting anything.
You could use the Console.Write(...)
method as Vasily mentions above, or you could create the output string inside a for loop then output the string as a line. I have also changed it to use an inline statement and a for loop. ie:
int i=1;
string output="";
for(int count = 0; count < 22; count++) {
output += i % 2 == 1 ? i : "t"; }
console.WriteLine(output);
The advantage if you used the Console.Write()
command in the above code would be shortened by a line, I just prefer to get my ducks in a row before outputting anything.
answered Nov 12 at 3:20
Slipoch
362213
362213
add a comment |
add a comment |
here is an example of recursive function
private void Print(int counter, int end)
{
if (counter > end)
return;
if (counter % 2 == 1)
Console.Write(counter);
Console.Write("t");
counter++;
Print(counter, end);
}
then call Print(1,22)
add a comment |
here is an example of recursive function
private void Print(int counter, int end)
{
if (counter > end)
return;
if (counter % 2 == 1)
Console.Write(counter);
Console.Write("t");
counter++;
Print(counter, end);
}
then call Print(1,22)
add a comment |
here is an example of recursive function
private void Print(int counter, int end)
{
if (counter > end)
return;
if (counter % 2 == 1)
Console.Write(counter);
Console.Write("t");
counter++;
Print(counter, end);
}
then call Print(1,22)
here is an example of recursive function
private void Print(int counter, int end)
{
if (counter > end)
return;
if (counter % 2 == 1)
Console.Write(counter);
Console.Write("t");
counter++;
Print(counter, end);
}
then call Print(1,22)
answered Nov 12 at 3:32
Z.R.T.
1,047511
1,047511
add a comment |
add a comment |
try this
int i = 1;
while (i < 22)
{
if (i % 2 == 1)
Console.Write(i+"t");
i++;
}
out put 1 3 5 7 9 11 13 15 17 19 21
add a comment |
try this
int i = 1;
while (i < 22)
{
if (i % 2 == 1)
Console.Write(i+"t");
i++;
}
out put 1 3 5 7 9 11 13 15 17 19 21
add a comment |
try this
int i = 1;
while (i < 22)
{
if (i % 2 == 1)
Console.Write(i+"t");
i++;
}
out put 1 3 5 7 9 11 13 15 17 19 21
try this
int i = 1;
while (i < 22)
{
if (i % 2 == 1)
Console.Write(i+"t");
i++;
}
out put 1 3 5 7 9 11 13 15 17 19 21
answered Nov 12 at 3:46
Dhanushka Dayawansha
32619
32619
add a comment |
add a comment |
6
There is
Console.Write(...)
method– vasily.sib
Nov 12 at 3:11