C Program To Convert Binary Number To Octal Number, using While Loop

Lets write a C program to convert a number from Binary number system(base 2) to Octal number system(base 8), using while loop.

In this C program we first convert the user entered number from Binary number system to Decimal number system. Then we take the result(which is in Decimal number system) and convert to Octal number system.

Number Systems

number systems

1. Binary Number System uses base 2 and digits 01.
2. Octal Number System uses base 8 and digits 01234567.
3. Decimal Number System uses base 10 and digits 0123456789.
4. Hexadecimal Number System uses base 16 and digits 0123456789ABCDEF.

Related Read:
while loop in C programming
C Program To Convert Binary Number To Decimal Number, using While Loop
C Program To Convert Decimal Number To Octal Number, using While Loop

Note:
In this C program we are dealing with Binary Number System, Decimal Number System and Octal Number System.

Binary Number System
Binary number system has base 2 and digits 0 and 1.
binary decimal number system
Example:
etc .. 24, 23, 22, 21, 20

etc .. 24 = 16, 23 = 8, 22 = 4, 21 = 2, 20 = 1.

Octal Number System
Octal number system has base 8 and digits 0, 1, 2, 3, 4, 5, 6, 7.
octal number system
Example:
etc .. 84, 83, 82, 81, 80

etc .. 84 = ‭4,096‬, 83 = 512, 82 = 64, 81 = 8, 80 = 1.

Expected Output for the Input

User Input:
Enter a Binary Number
111001

Output:
Octal Equivalent of Binary no 111001 is 71.

Video Tutorial: C Program To Convert Binary Number To Octal Number, using While Loop


