GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 06-Feb-2016, 13:49
budgie1980 budgie1980 is offline
New Member
 
Join Date: Feb 2016
Posts: 6
budgie1980 is on a distinguished road

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


Hello all, I need a little help with getting my program to work. The basic idea is that the program should ask the user to enter a number that is divisible by 3. If the number is not, the program should ask the user to try again. Once a number is determined to be divisible by 3, the program should cube each digit of the integer, sum them and print out the result. For example, 138 would be (1+27+512=540). Then the program should divide that number by 3 example (540/3=180) and print out that result as well. I think I know the way to do this,
if i were to input 138, I could seperate the number by using 138%10=8 then
13%10=3 then 1%10=1. But I am not sure how to insert that into my code.

This is what I have so far. My first problem is how to get a loop going where my "if" statement is, so I can enter as many wrong numbers I want without the program ending. Right now it only cycles twice. I tried using a "while(remainder > 0)" but that seems to start an infinite loop.

Also my printf statement after the first "if" is actually where the formula for cubing the digits of the users number should go. I just don't know how to compose it so it works correctly. Any help would be appreciated. Thanks for your time and help.

One last note. This is a beginners course, and we are just trying to learn how to use loops. I've tried looking online for help, but everything I find seems to use more advanced coding. This should be as basic as a c program should be, thanks again


CPP / C++ / C 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 just for          
                                                                             //testing loop

        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  
Old 07-Feb-2016, 09:26
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,505
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

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


To start with the "first problem", one potential way to do that is to create an endless loop that keeps prompting input (for both bad AND good numbers) as part of its handling, but there must be a way to exit, of course:
CPP / C++ / C Code:
#include <stdio.h>

int main(void) { 
    int input;
    int remainder;

    while ( 1 ) {  /* continuous loop */
        printf("\nEnter a number that is divisible by 3 (use -1 to quit): ");
        scanf("%i",&input);
        
        if ( -1 == input ) {  /* The quit value */
            break;
        }

        remainder = input % 3;

        if( 0 == remainder ) {
            printf("\n%i is divisible by 3!\n\n",input); // this is just for //testing loop
        }
        else {
            printf("\n\n%i is not divisible by 3, try again:\n",input);
        }
    }

    printf("Have a nice day!\n");
    
    return 0;
}
Here's some output based on that example:
Code:
Enter a number that is divisible by 3 (use -1 to quit): 3 3 is divisible by 3! Enter a number that is divisible by 3 (use -1 to quit): 4 4 is not divisible by 3, try again: Enter a number that is divisible by 3 (use -1 to quit): -1 Have a nice day!
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
Last edited by TurboPT : 07-Feb-2016 at 10:26.
  #3  
Old 09-Feb-2016, 19:30
budgie1980 budgie1980 is offline
New Member
 
Join Date: Feb 2016
Posts: 6
budgie1980 is on a distinguished road

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


Thanks so much for your reply, it helped greatly.
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
base64 encoding questions Howard_L C Programming Language 4 24-Feb-2012 16:10
NetBeans - Inventory Program biglez123 Java Forum 1 23-Jan-2011 20:48
[TUTORIAL] Calling an external program in C (Linux) dsmith C Programming Language 4 22-Apr-2005 13:30
Need help with a C program (Long) McFury C Programming Language 3 29-Apr-2004 20:06

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:02.


vBulletin, Copyright © 2000 - 2017, Jelsoft Enterprises Ltd.