Exercise from my book.

This is a discussion on Exercise from my book. within the C Programming forums, part of the General Programming Boards category; Hi, I found an exercise on my book like this: "Exist 4 numbers, smallers than 500, that are special. If ...

  1. #1
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    Exercise from my book.

    Hi, I found an exercise on my book like this:

    "Exist 4 numbers, smallers than 500, that are special. If we take all his chars, but one after one, and elevates to the 3rd power, this will return the number. example:
    1^3 + 5^3 + 3^3 = 153"

    Now I need to make a program, to discover all numbers like this btw 0 and 500.

    I dont know where to start, cuz the only way I found is making just on Loop, and than convert the number(index) into string, catch all the 3 chars of the string, that represents a single number and then transform then into Integer, apply the 3rd power on them, sum and than see if it is equals to the NUM.

    But I think have another way, not? my way is so stupid I think.
    I dont need you guys make my home-work, I just want to understand.

    Thanks!!!

  2. #2
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    Ok, I found a way to solve this.

    It's an stupid way but works
    I'll be happy if someone could tell me a better way.

    Thanks!

    Code:
    /* header files */
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
    
    /* Functions */
    char subchar(char *string, int nIndex);
    void check(int nNum);
    int chatoi(char ch);
    
    /* Global array, to hold the found  numbers */
    int nNums[10];
    
    /* main */
    int main(void)
    {
        int nIndex; /* for looping */
        
        /* start looping */
        for(nIndex = 100; nIndex <= 500; ++nIndex)
            check(nIndex); /* see if it's the kind of number */
        
            
        /* pause */
        system("PAUSE");
        
        /* return 0 */
        return 0;
    }
    
    char subchar(char *string, int nIndex)
    {
        return string[nIndex]; /* return the right char */
    }
    
    void check(int num)
    {
        char ch1, ch2, ch3, str[3];
        int n1, n2, n3, curr = 0;
            
        itoa(num,str,10); /* transform integer into string */
        ch1 = subchar(str,0); /* catch first char */
        ch2 = subchar(str,1); /* second char */
        ch3 = subchar(str,2); /* third char */
        
        
        /* transform into numbers */
        n1 = chatoi(ch1);
        n2 = chatoi(ch2);
        n3 = chatoi(ch3);
        
        /* check if the 3rd power of the numbers added together
        results in the num */
        
        if(((n1 * n1 * n1) + (n2 * n2 * n2) + (n3 * n3 * n3)) == num)
            printf("Yes! %d\n",num);
            
    }
    
    int chatoi(char ch)
    {
        return ch - 48;
    }

  3. #3
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Converting it to a string is unnecissary.

    Just:
    --------
    divide the number by 100 to get the leftmost digit

    divide the number by 10 then mod by 10 to get the middle digit

    mod by 10 to get the rightmost digit

    Then just continue as usual.
    --------
    Also, I can tell you tat 0 is one right off the bat 0^3 + 0^3 + 0^3 is 0

  4. #4
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Code:
    #include<stdio.h>
    #include<math.h>
    
    int main()
    {
        int i;
    
        for( i = 0; i <= 500; ++i )
            if( pow( i / 100, 3 ) + pow( i / 10 % 10, 3 ) + pow( i % 10, 3 ) == i )
                printf( "%d\n", i );
    
        return 0;
    }

    Note: it's 6 numbers, not 4
    Last edited by Polymorphic OOP; 12-05-2002 at 03:35 AM.

  5. #5
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    Ok, thanks man :)

    Really thanks.

  6. #6
    orbitz_school
    Guest
    My two cents
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main (void)
    {
            int     left, middle, right;
    
            for (left = 0; left < 5; left++)
                    for (middle = 0; middle < 10; middle++)
                            for (right = 0; right < 10; right++)
                            {
                                    if ((pow (left, 3) + pow (middle, 3) + pow (right, 3)) ==
                                                    left * 100 + middle * 10 + right)
                                            printf ("%d%d%d\n", left, middle, right);
                            }
            return 0;
    }
    Is 4 3 digit numbers, which I assume is what you rbook wants. (the other 2, to make it are real cheap)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help with the exercise from the book
    By kenryuakuma in forum C++ Programming
    Replies: 19
    Last Post: 09-26-2008, 08:07 AM
  2. Help with K&R Book Exercise
    By Alejandrito in forum C Programming
    Replies: 5
    Last Post: 03-11-2008, 01:24 PM
  3. book exercise
    By luigi40 in forum C# Programming
    Replies: 1
    Last Post: 11-13-2005, 10:28 AM
  4. Books on C and C++
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-28-2002, 03:18 PM
  5. any recommended exercise book for C++?
    By gogo in forum C++ Programming
    Replies: 5
    Last Post: 11-07-2001, 03:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21