Question: Strong number code in C: Hi there, I am writing a code that is going to give me the sum of each digit to the

Strong number code in C:

Hi there,

I am writing a code that is going to give me the sum of each digit to the cube e.g 123 = ((1 * 1 *1) + (2 * 2 * 2) + (3 * 3 *3)) to then determine whether this number is a strong number or not. I have written the code to retrieve the last digit and elevating this number to the power of 3. Also written the code that would give me the second and third digits so that this calculation can be done with each one of them. However, I am unable to join these pieces of code in a while loop (while loop is required for this exercise). Please see below:

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 */

}

Strong number code in C: Hi there, I am writing a code

Can you please help me to find what is wrong with my code?

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!