w3resource
C# Sharp Programming Exercises

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:

C# Sharp Exercises: Convert a decimal number to binary without using an array

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:

Flowchart: Convert a decimal number to binary without using array

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?



New Content: Composer: Dependency manager for PHP, R Programming