This C# Program converts a binary number to an equivalent octal number.
Example: Program to convert Binary to Octal
In this program, user is asked to enter the binary number and the program then converts that binary number to the octal number by calling a user defined function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
using System; public class Exercise53 { public static void Main() { int n1, n,p=1; int dec=0,i=1,j,d; int ocno=0; Console.Write("\n\n"); Console.Write("Convert binary number into octal:\n"); Console.Write("-----------------------------------"); Console.Write("\n\n"); Console.Write("Input a binary number :"); n = Convert.ToInt32(Console.ReadLine()); n1=n; for (j=n;j>0;j=j/10) { d = j % 10; if(i==1) p=p*1; else p=p*2; dec=dec+(d*p); i++; } /*--------------------------------------------*/ i=1; for(j=dec;j>0;j=j/8) { ocno=ocno+(j % 8)*i; i=i*10; n=n/8; } Console.Write("\nThe Binary Number : {0}\nThe equivalent Octal Number : {1} \n\n",n1,ocno); } } |
Output:
1 2 3 4 5 6 7 |
Convert binary number into octal: ----------------------------------- Input a binary number :110101 The Binary Number : 110101 The equivalent Octal Number : 65 |