0

Hi ..

        for(int i=1;i<=n;i++) 
        { 
                first=i%10; 
last=i/10;

.......
i want to see how many number that first number equal last number;
like 3003..and 1,2,3... is counted because its a single numbers
but my statmant it is just count the num unti 100 and its not count the single numbers ..
I think the wrong in value of last ..i wamnted count unt the number that user inter
last =??
Thank you :)

Edited by Nick Evan: Fixed formatting

2
Contributors
6
Replies
7
Views
7 Years
Discussion Span
Last Post by programing
0
int first;
int last;
int counter =0;
for(int i=1;i<=n;i++){
last = i % 10;

for(first = i; first / 10 > 10; first = first / 10);

if (last == first) counter++;
}
0
for(int i=1;i<=n;i++) 

        { 

                first=i%10;
                if(first==last && last==first) 
                { 
                        counter++;

look just last the error
i try to put
last=0
last++;
but its failled

0

First of all line 7 first==last and last==first is the same thing you dont need to compare it twice, secondly to calculate last the easier way should be using a for loop
ie

// keep divide the number by 10 until it reaches the right most digit.
//ie 12345 / 10 = 1234, 1234 /10 = 123, 123 /10 = 12, 12/ 10 = 1 and you get last = 1
for (last = i ; last / 10 > 10; last = last/10)
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.