Computer Science, asked by abhay9514, 10 months ago

How to convert a Decimal to Octal using C#?

Answers

Answered by Smitvan
0

In this program, we will read an integer number in Decimal and converts it into Octal Number System. This program is for Decimal to Octal Conversion in C.

The logic behind to implement this program - Get remainder using modulus operator by 8 and store it into an array then divide number by 8, repeat this process till given number is greater than 0. Because 8 is the base of Octal Number System.

For more details Learn: Computer Number System and its conversions.

Decimal to Octal Conversion using C program

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

/*C program to convert number from decimal to octal*/

#include <stdio.h>

int main()

{

int number,cnt,i;

int oct[32];

printf("Enter decimal number: ");

scanf("%d",&number);

cnt=0; /*initialize index to zero*/

while(number>0)

{

oct[cnt]=number%8;

number=number/8;

cnt++;

}

/*print value in reverse order*/

printf("Octal value is: ");

for(i=(cnt-1); i>=0;i--)

printf("%d",oct[i]);

return 0;

}

Output

Enter decimal number: 545

Octal value is: 1041

Similar questions