Click here to Skip to main content
14,540,528 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have tried to make a program on binary to octal conversion in 'c' 'without using function but I think my logic is incorrect.

What I have tried:

//binary to octal conversion
#include<stdio.h>
#include<math.h>
int main()
{
	int binary,i=0,rem,c,count=0,x=0;
	printf("enter a binary number for conversion to decimal\n");
	scanf("%d",&binary);
	while(count<=3);
	{
	rem=binary%10;
	c=pow(2,i);
	x+=rem*c;
	i++;
	count++;
	}
	printf("the converted binary number to octal is:  %d",x);
	getch();
	return 0;
}
Posted
Updated 14-Oct-16 9:50am
v2
Comments
CPallini 14-Oct-16 15:53pm
   
What do you mean for binary to octal conversion?
Is it, for instance 00100111->047?
Note: binary and octal are just number representations (that is a string should be expected by the user).
Rate this:
Please Sign up or sign in to vote.

Solution 1

Oh yes, it's incorrect.
This is your homework, so no code!

But...think about it. What is Octal? What are Octal numbers?
The answer is: base 8. Octal numbers contain only the digits '0' to '7' inclusive, in the same way that Decimal (base 10) contains only the digits '0' to '9' inclusive, and Binary (Base 2) contains only the digits '0' and '1'.

Your code doesn't even try to use Octal...

I'd strongly suggest that you invest some time in working out how to use the debugger - Google will help you there, just google 'C', the name of the C IDE you are using, and "Debugger" and it should give you an explanation. It will save you a lot of hair pulling when you get to this stage!
   
Comments
Aritra94 14-Oct-16 15:36pm
   
THANK YOU..now I did corrected the program now and its working fine
OriginalGriff 14-Oct-16 16:43pm
   
You're welcome!
Rate this:
Please Sign up or sign in to vote.

Solution 2

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.

Advice: take a sheet of paper and try to do it by hand, your program should use the same procedure.
   
Comments
Aritra94 14-Oct-16 15:38pm
   
thanks for the suggestion debugging did helped me in it
Patrice T 14-Oct-16 18:14pm
   
Happy to know that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100