Hello everyone this is the correct ans for your problem....this program is working propely but but int thing is that i can't solve is that the decimal number range between 1 to 7. if any character in my number will increase from 7 thou it shuold give output the number is invalid but but i can't do this if anyone can solve this problem thou plz tell me abou it.......

#include <stdio.h> 
#include <conio.h>
int octaltodecimal(int x);
void main ()

{

        int a,decimal;

        printf("Enter an integer = ");
        scanf("%d", &a);
		decimal=octaltodecimal(a);
		printf("%d",decimal);
		getch();
}
int octaltodecimal(int x)
{
		int base8,m,y,z,n,lastDigit,dec;
		lastDigit=x%10;
        n=0;
        y=x;
        if(x>10)
		{
       
        z=y%10;      
        while (y != z) 
		{
        y=y/10;
        z=y%10;
        base8=z*8^n;
        n=n+1;   
        }
		}
        dec=base8+lastDigit;
        return dec;
}

Edited 5 Years Ago by Narue: added code tags

Zia, welcome to the forum! ;)

First thing you have to know is to ALWAYS highlight your code and click on the [ code] icon in the editor window. (it won't have a space before the c).

Otherwise, your code will all be squished over to the far left, and be VERY hard to study. Thus, it gets generally ignored.

Your should add an initializer for base8; if x is < 10 then base8 is never assigned and will cause problems with the result. In addition, it looks like you can't go beyond certain number (i.e. 123 does not work).

In either case, I think you're over complicating the issue. You could simply iterate through each digit multiplying it by 8 to the power of the position number:

int ret = 0;

for(int multi = 1; x > 0; multi *= 8) {
	int digit = x % 10;
	if(digit > 7)
		return -1;
	ret += digit * multi;
	x /= 10;
}

return ret;

In each iteration you can check for an invalid digit.

EDIT:
Just to clarify, the ^ operator is a bitwise XOR operator, not a power function. I believe that is not what you intended.

Edited 5 Years Ago by nmaillet: n/a

C has octal, decimal, and hexadecimal converters, already built into it. Do you want to use them?

You can put 123 into a char array (a string). Then, to read it in as a decimal, sscanf(%d, &intVariableName);

To read it in as an octal, use %o, as a hexadecimal, use %x.

If you want to make a converter yourself, just post your logic (pseudo code), because I'm not understanding the logic, from the code you posted. That is, it's not clear to me, HOW you want to make the conversion, logically.

#include <stdio.h> 
#include <conio.h>
int octaltodecimal(int x);
void main ()

{

        int a,decimal;

        printf("Enter an integer = ");
        scanf("%d", &a);
		decimal=octaltodecimal(a);
		printf("%d",decimal);
		getch();
}
int octaltodecimal(int x)
{
	int base8,m,y,z,n,lastDigit,dec;

//Your while loop should start up here.
//The last digit (right hand side or 1's column), doesn't need %10, since it is octal, not decimal, and already is in the 
//right format (a one's column number less than 10)
	lastDigit=x%10;

        n=0;
        y=x; //x should be x/10, here since the right most digit is already handled
        if(x>10)  //x can't be greater than 10, the number base is 10.
		{
       
        z=y%10;      //don't need z
        while (y != z) //loop can start higher up, and remove some of the duplicated code. while x > 0 is all you need 
		{
        y=y/10;
        z=y%10;
        base8=z*8^n; See pow() in math.h, for exponentiation. ^ is not what you want in C.
        n=n+1;   
        }
		}
        dec=base8+lastDigit;
        return dec;
}

Edited 5 Years Ago by Adak: n/a

This article has been dead for over six months. Start a new discussion instead.