How to get a while loop in C# to print on a single line [duplicate]












0















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










share|improve this question















marked as duplicate by mjwills, TheGeneral c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

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 is Console.Write(...) method
    – vasily.sib
    Nov 12 at 3:11
















0















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










share|improve this question















marked as duplicate by mjwills, TheGeneral c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

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 is Console.Write(...) method
    – vasily.sib
    Nov 12 at 3:11














0












0








0








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










share|improve this question
















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#






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

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 c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

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 is Console.Write(...) method
    – vasily.sib
    Nov 12 at 3:11














  • 6




    There is Console.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












3 Answers
3






active

oldest

votes


















0














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.






share|improve this answer





























    0














    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)






    share|improve this answer





























      0














      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






      share|improve this answer




























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        0














        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.






        share|improve this answer


























          0














          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.






          share|improve this answer
























            0












            0








            0






            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.






            share|improve this answer












            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.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 12 at 3:20









            Slipoch

            362213




            362213

























                0














                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)






                share|improve this answer


























                  0














                  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)






                  share|improve this answer
























                    0












                    0








                    0






                    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)






                    share|improve this answer












                    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)







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 12 at 3:32









                    Z.R.T.

                    1,047511




                    1,047511























                        0














                        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






                        share|improve this answer


























                          0














                          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






                          share|improve this answer
























                            0












                            0








                            0






                            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






                            share|improve this answer












                            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







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 12 at 3:46









                            Dhanushka Dayawansha

                            32619




                            32619















                                Popular posts from this blog

                                Florida Star v. B. J. F.

                                Error while running script in elastic search , gateway timeout

                                Adding quotations to stringified JSON object values