0
Multiplication of 3 project
I'm having issues with my code. I don't believe I know a way of replacing the multiples of 3 with asteriks. I have the asteriks placed by the multiples of 3 though. Anything I'm missing? static void Main(string[] args) { int N = Convert.ToInt32(Console.ReadLine()); while(N>0) { if ((N%3)==0) Console.WriteLine("*"); Console.WriteLine(N); N--; }
5 Antworten
+ 4
In that case, you simply add `else`
if (N % 3) == 0)
Console.WriteLine("*");
else
Console.WriteLine(N);
+ 5
When <N> is fully divisible by 3, do you want to print <N> with (or followed by) the '*', or just print the '*'?
I assume you want to print <N> only when <N> is not fully divisible by 3?
+ 2
Samuel Miller
I'm glad to hear that. I was also unsure about that suggestion, because I've seen people printing the <N> (be fully divisible by 3), followed by an '*' (on the next line) in their countdown project. Like these (starting from 10)
10
9
*
8
7
... and so on
If you're not too comfortable with unguarded `if..else` block, you can wrap the block sections with curly braces. Curly braces clearly defines code block structure. And at the same time, also improves code redability 👍
if((N % 3) == 0)
{
// print '*'
}
else
{
// print <N>
}
+ 1
Ipang yes that is correct. <N> should be printed when not divisible by 3, and "*" should replace <N> when its divisible by 3.
+ 1
Ipang One word to fix my entire code! I was unsure if the decrement would work being under "else", but it worked just fine. Thanks so much!