[youtube https://www.youtube.com/watch?v=W1T6Jm41fok]

YouTube Link: https://www.youtube.com/watch?v=W1T6Jm41fok [Watch the Video In Full Screen.]

Logic To Convert Binary Number To Octal Number, using While Loop

To get complete logic to this program kindly watch these two video tutorials without fail.

1. C Program To Convert Binary Number To Decimal Number, using While Loop
2. C Program To Convert Decimal Number To Octal Number, using While Loop

Source Code: C Program To Convert Binary Number To Octal Number, using While Loop

#include < stdio.h >
#include < math.h >

int main()
{
    long num;
    int  dec = 0, oct = 0, rem = 0, place = 0;

    printf("Enter a Binary Number\n");
    scanf("%ld", &num);

    printf("\nOctal Equivalent of Binary no %ld is ", num);
    while(num)
    {
        rem = num % 10;
        dec = dec + rem * pow(2, place);
        num = num / 10;
        place++;
    }

    place = 1;
    rem   = 0;

    while(dec)
    {
        rem   = dec % 8;
        oct   = oct + rem * place;
        dec   = dec / 8;
        place = place * 10;
    }
    printf("%d.\n", oct);

    return 0;
}

Output 1:
Enter a Binary Number
1111

Octal Equivalent of Binary no 1111 is 17.

Output 2:
Enter a Binary Number
111001

Octal Equivalent of Binary no 111001 is 71.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Convert Octal To Decimal Number In One Line

Lets write a C program to convert a number from Octal number system(base 8) to Decimal number system(base 10) in one line.

Related Read:
C Program To Convert Octal Number To Decimal Number, using While Loop

Expected Output for the Input

User Input:
Enter an Octal number
41

Output:
Decimal Representation of Octal Number 41 is 33.

Logic To Convert Octal To Decimal Number In One Line of Code

In c programming language, any octal number(%o format specifier) you store will also be stored in decimal form and you can print it using %d format specifier.

Video Tutorial: C Program To Convert Octal To Decimal Number In One Line


[youtube https://www.youtube.com/watch?v=ifkQ4wDsE6I]

YouTube Link: https://www.youtube.com/watch?v=ifkQ4wDsE6I [Watch the Video In Full Screen.]

Source Code: C Program To Convert Octal To Decimal Number In One Line

#include < stdio.h >

int main()
{
    int num;

    printf("Enter an Octal number\n");
    scanf("%o", &num);

    printf("\nDecimal Representation of Octal Number %o is %d\n", num, num);

    return 0;
}

Output 1:
Enter an Octal number
20

Decimal Representation of Octal Number 20 is 16

Output 2:
Enter an Octal number
51

Decimal Representation of Octal Number 51 is 41

Output 3:
Enter an Octal number
132

Decimal Representation of Octal Number 132 is 90

Output 4:
Enter an Octal number
16

Decimal Representation of Octal Number 16 is 14

Output 5:
Enter an Octal number
40

Decimal Representation of Octal Number 40 is 32

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Convert Decimal To Octal Number In One Line

Lets write a C program to convert a number from Decimal number system(base 10) to Octal number system(base 8) in one line.

Related Read:
C Program To Convert Decimal Number To Octal Number, using While Loop

Expected Output for the Input

User Input:
Enter a Decimal Number
90

Output:
Octal representation of Decimal number 90 is 132

Logic To Convert Decimal To Octal Number In One Line of Code

In c programming language, any decimal number you store will also be stored in Octal form and you can print it using %o format specifier.

Video Tutorial: C Program To Convert Decimal To Octal Number In One Line


[youtube https://www.youtube.com/watch?v=41-xLNYbvDY]

YouTube Link: https://www.youtube.com/watch?v=41-xLNYbvDY [Watch the Video In Full Screen.]

Source Code: C Program To Convert Decimal To Octal Number In One Line

#include < stdio.h >

int main()
{
    int num;

    printf("Enter a Decimal Number\n");
    scanf("%d", &num);

    printf("\nOctal representation of Decimal number %d is %o\n", num, num);

    return 0;
}

Output 1:
Enter a Decimal Number
16

Octal representation of Decimal number 16 is 20

Output 2:
Enter a Decimal Number
41

Octal representation of Decimal number 41 is 51

Output 3:
Enter a Decimal Number
90

Octal representation of Decimal number 90 is 132

Output 4:
Enter a Decimal Number
14

Octal representation of Decimal number 14 is 16

Output 5:
Enter a Decimal Number
32

Octal representation of Decimal number 32 is 40

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Convert Octal Number To Decimal Number, using While Loop

Lets write a C program to convert a number from Octal number system(base 8) to Decimal number system(base 10), using while loop.

Number Systems

number systems

1. Binary Number System uses base 2 and digits 01.
2. Octal Number System uses base 8 and digits 01234567.
3. Decimal Number System uses base 10 and digits 0123456789.
4. Hexadecimal Number System uses base 16 and digits 0123456789ABCDEF.

Related Read:
while loop in C programming
Calculate Sum of Digits: C Program
C Program To Reverse a Number

Note: Octal number system can be derived by base 8 to the power of whole numbers.

Example:
etc .. 84, 83, 82, 81, 80

etc .. 84 = ‭4,096‬, 83 = 512, 82 = 64, 81 = 8, 80 = 1.

octal number system

Expected Output for the Input

User Input:
Enter an Octal Number
24

Output:
Decimal Equivalent of 24 is 20.

Explanation:

Decimal to binary

If user enters num = 24.

(83 x 0) + (82 x 0) + (81 x 2) + (80 x 4 )
= 0 + 0 + (8 x 2) + (1 x 4)
= 16 + 4
= 20
So Decimal equivalent of Octal number 24 is 20.

Video Tutorial: C Program To Convert Octal Number To Decimal Number, using While Loop


[youtube https://www.youtube.com/watch?v=QVksCjl2yhY]

YouTube Link: https://www.youtube.com/watch?v=QVksCjl2yhY [Watch the Video In Full Screen.]

Logic To Convert Octal Number To Decimal Number, using While Loop

In this C program we ask the user to enter / input an Octal number. Using while loop we calculate the reminder and add it to previous value of variable dec, along with pow(8, place). We make use of variable place to position the reminder.
dec = dec + rem * pow(8, place);

octal number system

Source Code: C Program To Convert Octal Number To Decimal Number, using While Loop

#include < stdio.h >
#include < math.h >

int main()
{
    int num, dec = 0, rem = 0, place = 0;

    printf("Enter an Octal Number\n");
    scanf("%d", &num);

    printf("\nDecimal Equivalent of %d is ", num);
    while(num)
    {
        rem = num % 10;
        dec = dec + rem * pow(8, place);
        num = num / 10;
        place++;
    }
    printf("%d\n", dec);

    return 0;
}

Output 1:
Enter a decimal number
16

Octal Equivalent of 16 is 20

Output 2:
Enter an Octal Number
24

Decimal Equivalent of 24 is 20

Output 3:
Enter an Octal Number
132

Decimal Equivalent of 132 is 90

Output 4:
Enter an Octal Number
16

Decimal Equivalent of 16 is 14

Output 5:
Enter an Octal Number
40

Decimal Equivalent of 40 is 32

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Convert Decimal Number To Octal Number, using While Loop

Lets write a C program to convert a number from Decimal number system(base 10) to Octal number system(base 8), using while loop.

Number Systems

number systems

1. Binary Number System uses base 2 and digits 01.
2. Octal Number System uses base 8 and digits 01234567.
3. Decimal Number System uses base 10 and digits 0123456789.
4. Hexadecimal Number System uses base 16 and digits 0123456789ABCDEF.

Related Read:
while loop in C programming
Calculate Sum of Digits: C Program
C Program To Reverse a Number

Note: Octal number system can be derived by base 8 to the power of whole numbers.

Example:
etc .. 84, 83, 82, 81, 80

etc .. 84 = ‭4,096‬, 83 = 512, 82 = 64, 81 = 8, 80 = 1.

octal number system

Expected Output for the Input

User Input:
Enter a decimal number
20

Output:
Octal equivalent of 20 is 24.

Explanation:
If user enters num = 20

We keep on dividing the number 20 by 8.

20 / 8 = 2, reminder 4.
02 / 8 = 0, reminder 2.

Decimal to binary

So Octal equivalent of decimal number 20 is 24.

OR

(83 x 0) + (82 x 0) + (81 x 2) + (80 x 4 )
= 0 + 0 + (8 x 2) + (1 x 4)
= 16 + 4
= 20

Video Tutorial: C Program To Convert Decimal Number To Octal Number, using While Loop


[youtube https://www.youtube.com/watch?v=zd_rO67U-Gw]

YouTube Link: https://www.youtube.com/watch?v=zd_rO67U-Gw [Watch the Video In Full Screen.]

Logic To Convert Decimal Number To Octal Number, using While Loop

In this C program we ask the user to enter / input a decimal number. Using while loop we calculate the reminder and add it to previous value of variable oct. We make use of variable place to position the reminder based on number system – unit, ten, hundred, thousand, ten thousand etc.

Source Code: C Program To Convert Decimal Number To Octal Number, using While Loop

#include < stdio.h >

int main()
{
    int num, oct = 0, rem = 0, place = 1;

    printf("Enter a decimal number\n");
    scanf("%d", &num);

    printf("\nOctal Equivalent of %d is ", num);
    while(num)
    {
        rem = num % 8;
        oct = oct + rem * place;
        num = num / 8;
        place = place * 10;
    }
    printf("%d\n", oct);

    return 0;
}

Output 1:
Enter a decimal number
16

Octal Equivalent of 16 is 20

Output 2:
Enter a decimal number
41

Octal Equivalent of 41 is 51

Output 3:
Enter a decimal number
90

Octal Equivalent of 90 is 132

Output 4:
Enter a decimal number
14

Octal Equivalent of 14 is 16

Output 5:
Enter a decimal number
32

Octal Equivalent of 32 is 40

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert