Hello, I’m working on this task:
Write a program that reads an integer from the console. The integer should be a power of two: 2, 4, 8, 16, 32, etc. Then your program should divide it by 2 and output every division result to the console, until it reaches number 1. Print out each result to a new line. Use a while loop. For example:
>16
8
4
2
1
Here is my code:
namespace WhileLoop
{
class InfiniteDividor
{
static void Main(string[] args)
{
using System;
class Program
{
static void Main(string[] args)
{
int n = int.Parse(Console.ReadLine());
if ((n & (n - 1)) != 0)
{
return;
}
while (n > 1)
{
Console.WriteLine(n);
n /= 2;
}
}
}
Can anyone please help me to solve it?