C# - Check if a Number is Binary or not
This program takes an input from the user and checks if it is a Binary Number. That is, if the input contains only 1's and 0's
CODE
using System;
class Program
{
public static void Main(string[] args)
{
Console.Write("Enter a Binary Number: ");
string binaryNumber = Console.ReadLine();
binaryNumber = binaryNumber.Trim();
bool isBinaryNumber = true;
for(int i = 0; i < binaryNumber.Length; i++)
{
if (binaryNumber[i] != '1' && binaryNumber[i] != '0')
{
isBinaryNumber = false;
break;
}
}
Console.WriteLine("Is Binary Number? " + isBinaryNumber);
Console.ReadKey();
}
}
INPUT & OUTPUT
Comments
Post a Comment