Octal-To-Decimal-In-C
INTRODUCTION
A number is represented as octal whenever a numerical value is initialized by adding '0' preceding an integer. '10', for example, appears as 10, although '010' is interpreted as 8. The primary eight figures have been referred to as octal numerals, followed by octal literals, which can be employed to express the octal integers' digits.
A small program to illustrate the octal numbers in C:
// C Program for illustrating or defining the octal numbers.
#include <stdio.h>
int main ()
{
// using the octal numeric to begin a number.
// octal literal starts with zero.
int s = 0101;
printf("The value of s = %d", s);
return 0;
}
OUTPUT:
![OCTAL TO DECIMAL IN C](https://static.tutorialandexample.com/c/octal-to-decimal-in-c1.png)
The other example is as follows:
// C Program for illustrating or defining the octal numbers.
#include <stdio.h>
int main ()
{
// using the octal numeric to begin a number.
// octal literal starts with zero.
int g = 010101;
printf("The value of g = %d", g);
return 0;
}
OUTPUT:
![OCTAL TO DECIMAL IN C](https://static.tutorialandexample.com/c/octal-to-decimal-in-c2.png)
We can get an error when we use other digits rather than 0 or 1. The example is as follows:
#include <stdio.h>
int main ()
{
int j = 080;
printf("The integer value of j is = %d", j);
return 0;
}
OUTPUT:
![OCTAL TO DECIMAL IN C](https://static.tutorialandexample.com/c/octal-to-decimal-in-c3.png)
CONVERSION OF OCTAL TO DECIMAL IN C
Let us look at the conversion of octal numbers to decimals in C programming along with its algorithm.
The Octal numerals into Decimals translation C application. Octal concerning Decimals in C works by assuming that a decimal value may be obtained by multiplying every individual octal through a power of about 2, then summing the results from every multiplicity operation. The Numeric's power scales between zero and n-1, where n is determined to represent the total quantity of numeric characters in the octal number.
The traditional way of converting octal numbers to decimals is shown below.
((653)8 = 6 * 82 + 5 * 81 + 3 * 80
384 + 40 + 3 = (427)10
The algorithm is given as follows:
- Setting i = 0, basic = 8, and decimals =0
- Calculate the final sector (sector = number% 10)
- Compute this sector's decimals equivalency.
- Into a decimal inconsistent, add this.
- sector * pow (8, i) + decimal;
- Diminish the quantity by 10 (number).
- Raise the level of i.
Let us look at the picture describing the conversion process from octal to decimal.
![OCTAL TO DECIMAL IN C](https://static.tutorialandexample.com/c/octal-to-decimal-in-c4.png)
The complete C program to convert.
// C Program for conversion of octal numbers to decimal numbers.
#include<stdio.h>
#include<math.h>
// function for performing.
Int convert (long number)
{
int i = 0, decimal = 0;
// only works with base 8.
int basic = 8;
//The conversion happens here.
while (number! =0)
{
int sector = number % 10;
decimal += sector * pow (basic, i);
number /= 10;
i++;
}
return decimal;
}
//The main program for the function is as follows.
int main()
{
//To store long values, we use long rather than int.
long long octal;
printf("The octal number you want to convert is: ");
scanf("%lld", &octal);
printf("The decimal value for the provided octal value is: %lld", convert(octal));
return 0;
}
OUTPUT:
![OCTAL TO DECIMAL IN C](https://static.tutorialandexample.com/c/octal-to-decimal-in-c5.png)
CONVERSION USING WHILE LOOP.
Utilizing Reasoning, Transform an octal Amount into A Numeric One During looping.
The following code in C requests an octal integer from the individual who runs it. We compute the alert employing an ongoing while loop, then add this to the earlier amount for the parameter dec in addition to pow (8, place). For positioning the reminder, we employ an option location.
Dec is equal to Dec + Rem * Pow (8, Place);
![OCTAL TO DECIMAL IN C](https://static.tutorialandexample.com/c/octal-to-decimal-in-c6.png)
C program for conversion using a while loop:
#include <stdio.h>
#include <math.h>
int main ()
{
int num, dec = 0, rem = 0, place = 0;
printf("The octal you wanted to convert\n");
scanf("%d", &num);
printf("\nThe decimal equivalent for %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:
![OCTAL TO DECIMAL IN C](https://static.tutorialandexample.com/c/octal-to-decimal-in-c7.png)
![OCTAL TO DECIMAL IN C](https://static.tutorialandexample.com/c/octal-to-decimal-in-c8.png)
Despite using the pow () operation, convert octal to decimals using C.
#include<stdio.h>
#include<conio.h>
int main()
{
int oct, dec=0, rem, m=1;
printf("Provide a octal number that should be converted: ");
scanf("%d", &oct);
while(oct!=0)
{
rem = oct%10;
dec = dec + (rem*m);
m= m*8;
oct= oct/10;
}
printf("\nThe decimal equivalent is = %d", dec);
getch();
return 0;
}
OUTPUT:
![OCTAL TO DECIMAL IN C](https://static.tutorialandexample.com/c/octal-to-decimal-in-c9.png)