Chapter - 4: The Loop Control Structure

Write a program to find the octal equivalent of the entered number. Hint: To obtain octal equivalent fo an integer, divide it continuously by 8 till dividend doesn't become zero, then write the remainders obtained in reverse direction.


B
Sections
8
Exercises

A

B

C

D

E

1

2

3

4

5

6

7

8

9

#include<stdio.h>
#include<conio.h>
int main()
{
	
	int num, ans = 0, temp, i;
	printf("Enter a number : ");
	scanf("%d", &num);
	
	for(i = 1; num; i *= 10)
	{
		temp = num % 8;
		ans += temp*i;
		num /= 8;
	}
	
	//To check, octal eq. of 15 is 17.
	
	printf("\n\n%d is the octal equivalent of %d.", ans, num);
	getch();
	return 0;
}

© 2020 Garbage Valuegarbage value logo