Click here to Skip to main content
14,540,631 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
int main()
	int binary,i=0,rem,c,count=0,x=0;
	printf("enter a binary number for conversion to decimal\n");
	printf("the converted binary number to octal is:  %d",x);
	return 0;
Updated 14-Oct-16 9:50am
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 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[^][^][^]

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.
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.
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!
Aritra94 14-Oct-16 15:36pm
THANK I did corrected the program now and its working fine
OriginalGriff 14-Oct-16 16:43pm
You're welcome!

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