Está en la página 1de 3

7/7/2014 C Program to Convert Binary Number to Octal and Octal to Binary

Home C Programming C++ Python

C Program to Convert Binary Number to Octal and


Octal to Binary

Learn Ethical Hacking


innobuzz.in/Hacking
Training Program on Cyber Ethical Hacking & Information Security!
This program converts either binary number entered by user to octal number or octal number entered
by user to binary number in accordance with the character entered by user.

Connect With Us
Source Code to Convert Binary to Octal and Vice Versa
Facebook

/* C programming source code to convert either binary to octal or octal to binary acco

Twitter #include <stdio.h>


#include <math.h>
int binary_octal(int n);
Google+ int octal_binary(int n);
int main()
{
int n;
Game Development char c;
printf("Instructions:\n");
C Programming Examples printf("Enter alphabet 'o' to convert binary to octal.\n");
printf("2. Enter alphabet 'b' to convert octal to binary.\n");
C Programming Tutor scanf("%c",&c);
if ( c=='o' || c=='O')
Basic C Programming
{
printf("Enter a binary number: ");
Application Development
scanf("%d",&n);
printf("%d in binary = %d in octal", n, binary_octal(n));
Data Structures In C
}
ads
if ( c=='b' || c=='B')
{
printf("Enter a octal number: ");
scanf("%d",&n);
printf("%d in octal = %d in binary",n, octal_binary(n));
}
return 0;
}
int binary_octal(int n) /* Function to convert binary to octal. */
{
int octal=0, decimal=0, i=0;
while(n!=0)
{
decimal+=(n%10)*pow(2,i);
++i;
n/=10;
}

/*At this point, the decimal variable contains corresponding decimal value of binary nu

i=1;
while (decimal!=0)
{
octal+=(decimal%8)*i;
decimal/=8;
i*=10;

http://www.programiz.com/c-programming/examples/octal-binary-convert 1/3
7/7/2014 C Program to Convert Binary Number to Octal and Octal to Binary
}
return octal;
}
int octal_binary(int n) /* Function to convert octal to binary.*/
{
int decimal=0, binary=0, i=0;
while (n!=0)
{
decimal+=(n%10)*pow(8,i);
++i;
n/=10;
}
/* At this point, the decimal variable contains corresponding decimal value of that oc
i=1;
while(decimal!=0)
{
binary+=(decimal%2)*i;
decimal/=2;
i*=10;
}
return binary;
}

Output

This program asks user to enter alphabet 'b' to convert octal number to binary or alphabet 'o' to
convert binary number to octal. In accordance with the character entered, user is asked to enter
either binary value to convert to octal or octal value to convert to binary.

To perform conversion, two functions are made octal_binary(); to convert octal to binary and
binary_octal(); to convert binary to octal. Octal number entered by user is passed to octal_binary() and
this function computes the binary value of that octal number number and returns it main() function.
Similarly, binary number is passed to function binary_octal() and this function computes octal value of
that number and return it main() function.

Application Development C Programming Examples

C Programming Patterns Flow Chart Software

C Language Software Best Antivirus Software

Data Structures In C Free Software Downloads

Graphic Design C Programming To Calculate

Online Programming Classes Learn C Programming


ads

Similar Examples

C Program to Convert Octal Number to Decimal and Decimal to Octal

C Program to Convert Binary Number to Decimal and Decimal to Binary

C Program to Convert Binary Number to Hexadecimal Vice Versa

C Program to Convert Hexadecimal to Octal and Vice Versa

C Program to Change Decimal to Hexadecimal Number and Vice Versa

C Program to Find Size of int, float, double and char of Your System

http://www.programiz.com/c-programming/examples/octal-binary-convert 2/3
7/7/2014 C Program to Convert Binary Number to Octal and Octal to Binary

C Program to Print a Integer Entered by a User

C Program to Find Quotient and Remainder of Two Integers Entered by User

About Us | Contact Us | Articles | Advertise With Us

Copyright by Programiz | All rights reserved | Privacy Policy

http://www.programiz.com/c-programming/examples/octal-binary-convert 3/3

También podría gustarte