Problem statement:- Program to convert decimal to octal using while loop.
Data requirement:-
Input Data:- number
Output Data:-octal
Output Data:-octal
Program in C
Here is the source code of the C Program to convert decimal to octal using a while loop.
Code:
Code:
#include <stdio.h>
int
main ()
{
int rem, number,sem=1,octal=0;
printf ("Enter the Decimal Number:");
scanf ("%d",&number);
while(number !=0)
{
octal=octal+(number%8)*sem;
number=number/8;
sem=sem*10;
}
printf (" Octal Number is %d",octal);
return 0;
}
Input/Output:
Enter the Decimal Number:35
Octal Number is 43
Program in C++
Here is the source code of the C++ Program to convert decimal to octal using a while loop.
Code:
Code:
#include <iostream>
using namespace std;
int
main ()
{
int rem, number,sem=1,octal=0;
cout<<"Enter the Decimal Number:";
cin>>number;
while(number !=0)
{
octal=octal+(number%8)*sem;
number=number/8;
sem=sem*10;
}
cout<<"Octal Number is "<<octal;
}
Input/Output:
Enter the Decimal Number:78
Octal Number is 116
Program in Java
Here is the source code of the Java Program to convert decimal to octal using a while loop.
Code:
Code:
import java.util.Scanner;
public class DecimalToOctal {
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int rem, number,sem=1,octal=0;
System.out.println("Enter the Decimal Number:");
number=cs.nextInt();
while(number !=0)
{
octal=octal+(number%8)*sem;
number=number/8;
sem=sem*10;
}
System.out.println("Octal Number is "+octal);
cs.close();
}
}
Input/Output:
Enter the Decimal Number:
89
Octal Number is 131
Program in Python
Here is the source code of the Python Program to convert decimal to octal using a while loop.
Code:
Code:
sem=1
octal=0
print("Enter the Decimal Number:")
number=int(input())
while(number !=0):
octal=octal+(number%8)*sem
number=number//8
sem=int(sem*10)
print("Octal Number is ",octal)
print("Enter the Decimal Number:")
number=int(input())
while(number !=0):
octal=octal+(number%8)*sem
number=number//8
sem=int(sem*10)
print("Octal Number is ",octal)
Input/Output:
Enter the Decimal Number:
9
Octal Number is 11
Most Recommend Questions:-
More Questions:-
0 Comments
Please do not Enter any spam link in the comment box