C program to convert decimal to Octal; Through this tutorial, we will learn how to convert decimal numbers to octal numbers in c program using for loop, while loop and function.
Programs to Convert Decimal to Octal Number in C
Use the following programs to convert decimal numbers to octal numbers in c program using for loop, while loop and function in c:
- C Program to Convert Decimal to Octal Number using While Loop
- C Program to Convert Decimal to Octal Number using For Loop
- C Program to Convert Decimal to Octal Number using Function
C Program to Convert Decimal to Octal Number using While Loop
/* C Program to Convert Decimal to Octal Number */ #include <stdio.h> int main() { int octalNumber[10], number, i, j; printf("\n Please Enter Decimal Number : "); scanf("%d", &number); while(number != 0) { octalNumber[i++] = number % 8; number = number / 8; } printf("\n Equivalent Octal Number of a Given Number = "); for(j = i - 1; j > 0; j--) { printf("%d", octalNumber[j]); } return 0; }
The output of the above c program; as follows:
Please Enter Decimal Number : 110110 Equivalent Octal Number of a Given Number = 327036
C Program to Convert Decimal to Octal Number using For Loop
/* C Program to Convert Decimal to Octal Number */ #include <stdio.h> int main() { int octalNumber[10], number, i, j; printf("\n Please Enter Decimal Number : "); scanf("%d", &number); for(i = 0; number > 0; i++) { octalNumber[i] = number % 8; number = number / 8; } printf("\n Equivalent Octal Number of a Given Number = "); for(j = i - 1; j >= 0; j--) { printf("%d", octalNumber[j]); } return 0; }
The output of the above c program; as follows:
Please Enter Decimal Number : 1420 Equivalent Octal Number of a Given Number = 2614
C Program to Convert Decimal to Octal Number using Function
/* C Program to Convert Decimal to Octal Number */ #include <stdio.h> long Decimal_to_Octal(int number) { long octalNumber = 0, i = 1; int remainder; while(number != 0) { remainder = number % 8; number = number / 8; octalNumber = octalNumber + (remainder * i); i = i * 10; } return octalNumber; } int main() { int number; printf("\n Please Enter Decimal Number : "); scanf("%d", &number); long oct = Decimal_to_Octal(number); printf("\n Equivalent Octal Number of %d = %ld", number, oct); return 0; }
The output of the above c program; as follows:
Please Enter the Number You want to Convert : 22 Equivalent Octal Number of 22 = 26