C# Sharp Exercises: Convert a decimal number to binary without using an array
C# Sharp For Loop: Exercise-41 with Solution
Write a program in C# Sharp to convert a decimal number into binary without using an array.
Pictorial Presentation:
Sample Solution:
C# Sharp Code:
using System;
public class Exercise41
{
public static void Main()
{
int n, i, j, binno=0,dn;
Console.Write("\n\n");
Console.Write("Convert a decimal number to binary without using array:\n");
Console.Write("---------------------------------------------------------");
Console.Write("\n\n");
Console.Write("Enter a number to convert : ");
n = Convert.ToInt32(Console.ReadLine());
dn=n;
i=1;
for(j=n;j>0;j=j/2)
{
binno=binno+(n%2)*i;
i=i*10;
n=n/2;
}
Console.Write("\nThe Binary of {0} is {1}.\n\n",dn,binno);
}
}
Sample Output:
Convert a decimal number to binary without using array: --------------------------------------------------------- Enter a number to convert : 65 The Binary of 65 is 1000001.
Flowchart:
C# Sharp Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a C# Sharp Program to display the following pattern using the alphabet.
Next: Write a program in C# Sharp to convert a binary number into a decimal number without using an array, function and while loop.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework