-
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 ..
-
>> 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.
-
just as you said
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?
-
To add to (maybe clarify) Salem's comment: the condition for the while loop will never be false.
-
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? :confused:
-
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
-
wow man.... thnx for giving full... hehe figure out myself...thnx ...
-
but how to cout the number of step ?
-
-
can someone explain on this part of code
Code:
result = result + digit * digit;
day = day / 10;
-
Why don't you put a printf() inside the loop of that function and print each one out?