C Programming

Solved Example

Practice Questions

Exercise Question

Program to find octal equivalent of a number

Objective

Write a program to enter a number and finds the octal equivalent of the number.

Program / Solution

#include<stdio.h>
#include<math.h>
int main() {
	int i,n,oct,dig,old;
	
	printf("Enter any number: ");
	scanf("%d",&n);
	
	i = 0; oct = 0; old = n;
	while(n != 0) {
		dig = n % 8; // find the octal digit
		n = n / 8; //divide the number
		oct = oct + dig * pow(10,i); //suffix digit in oct in reverse order
		i++;
	}
	
	printf("Octal equivalent %d = %d",old,oct);
	return 0;
}

Sample Output

Enter any number: 50
Octal equivalent 50 = 62