Question: Hi there, I am writing a code that will return the third power of each digit from any number input. The code is divided in

Hi there,

I am writing a code that will return the third power of each digit from any number input. The code is divided in a while loop that will keep on giving me the last digit of the number given, the second part will calculate the third power and the last one will add up each digit to the third power. I am required to use while loops for this program, with the outer while loop that calculates the last digit not working. if I remove the while condition, it does work by giving me the last digit by using %10, but when adding the while loop it does not work. Please see bellow:

int main ()

{

int x,y=3, r, m, f = 0;

printf("Enter number: ");

scanf("%d", &x);

/* Piece of code to retrieve the last digit of the number given */

while (x>=1)

{

m = x %10;

r = m;

x = x/10;

}

while (y>1) /* Piece of code that gives any digit to the third power (it works as long as not used with the outher loop)*/

{

m = m*r;

y--;

}

f = f + m; /* Piece of code that adds up each digits after elevated to the third power

(It works as long as not used with the outer while e.g f = 1 and 123 = 3^3 + 1 + 1 + 1 + 1 = 31)*/

printf("%d", f); /* This piece of code should give me the addition of each digit to the power of 3 */

}

Hi there, I am writing a code that will return the third

Can you please help me to find out the reason for this not working?

Thank you,

int main() { int x,y=3, r, m, f = 0; printf("Enter number: "); scanf("%d", &x); /* Piece of code to retrieve the last digit of the number given */ while (x>=1) { m = x %10; r = m; x = x/10; while (y>1) /* Piece of code that gives any digit to the third power (it works as long as not used with the outher loop)*/ m = m*r; y--; } f = f + m; /* Piece of code that adds up each digits after elevated to the third power (It works as long as not used with the outer while e.g f = 1 and 123 = 3^3 + 1 + 1 + 1 + 1 = 31)*/ printf("%d", f); /* This piece of code should give me the addition of each digit to the power of 3 */

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!