Hey guys! I'm having a problem and I hope you guys can help me figure it out. I have to create a program that prints all the armstrong numbers from a user given lower and upper bound. I have a lot of the code but I can't seem to get a specific function right. The function is supposed to basically seperate a given number. So if x = 153, the function should seperate it into the numbers 1, 5, and 3. Problem is I have to seperate them and pass them to another function without an array. Here's what I have so far. The function I'm having problems with is called DthDigitofX.

//-----------------------------------------------------
int main()
//-----------------------------------------------------
{
boolean IsArmstrongNumber(int x);

int LB,UB,x;

printf("LB? "); scanf("%d",&LB);
printf("UB? "); scanf("%d",&UB);

for(x = LB; x <= UB; x++)
{
if(IsArmstrongNumber(x) == true)
printf("%i is an Armstrong Number", x);
}

system("pause");

return( 0 );
}

//-----------------------------------------------------
boolean IsArmstrongNumber(int x)
//-----------------------------------------------------
{
while (x >= 0)
{
int NumberOfDigits(int r);
int DthDigitOfX(int x,int d);

int sum,d,temp;
int n = NumberOfDigits(x);

for (d = 1; d <= n; d++)
{
sum = sum + pow(DthDigitOfX(x,d),n);
}

if(sum == x)
return true;
else
return false;
}
}

//-----------------------------------------------------
int NumberOfDigits(int x)
//-----------------------------------------------------
{

while(x >= 0)
{
int r = 0;

do
{
r++;
x /= 10;
//printf("%i\n", r);
}
while ( x != 0 );
return( r );
}
}

//-----------------------------------------------------
int DthDigitOfX(int x,int d)
//-----------------------------------------------------
{
while (x >= 0)
{
for (d = 1; d <= 10; d++)
x = x % 10;

return(x);
}
}

## All 7 Replies

Hey,

"boolean" is not a data type, look at the correct definition/declartion

Also, are you defining the function prototypes before main (since you're using them after main) because if not, the program won't know where to call the fucntions.

Hope this helps :)

The function I'm having problems with is called DthDigitofX.

Problems mean nothing to me. Describe exactly WHAT is the problem?

The problem is that it correctly extracts the first number but doesn't do the rest. For example, if I have 153, it will extract the 3 but not the rest.

Well just concentrate on this first.

Create one function which spits out all the numbers, 3 ,5,1

put a load of couts and see what you come up with.

(You may want to consider dividing by multiples of ten as well as using the modulus operator as a hint)

Also phorce makes a point boolean is not a datatype I think bool is though. But concentrate on the above.

Okay, using your advice, I think my problem is more how to pass the individual numbers from function to function than how to seperate them. Correct me if I'm wrong but this should seperate them correctly?

int a = 0;
for (d = 1; d <= 10; d++)
{
a = x % 10;
x = x / 10;
}

Correct me if I'm wrong but this should seperate them correctly?

Check it with a few examples? Do most of them work, if so you've answered your question. The loop looks a little dubious though, as long as d <=10, I wonder if the integer you passed in was more than ten digits what would happen? OK you can't declare ints that big but best to make your code extra safe.

I think my problem is more how to pass the individual numbers from function to function than how to seperate them

If this is the case then try writing the program without functions. People tend to see the logic much clearer so they know exactly where they're going wrong and what the function must do and where they must place it.

Why are you running the loop till d <= 10 ? Can't understand this part.

Anyways try this, see if it works or not.

int DthDigitOfX( int x, int d)
{
int ans = 0 ;

for( int i = 0 ; i < d ; i++)
{
ans = x % 10;
x = x / 10;
}

return ans;
}

What I did is, ran the loop till the dth digit. Not 10. It will give the digit from last, like if its 12345, 2nd digit would be 4, 4th digit would be 2.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.