6
public class Return {    
    public static void main(String[] args) {        
        int answer = digit(9635, 1);
        print("The answer is " + answer);
    }

    static void print(String karen) {
        System.out.println (karen);
    }

    static int digit(int a, int b) {
        int digit = a;
        return digit;
    }     
}

Create a program that uses a function called digit which returns the value of the nth digit from the right of an integer argument. The value of n should be a second argument.

For Example: digit(9635, 1) returns 5 and digit(9635, 3) returns 6.

Sumurai8
  • 20,333
  • 11
  • 66
  • 100
Joey
  • 111
  • 3
  • 4
  • 9
  • 1
    Having any specific issue with the code? Have you tried anything? – Rohit Jain Oct 05 '13 at 05:14
  • 1
    Keep dividing by 10 a certain number of times, then take modulo 10 at the end. Is that enough to get you going with your homework? – Floris Oct 05 '13 at 05:14
  • what does digit(9635, 1) returns 5 means... what is the returns 5 is it from 9635? – Joey Oct 05 '13 at 05:16
  • 3
    I suggest that you read the description again: "Create a program that uses a function called digit which returns the value of the nth digit from the right of an integer argument." explains exactly where the `5` comes from. – Code-Apprentice Oct 05 '13 at 05:17
  • Yes! @Code-Guru what i mean is what is returns 5 means... where does it come from.. I'm presently learning Java in our school... – Joey Oct 05 '13 at 05:19
  • In Java, "`digit(9635 ,1)` returns 5" means that the result of the `digits` function is 5 when the inputs are 9635 and 1. Do you understand *why* this is the result? – Code-Apprentice Oct 05 '13 at 05:20
  • @Code-Guru can you please give me some examples how to code return based on my problem... so i can understand.... – Joey Oct 05 '13 at 05:21
  • You can get the last digit by dividing by 10 and look at the remainder of the division. int lastDigit = a%10; – Tesseract Oct 05 '13 at 05:23
  • @Code-Guru no sir i really don't have any idea why... I'm a bit confused.... – Joey Oct 05 '13 at 05:23
  • @Joey First, I suggest you make sure you know why the examples give the results that are stated. Then I suggest that you research how the `/` and `%` operators work. These are crucial to understanding how to write a very short function to get those results. – Code-Apprentice Oct 05 '13 at 05:23
  • 1
    @Joey You also need to understand how numbers are represented in the decimal system that we use every day. – Code-Apprentice Oct 05 '13 at 05:24
  • @Joey What is the 1st digit from the right of 9635? What is the 3rd digit from the right of 9635? – Code-Apprentice Oct 05 '13 at 05:26
  • You can also think about this recursively. – Tesseract Oct 05 '13 at 05:27
  • When struggling for what to do, try writing what you know, even if it's just the statement of the problem or a definition of one of its words! – clwhisk Oct 05 '13 at 05:30
  • @Code-Guru: Am i going to divide or get a remainder from digits(9635, 1)? .. i just dont get why it says returns 5? – Joey Oct 05 '13 at 06:02
  • What is the 1st digit from the right of 9635? – Code-Apprentice Oct 05 '13 at 18:08
  • @Code-Guru number 5... how can i show number 5 if the user inputs 1?? – Joey Oct 07 '13 at 12:39
  • The user should input **both** numbers: 9635 and 1. – Code-Apprentice Oct 07 '13 at 20:40

4 Answers4

29

Without spoon-feeding you the code:

The nth digit is the remainder after dividing (a divided by 10b-1) by 10.

int digit(int a, int b) {
return a / (int)Math.pow(10, b - 1) % 10;
}
See live demo.


If you want an iterative approach:

Loop b-1 times, each time assigning to the a variable the result of dividing a by 10.
After looping, the nth digit is the remainder of dividing a by 10.

int digit(int a, int b) {
while (--b > 0) {
a /= 10;
}
return a % 10;
}
See live demo.


Relevant facts about Java:

  • The modulo operator % returns the remainder after division, eg 32 % 10 returns 2

  • Integer division drops remainders, eg 32 / 10 returns 3.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
2
static int dig(int a, int b) {
    int i, digit=1;
    for(i=b-1; i>0; i++)
        digit = digit*10;
    digit = (a/digit) % 10;
    return digit;
 }
Matthieu
  • 2,736
  • 4
  • 57
  • 87
1

The other way is convert the digit into array and return the nth index

static char digit(int a,int b)
    {
        String x=a+"";
        char x1[]=x.toCharArray();
        int length=x1.length;
        char result=x1[length-b];
        return result;
    }

Now run from your main method like this way

System.out.println("digit answer  "+digit(1254,3));

output

digit answer  2
SpringLearner
  • 13,738
  • 20
  • 78
  • 116
  • @SpiderPig Maybe not fastest in real time, but that's the perfect computational theory answer – clwhisk Oct 05 '13 at 05:32
  • @SpiderPig Since the OP is struggling with terminology such as "returns", recursion is a less than helpful suggestion. (And I think I could code a direct calculation in less key strokes than I could do a recursive solution.) – Code-Apprentice Oct 05 '13 at 05:38
0

Convert number to string and then use the charAt() method.

class X{   
    static char digit(int a,int n)
    {
        String x=a+"";
        return x.charAt(n-1); 
    }

    public static void main(String[] args) {
        System.out.println(X.digit(123, 2));
    }
}

You may want to double check that the nth position is within the length of the number:

static char digit(int a, int n) {
    String x = a + "";
    char digit='\0' ;
    if (n > 0 && n <= x.length()) {
        digit = x.charAt(n - 1);
    }
    return digit;
}
Randa Sbeity
  • 370
  • 3
  • 10