Problem statement:- Program to convert octal to binary using a while loop.
Data requirement:-
Input Data:- octal
Output Data:-binary
Output Data:-binary
Program in C
Here is the source code of the C Program to convert octal to binary using a while loop.
Code:
Code:
#include <stdio.h>
#include <math.h>
int
main ()
{
int octal;
printf ("Enter octal number: ");
scanf ("%d", &octal);
int decimal = 0, i = 0;
int binary = 0;
while (octal != 0)
{
decimal = decimal + (octal % 10) * pow (8, i);
i++;
octal = octal / 10;
}
i = 1;
while (decimal != 0)
{
binary = binary + (decimal % 2) * i;
decimal = decimal / 2;
i = i * 10;
}
printf ("Binary number is: %d", binary);
return 0;
}
Input/Output:
Enter octal number: 105
Binary number is: 1000101
Program in C++
Here is the source code of the C++ Program to convert octal to binary using a while loop.
Code:
Code:
#include <iostream>
#include <cmath>
using namespace std;
int
main ()
{
int octal;
cout<<"Enter octal number: ";
cin>>octal;
int decimal = 0, i = 0;
int binary = 0;
while (octal != 0)
{
decimal = decimal + (octal % 10) * pow (8, i);
i++;
octal = octal / 10;
}
i = 1;
while (decimal != 0)
{
binary = binary + (decimal % 2) * i;
decimal = decimal / 2;
i = i * 10;
}
cout<<"Binary number is: "<<binary;
return 0;
}
Input/Output:
Enter octal number: 445
Binary number is: 100100101
Program in Java
Here is the source code of the C Program to convert octal to binary using a while loop.
Code:
Code:
import java.util.Scanner;
public class OctalToBinary {
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int octal;
System.out.println("Enter octal number: ");
octal=cs.nextInt();
int decimal = 0, i = 0;
int binary = 0;
while (octal != 0)
{
decimal = (int) (decimal + (octal % 10) * Math.pow (8, i));
i++;
octal = octal / 10;
}
i = 1;
while (decimal != 0)
{
binary = binary + (decimal % 2) * i;
decimal = decimal / 2;
i = i * 10;
}
System.out.println("Binary number is: "+ binary);
cs.close();
}
}
Input/Output:
Enter octal number: 445
Binary number is: 100100101
Program in Python
Here is the source code of the C Program to convert octal to binary using a while loop.
Code:
Code:
print("Enter octal number: ")
octal=int(input())
decimal = 0
i = 0
binary = 0
while (octal != 0):
decimal = decimal + (octal % 10) * pow (8, i)
i+=1
octal = octal // 10
i = 1
while (decimal != 0):
binary = binary + (decimal % 2) * i
decimal = decimal // 2
i = i * 10
print ("Binary number is: ", binary)
decimal = 0
i = 0
binary = 0
while (octal != 0):
decimal = decimal + (octal % 10) * pow (8, i)
i+=1
octal = octal // 10
i = 1
while (decimal != 0):
binary = binary + (decimal % 2) * i
decimal = decimal // 2
i = i * 10
print ("Binary number is: ", binary)
Input/Output:
Enter octal number:
787
Binary number is: 1000000111
Most Recommend Questions:-
More Questions:-
0 Comments
Please do not Enter any spam link in the comment box