nicetomeetyou98
nicetomeetyou98

Reputation: 57

Finding the nth Digit from a number not more than 8 digits (starting from left)

Can anyone enlighten me on how to retrieve the nth digit of an integer, without treating the integer as a string. Just purely using for or while loop, and starting from the left.

So for example if the integer is 2345,

when I enter position 1; it should return 2,
position 2 should return 3, so on and so forth.

I really have no idea how to tackle this question.. Please help. Thank you

I did some code below but I know it is wrong.

import java.util.*;
class FindAnyDigit {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a positive integer: ");
        int integer = sc.nextInt();
        System.out.println("Enter the position where you wish to retrieve the digit: ");
        int position = sc.nextInt();
        int multiple = 1;
        int digit = 1;

        for (int i = 1; i <= position; i++) {
            multiple = multiple * 10;
            while (integer > multiple) {
                digit = integer % 10;
                integer = integer / 10;
            }
        }
        System.out.println("The digit at " + position + 
               " position is " + digit);
    }
}

Upvotes: 3

Views: 890

Answers (4)

GURU Shreyansh
GURU Shreyansh

Reputation: 909

N-th digit from the left is Number of digits - N + 1-th digit from the right. So, using this we can easily find out the desired digit.
First, we need to find the number of digits in the number integer which can be easily done using a single while loop.
Then iterate for position-1 times and keep dividing the integer by 10 so that we can get the current last digit of the number, like done below:

int i = 1;
int newPosition = digit - position + 1;
while (i < newPosition)
{
    ++i;
    integer /= 10; // digit at `i-th` position from right
}
System.out.println("The digit at " + position + " position is " + integer % 10);

Upvotes: 1

Ricola
Ricola

Reputation: 2922

To retrieve a particular digit without transforming the number to string, you can divide by 10 util the digit you're looking for became the rightmost digit. Then you just need to apply modulo 10 to get that digit.

When you want the ith digit from the right, it's easy, you just have to divide by 10 i-1 times. But in your case you need the nth digit from the left. So you just have to find how to get i from n, which is i = totalDigits - n +1. So i-1 = totalDigits - n and you have to divide totalDigits - n times.

To get the total number of digits, you just need to count how many times you need to divide your number by 10 for it to equal 0.

Here is this solution, without using any math import: (in the code below, digitsFromTheRight = i-1)

import java.util.Scanner;

class FindAnyDigit {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a positive integer: ");
        int integer = sc.nextInt();
        System.out.println("Enter the position where you wish to retrieve the digit: ");
        int position = sc.nextInt();

        int digit = getNthDigit(integer, position);

        System.out.println("The digit at " + position +
                " position is " + digit);
    }

    private static int getNthDigit(int integer, int position){
        int totalDigits = countDigits(integer);
        if(position > totalDigits){
            throw new IllegalArgumentException("position is bigger than the total number of digits!");
        }
        int digitsFromTheRight = totalDigits - position;

        for (int i = 1; i <= digitsFromTheRight; i++) {
            integer /= 10;
        }

        return integer %10;
    }

    private static int countDigits(int n) {
        int count = 0;
        while (n != 0)
        {
            n = n / 10;
            ++count;
        }
        return count;
    }
}

Upvotes: 1

Ewan Donaldson
Ewan Donaldson

Reputation: 46

The following solution achieves what you desire simply.

import java.util.*;
public class FindAnyDigit {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
    
        System.out.println("Enter a positive integer: ");
        int integer = sc.nextInt();
    
        System.out.println("Enter the position where you wish to retrieve the digit: ");
        int position = sc.nextInt();
            
        LinkedList<Integer> stack = new LinkedList<Integer>();
        while(integer > 0){
            stack.push(integer % 10);
            integer = integer / 10;
        }
    
        System.out.println(stack.get(position - 1));
    }
}

Upvotes: 3

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521093

One way to phrase this problem is that, given a one-based index on the left side of the number, we want to divide the number by 10 the number's length minus index number of times. Then, just return the mod 10 value of the remaining input.

public static int getDigit(int input, int pos) {
    int length = (int) (Math.log10(input) + 1);   // number of digits in input
    for (int i=0; i < length-pos; ++i) {
         input = input / 10;                      // put target digit in tens place
    }
    
    return input % 10;
}

public static void main(String args[]) {
    int input = 2345;
    System.out.println(getDigit(input, 1));  // 2
    System.out.println(getDigit(input, 2));  // 3
    System.out.println(getDigit(input, 3));  // 4
    System.out.println(getDigit(input, 4));  // 5
}

Upvotes: 4

Related Questions