beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

C Program to Convert Decimal to Octal Number

By Chaitanya Singh | Filed Under: C Programs

This program converts the input decimal number to an octal number.

Example: Program to convert decimal to octal

In this program, we have created a user defined function decimalToOctal() for decimal to octal conversion. The program takes the decimal number(entered by user) as input and converts it into an octal number using the function. To understand this program, you should be familiar with the following C programming concepts:

  1. C Programming User defined functions
  2. while loop in C programming
#include <stdio.h>
#include <math.h>
/* This function converts the decimal number "decimalnum"
 * to the equivalent octal number
 */
int decimalToOctal(int decimalnum)
{
    int octalnum = 0, temp = 1;

    while (decimalnum != 0)
    {
    	octalnum = octalnum + (decimalnum % 8) * temp;
    	decimalnum = decimalnum / 8;
        temp = temp * 10;
    }

    return octalnum;
}
int main()
{
    int decimalnum;

    printf("Enter a Decimal Number: ");
    scanf("%d", &decimalnum);

    printf("Equivalent Octal Number: %d", decimalToOctal(decimalnum));

    return 0;
}

Output:

Enter a Decimal Number: 436
Equivalent Octal Number: 664

Check out these related C Programs:

  1. C Program to convert Octal to Decimal
  2. C Program to convert Octal to Binary
  3. C Program to convert Binary to Octal
  4. C Program to convert Binary to Decimal
  5. C Program to convert uppercase string to lowercase string

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Programs

  • C Programs
  • Java Programs

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap