hey everybody, I'm new here and also at c programming. I'm having trouble figuring out how to fix my code so it works properly. First let me give a brief description of what i'm trying to do with this program.
It should ask the user to input a number divisible by 3, test the number to make sure, if it isn't, ask the user to try again. Then cube each digit in the number and add them together. It then goes on to do other things but I think I can do all of that by myself.
I need help with two things.
The first is that I need to change my two "if" statements into a loop that will allow the user to try multiple times to enter a number divisible by 3 if the first few attempts are not. I tried a "while" loop a few different ways, but kept getting caught in an infinite loop.
And second, I don't know how to format a command to cube each digit in an integer (Ex: 138 = 1+27+512). I think I will have to use something like this 138%10=8 then 13%10=3 then 1%10=1 but I'm not sure how to write it as a command.
If anyone can help me with these two things I would be very greatful. Thank you all for your time.
This is what I have so far:
Code:
#include<stdio.h>
int main(void) {
int input;
int remainder;
printf("\nEnter a number that is divisible by 3: ");
scanf("%i",&input);
remainder = input % 3;
if(remainder == 0)
printf("\n%i is divisible by 3!\n\n",input); //This is where
//the cube command should go
else {
printf("\n\n%i is not divisible by 3, try again:\n",input);
printf("\n\nEnter a number that is divisible by 3: ");
scanf("%i",&input);
}
return 0;
}