Q:

Write a program in C# Sharp to convert an octal number into binary

0

Write a program in C# Sharp to convert an octal number into binary

Sample Output:

Convert Octal to Binary:                                                                                    
--------------------------                                                                                  
Input an octal number (using digit 0 - 7) :45                                                               
The Octal Number : 45                                                                                       
The equivalent Binary  Number : 100101

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

using System;  
public class Exercise54
{  
    public static void Main()
{


     int n1, n5,p=1;
     int dec=0,i=1,j,d;
     int binno=0;
	 
	Console.Write("\n\n");
    Console.Write("Convert Octal to Binary:\n");
    Console.Write("--------------------------");
    Console.Write("\n\n");	 


	Console.Write("Input an octal number (using digit 0 - 7) :");
    n1 = Convert.ToInt32(Console.ReadLine());	
	n5=n1;



	for (j=n1;j>0;j=j/10)
	{  
          d = j % 10;
            if(i==1)
                  p=p*1;
            else
                 p=p*8;

	   dec=dec+(d*p);
	   i++;
	}

/*------------------------------------------------------------------------------*/
     i=1;

      for(j=dec;j>0;j=j/2)
       {
        binno=binno+(dec % 2)*i;
        i=i*10;
        dec=dec/2;
       }
     
        Console.Write("\nThe Octal Number : {0}\nThe equivalent Binary  Number : {1} \n\n",n5,binno);
}
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now