Thread: squared digit length

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    141

    squared digit length

    hi everyone this is my code for square digit length

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int day = 29;
        int mod;
        int div;
        int total;
        int counter = 0;
        int working = ((mod * mod) + (div * div))
        
        mod = day%10;//gives 9
        div = day/10;// gives 2
        total = day;
        
        while(working != 4 || working != 1)
        {
            total = (mod * mod) + (div * div);
            
            counter++;
        }    
        cout<<counter<<endl;
        
        
        
    system("pause");
    return 0;
    }
    is not working, can someone help me on this?

    and i got a problem for changing the integer for day ..

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> int working = ((mod * mod) + (div * div))

    at this point mod and div are uninitialized.

    >> while(working != 4 || working != 1)

    your loop makes no sense. working never gets modified so the loop becomes infinite. besides that, the assignment in the loop will always yield the same result.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Arggggh DeepFyre's Avatar
    Join Date
    Sep 2004
    Posts
    227
    just as you said
    Code:
    int counter = 0;
    you have to say
    Code:
    int mod = 0;//or whatever value
    likewise
    Code:
    int div = 0;//or whatever value

    Code:
        while(working != 4 || working != 1)
        {
            total = (mod * mod) + (div * div);
            
            counter++;
        }
    lets think about this....ASSUMING that div = 0 (which it doesnt) and assuming that mod = 0 (which it doesnt) ... working = (0 * 0) + (0 * 0)
    working = 0

    now ... after you set working = ((mod * mod) + (div * div)) ... does that value ever change after that?
    Keyboard Not Found! Press any key to continue. . .

  4. #4
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    To add to (maybe clarify) Salem's comment: the condition for the while loop will never be false.

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    can someone give me some idea on how to count the squared digit length.

    for example 29 is input

    and the process should be like this:

    2^2 + 9^2 = 85
    8^2 + 5^2 = 89
    8^2 + 9^2 = 145
    1^2 + 4^2 + 5^2 = 42
    4^2 + 2^2 = 20
    2^2 + 0^2 = 4

    this loop ends when the final solution ==4 or 1, and count how many step it takes....

    can someone please give me some idea... ? please?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    #include <stdio.h>
    
    int calc ( int value ) {
      int result = 0;
      while ( value > 0 ) {
        int digit = value % 10;
        result = result + digit * digit;
        value = value / 10;
      }
      return result;
    }
    int main ( ) {
      int num = 29;
      while ( num > 10 ) {
        int result = calc ( num );
        printf( "%d -> %d\n", num, result );
        num = result;
      }
      return 0;
    }
    
    
    $ gcc bar.c
    $ ./a.out
    29 -> 85
    85 -> 89
    89 -> 145
    145 -> 42
    42 -> 20
    20 -> 4
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    wow man.... thnx for giving full... hehe figure out myself...thnx ...

  8. #8
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    but how to cout the number of step ?

  9. #9
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    is alright, i got it ...

  10. #10
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    can someone explain on this part of code

    Code:
            result = result + digit * digit;
            day = day / 10;

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Why don't you put a printf() inside the loop of that function and print each one out?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Strange string behavior
    By jcafaro10 in forum C Programming
    Replies: 2
    Last Post: 04-07-2009, 07:38 PM
  3. Need help with a snake game (ncurses)
    By Adam4444 in forum C Programming
    Replies: 11
    Last Post: 01-17-2007, 03:41 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. newbie programmer - needs help bad.
    By hortonheat in forum C Programming
    Replies: 17
    Last Post: 10-20-2004, 05:31 PM