Thread: I need help with a program to sum the cubes of each digit of an integer

  1. #1
    Registered User
    Join Date
    Feb 2016
    Posts
    6

    I need help with a program to sum the cubes of each digit of an integer

    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;
    }

  2. #2
    Registered User
    Join Date
    Feb 2016
    Posts
    6
    hello
    Code:
    #include<stdio.h>
    int main()
    {
    int input;
    printf("Enter a number that is divisible by 3: ");
    scanf("%i",&input);
    while(input%3 != 0)
        {
        printf(\n\n%i is not divisible by 3, try again:\n",input);
        scanf("%i",&input);
        }
    int aux, digitsum = 0;
    while(input)
        {
        aux = input % 10;
        aux = aux * aux * aux;
        digitsum = digitsum + aux;
        input = input / 10;
        }
    return 0;
    }
    now about the code, first you read the first number, if it's div. with 3 the first while loop will be passed.
    if not, your program will read your input until the number it's div with 3.
    aux is used for your digit, first you cut the last digit and make it ^3.
    you use digitsum to sum all digit cubes from your number.
    as you used in your example, 138, digitsum = 1 + 27 + 512 = 540. if you printf digitsum after your input is 138, it will display 540.
    Last edited by ursu09; 02-06-2016 at 09:29 PM.

  3. #3
    Registered User
    Join Date
    Feb 2016
    Posts
    6
    Thank you so much for your help with this code. It works great. After seeing it written correctly it's a lot easier to see what I was doing wrong. I should now be able to finish the rest of the program with relative ease, fingers crossed. Thanks again for your time and help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using putchar to output an integer with more than 1 digit
    By RandomEvil in forum C Programming
    Replies: 1
    Last Post: 10-14-2009, 10:20 PM
  2. Replies: 8
    Last Post: 09-04-2008, 09:56 AM
  3. Recognizing each digit of an integer
    By sybariticak47 in forum C++ Programming
    Replies: 3
    Last Post: 02-08-2006, 01:23 AM
  4. 200 digit integer... how?
    By skeptik in forum C Programming
    Replies: 20
    Last Post: 07-25-2005, 07:56 AM
  5. encripting 4-digit integer?
    By o0o in forum C++ Programming
    Replies: 5
    Last Post: 12-25-2003, 09:48 PM