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 Octal Number to Decimal Number

By Chaitanya Singh | Filed Under: C Programs

This program converts an octal number to an equivalent decimal number.

Example: Program to Convert Octal to Decimal

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

  1. C – User defined functions
  2. while loop in C
#include <stdio.h>
#include <math.h>
/* This function converts the octal number "octalnum" to the
 * decimal number and returns it.
 */
long octalToDecimal(int octalnum)
{
    int decimalnum = 0, temp = 0;

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

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

    printf("Enter an octal number: ");
    scanf("%d", &octalnum);

    printf("Equivalent decimal number is: %ld", octalToDecimal(octalnum));

    return 0;
}

Output:

Enter an octal number: 754
Equivalent decimal number is: 492

Check out these related C Programs:

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

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