Let Us C Solution

 EXERCISE [B]

(e) Write a program to receive an integer and find its octal
equivalent.
(Hint: To obtain octal equivalent to an integer,divide it
continuously by 8 till dividend doens't become zero,then write the remainders
 obtained in reverse direction */
 
 #include<stdio.h>
 #include<conio.h>
 int main()
 {
 	int num,r,res=0,oct=0,flag=0;
 	printf("Etner an integer:");
 	scanf("%d",&num);
 	r=num;
 	// get the remainder
 	while(r!=0)
 	{
 		res=res*10+r%8;
 		//check for zero at first position
 		if(res==0)
 		{
 			flag=1;
		 }
		 r=r/8;
	 }
	 // reverse number
	 while (res!=0)
	 {
	 	oct=oct*10+res%10;
	 	res=res/10;
	 }
	 // if first position contain zero then multiply the number with 10
	 if(flag==1)
	 {
	 	oct=oct*10;
	 }
	 printf("The octal of %d is %d",num,oct);
	 
 	return 0;
 }

Post a Comment

0 Comments