Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

No output for loop program

The scope is to output the numbers from 1 to (input number) with the multiples of 3 being a * each. E.g. for 8 as input: 1,2,*,4,5,*,7,8. I tried to do this with a for loop but it has no output for some reason.

using System;
using System.Collections.Generic;


namespace HelpPleaseStackOverFlow
{
    class Program
    {
        static void Main(string[] args)
        {
            int number = Convert.ToInt32(Console.ReadLine());
            int x;
            for (x = 1; x <= number; ) 
                if (x % 3 == 0)
                {
                    Console.WriteLine("*");
                }
                else;
                {
                    Console.WriteLine(x); 
                    x = x++;
                }
        }
    }
}

I tried converting the x variable (integer) to a string and then making it the * that should be printed out, in the first half of the IF statement and the last part with the ELSE basically the same.

I also tried to make the for statement like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

for (x = 1; x <= number; x++)

with the x++ at the end but this just game me the result of ,,8. With an input of 7; 8 should not be part of this and I have no idea why it put 8 as an output.

>Solution :

most of the erroros are that you are lacking some {} and ()

 String input = Console.ReadLine();

        int number;

        int.TryParse(input, out number);

        for (int x = 1; x <= number; ++x)
        {
            if ((x % 3) == 0)
            {
                Console.WriteLine("*");
            }
            else
            {
                Console.WriteLine(x);

            }
        }
        Console.ReadLine();

Hope it helps.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading