Thread: A quick and easy problem (not for me though)

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    8

    A quick and easy problem (not for me though)

    Hello,

    I have a problem in C:

    Print all numbers in this form: a23a, where a is a digit, which are divisible by 6;

    I know the divisible part(if(x%6==0), etc.) but i'm having problems with the a23a;

    The problem must be solve in the easiest way possible.
    Thank you!
    Last edited by MySecretName; 10-14-2011 at 01:02 PM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    printf() gives you the complete control you need for this.

    print the divisible int, then 23, then the divisible int variable again. Print four numbers, with no spaces between them.

    Post up the code and we can see where the problem is. Right now, I can't understand what the stumbling block is.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    117
    Don't quite understand what you want.

    you want to print all the combinations of a23a and a is divisible by 6?

    so for positive numbers output should be:
    Code:
    6236
    122312
    182318
    so on...
    If so it will be an infinite loop and go on forever. Is there a max you put to a?

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    8
    Quote Originally Posted by JonathanS View Post
    Don't quite understand what you want.

    you want to print all the combinations of a23a and a is divisible by 6?

    so for positive numbers output should be:
    Code:
    6236
    122312
    182318
    so on...
    If so it will be an infinite loop and go on forever. Is there a max you put to a?
    Not an ifinite loop, just the numbers that have 23 in the middle , for example:

    1231
    1232
    3235
    9237
    ...

    All the combinations of 4 digits with 23 in the middle.

    @Adak: Thanks, I'm trying to figure it out right now.

    LATER EDIT:

    @Adak: using printf does not alow me to anything with the output number, for example:

    Code:
    int i;
    
    for(i=0; i<10; i++)
    {
        printf("%d23%d\n", i,i);
    }
    
    ...
    I need some way to store the a23a, so i can put it inside an if condition and see if my a23a is divided by 6. If it's divided by 6 i will printf it, else do nothing.
    Last edited by MySecretName; 10-14-2011 at 02:13 PM.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    117
    I don't get the reasoning for the output. What is supposed to be divisible by 6?

    1231 is not divisible by 6. neither is a (which is one in this case).

    is a+2+3 divisible by 6 you mean?

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    So the first one OR two digits (as in 1231), must be divisible by 6, and the last digit can be any digit at all? (3235)???.

    Problem is, 92 is not divisible by 6, (90 is and so is 96 though), and neither is 9 of course. You WILL need a top end stopping point for the loop, even for preliminary testing. Maybe 50,000 to start?

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    8
    I'll give you a PHP example to be more clear:

    Code:
    <?php
    
    for($i=0; $i<10; $i++)
    {
        $mynumber = $i."23".$i;
    
        if($mynumber%6 == 0)
            echo $mynumber."<br />";
    }
    
    ?>
    OUTPUT:

    Code:
    2232
    8238
    The C program should check just this combinations(a23a -> a is the same digit):

    1231
    2232
    3233
    4234
    5235
    6236
    7237
    8238
    9239
    Last edited by MySecretName; 10-14-2011 at 02:28 PM.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    OK, this part, I understand - very simple:
    Code:
    The C program should check just this combinations(a23a -> a is the same digit):
    
    1231
    2232
    3233
    4234
    5235
    6236
    7237
    8238
    9239
    But 2 mod six is not 0, so the mynumber % 6==0, part, is confusing me. As in: 2232

    You could print out "23" as a string:

    Code:
    printf(%d%s%d \n", mynumber, string23, mynumber);
    or you could make 23 into a digit:

    Code:
    int twenty3 =23;
    
    printf("%d%d%d", mynumber, twenty3, mynumber);
    In either case, you could use a for loop, very much like your PHP example.

    Do you want to generate these combinations, or simply check them that they have this feature?
    Last edited by Adak; 10-14-2011 at 03:08 PM.

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    8
    I need to generate them, and every time i generate a number i need to check if the number is divided by 6.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int i,a;
    
    int main(int argc, char *argv[])
    {
        a = 23;
        
        for(i=0; i<10; i++)
        {
                 printf("%d%d%d\n", i, a, i);
        }
      
      system("PAUSE");    
      return 0;
    }
    How does this code is supposed to look like, where the if should be in order to output just 2232,8238?

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by MySecretName View Post
    Hello,

    I have a problem in C:

    Print all numbers in this form: a23a, where a is a digit, which are divisible by 6;

    I know the divisible part(if(x%6==0), etc.) but i'm having problems with the a23a;

    The problem must be solve in the easiest way possible.
    Thank you!
    Ok if a is a single digit... there is only 1 possibility ... 6236 ... there all done.

  11. #11
    Registered User
    Join Date
    Oct 2011
    Posts
    8
    How is 6236 divided by 6?

    Google

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by MySecretName View Post
    How is 6236 divided by 6?

    Google
    Ok... so you want to divide the whole number, not just the two "tacked on" digits...

    Carry on....

  13. #13
    Registered User
    Join Date
    Oct 2011
    Posts
    8
    Ok, thanks anyway for your help.
    Close this thread please, you are to advanced to solve this, i understand.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'm still confused about what you are trying to do here. Why are you singling out "23"?

    1230 is divisible by 6.


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by MySecretName View Post
    Ok, thanks anyway for your help.
    Close this thread please, you are to advanced to solve this, i understand.
    Naaa... just making a little joke then steppin' aside so the others can take their turn...

    I don't know who is giving out these bizarre assignments but I've never seen anything like this one come up in real-world programming.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. quick easy question
    By Yarin in forum C++ Programming
    Replies: 3
    Last Post: 01-25-2008, 01:58 PM
  2. another quick n easy question
    By david999 in forum C Programming
    Replies: 2
    Last Post: 11-10-2005, 02:59 PM
  3. quick n easy question
    By david999 in forum C Programming
    Replies: 2
    Last Post: 11-10-2005, 01:53 PM
  4. Very quick, easy question
    By Codefish in forum C++ Programming
    Replies: 10
    Last Post: 07-26-2005, 05:45 PM
  5. Quick Easy Question
    By St0rmTroop3er in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 02-24-2004, 01:08 AM