Program to convert Decimal to Octal

Description

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Convert the given decimal number into an equivalent octal number means
convert the number with base value 10 to base value 8.
Convert the given decimal number into an equivalent octal number means convert the number with base value 10 to base value 8.
Convert the given decimal number into an equivalent octal number means
convert the number with base value 10 to base value 8.

C/C++

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/* C Program to convert Decimal to Octal */
//Save it as ConvertDecimalToOctal.c
#include<stdio.h>
int main(){
int dec, rem, octalNum=0, count=1;
printf("Enter a decimal number : ");
scanf("%d", &dec);
//Storing the decimal value to temporary variable
int temp = dec;
while(dec != 0) {
//Calculation of remainder
rem = dec % 8;
//Calculation of octal value
octalNum += (rem * count);
// storing exponential value
count *= 10;
dec /= 8;
}
//Restoring the decimal value
dec = temp;
printf("The Octal value of %d is %d", dec, octalNum);
return 0;
}
/* C Program to convert Decimal to Octal */ //Save it as ConvertDecimalToOctal.c #include<stdio.h> int main(){ int dec, rem, octalNum=0, count=1; printf("Enter a decimal number : "); scanf("%d", &dec); //Storing the decimal value to temporary variable int temp = dec; while(dec != 0) { //Calculation of remainder rem = dec % 8; //Calculation of octal value octalNum += (rem * count); // storing exponential value count *= 10; dec /= 8; } //Restoring the decimal value dec = temp; printf("The Octal value of %d is %d", dec, octalNum); return 0; }
/* C Program to convert Decimal to Octal */
//Save it as ConvertDecimalToOctal.c

#include<stdio.h>
int main(){

    int dec, rem, octalNum=0, count=1;

    printf("Enter a decimal number : ");
    scanf("%d", &dec);

    //Storing the decimal value to temporary variable
    int temp = dec;

    while(dec != 0) {

        //Calculation of remainder
        rem = dec % 8;

        //Calculation of octal value
        octalNum += (rem * count);

        // storing exponential value
        count *= 10;

        dec /= 8;
    }

    //Restoring the decimal value
    dec = temp;

    printf("The Octal value of %d is %d", dec, octalNum);

    return 0;
}
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Input:
Enter a decimal number : 19
Output:
The octal of 19 is 23
Input: Enter a decimal number : 19 Output: The octal of 19 is 23
Input:
Enter a decimal number : 19

Output:
The octal of 19 is 23

Java

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/* Java Program to convert Decimal to Octal */
//Save it as ConvertDecimalToOctal.java
import java.io.*;
import java.util.Scanner;
public class ConvertDecimalToOctal {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a decimal number : ");
int dec = scanner.nextInt();
//Storing the decimal value to temporary variable
int temp = dec;
int rem, octalNum=0, count=1;
while(dec != 0) {
//Calculation of remainder
rem = dec % 8;
//Calculation of octal value
octalNum += (rem * count);
// storing exponential value
count *= 10;
dec /= 8;
}
//Restoring the decimal value
dec = temp;
System.out.println("The octal of " + dec + " is "+ octalNum);
/*
There is predefined function in Java to convert decimal to octal
Integer.toOctalString(decimalNumber)
*/
}
}
/* Java Program to convert Decimal to Octal */ //Save it as ConvertDecimalToOctal.java import java.io.*; import java.util.Scanner; public class ConvertDecimalToOctal { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter a decimal number : "); int dec = scanner.nextInt(); //Storing the decimal value to temporary variable int temp = dec; int rem, octalNum=0, count=1; while(dec != 0) { //Calculation of remainder rem = dec % 8; //Calculation of octal value octalNum += (rem * count); // storing exponential value count *= 10; dec /= 8; } //Restoring the decimal value dec = temp; System.out.println("The octal of " + dec + " is "+ octalNum); /* There is predefined function in Java to convert decimal to octal Integer.toOctalString(decimalNumber) */ } }
/* Java Program to convert Decimal to Octal */
//Save it as ConvertDecimalToOctal.java

import java.io.*;
import java.util.Scanner;

public class ConvertDecimalToOctal {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        
        System.out.println("Enter a decimal number : ");
        int dec = scanner.nextInt();
        
                //Storing the decimal value to temporary variable
        int temp = dec;
        
        int rem, octalNum=0, count=1;
        
        while(dec != 0) {
            
                        //Calculation of remainder
            rem = dec % 8;
            
                        //Calculation of octal value
            octalNum += (rem * count);
            
                        // storing exponential value
            count *= 10;
            
            dec /= 8;
        }
        
                //Restoring the decimal value
        dec = temp;
        
        System.out.println("The octal of " + dec + " is "+ octalNum);
        
        
        /* 
            There is predefined function in Java to convert decimal to octal
            Integer.toOctalString(decimalNumber)
        */
        
    }
}
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Input:
Enter a decimal number :
19
Output:
The octal of 19 is 23
Input: Enter a decimal number : 19 Output: The octal of 19 is 23
Input:
Enter a decimal number : 
19

Output:
The octal of 19 is 23

Related Programs

1) Program to convert Binary to Decimal
2) Program to convert Binary to Hexa Decimal
3) Program to convert Decimal to Binary
4) Program to convert Octal to Decimal
5) Program to find sum of digits of a number
6) Program to reverse a number
7) Program to calculate Factorial of a Number
8) Program to display multiplication table of a number
9) Program to Display Fibonacci Series
10) Program to Display Fibonacci Series using Recursion
11) Program of Simple Calculator using switch
12) Program to print all Squares of numbers from 1 to given Range
13) Program to find sum of first and last digit of a number
Share Me

Leave a Reply