programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Finding the position of integer from the right side

man luke
Greenhorn
Posts: 3
Finding the position of integer from the right side

The position of the digit is counted from the right and starts from 1

For example if I input position(12315,1) it will return 2, so 5 is at index 1 and 1 is at index 2

So far I have done this

Ranch Hand
Posts: 146
4
OK, what happens when you compile and execute the code?

man luke
Greenhorn
Posts: 3
if i input position(2,2) output will be 1
if i input position(12354,2) nth happen

Henry Wong
author
Sheriff
Posts: 23295
125
man luke wrote:
if i input position(12354,2) nth happen

What do you mean by "nth happen"?

And BTW, considering that the digit is *not* always equals to the position (unless it is something that counts down like "987654321"), I think it is safe to say that your results for many cases would be incorrect.

Henry

man luke
Greenhorn
Posts: 3
nth happen means nothing happen no output

Junilu Lacar
Sheriff
Posts: 11494
180
To write a correct program, you need to fully understand what every part of your program is telling the computer to do. The program you have written so far says this:

If the digit in the number's ones place is the digit we're looking for, then the position is 1 (lines 4-7)
otherwise, if the number is negative, then the position is 0 (lines 9-12)
otherwise, the position is the digit we're looking for (line 14)

Only the first part of is correct. The rest makes no sense at all.

A correct solution here will have three key elements; you already have the first one which is a way to check the first digit of the number. You're still missing the other two, namely:

1. Checking all the other digits in the number in turn
2. Keeping track of how many digits you've checked so far

Here's an analogy that might give you an idea for solving this: Imagine a line of soldiers and a sergeant who wants to inspect each soldier's uniform. The most natural way this happens is for the soldiers to all stand at attention as the sergeant slowly makes his way down the line, inspecting each soldier in turn. Now, what if that sergeant is feeling kind of lazy and doesn't feel like walking down the line. Instead, the sergeant just stands at one end of the line and as he gets done inspecting the soldier there, the sergeant dismisses him and has the line move so that the next soldier is now standing in front of him. He does this repeatedly until he has inspected all the soldier's uniforms.

So, you're the sergeant and the number is the line of soldiers you want to inspect. You already figured out how to inspect the soldier in front of you, now figure out how to get the rest of the soldiers in line to move towards you and how to repeat the process until you've inspected everyone.

Campbell Ritchie
Marshal
Posts: 56578
172
Welcome to the Ranch

That is an unusual definition of the position of digits; I shall regard it as specific to this exercise because other digit numbering systems are used in other situations.