How to get the Nth digit of a number from the right in Go

2 Answers

0 votes
package main

import (
	"errors"
	"fmt"
	"math"
)

/*
GetNthDigitFromRight returns the N-th digit of a number counting from the right.

Examples:

	GetNthDigitFromRight(12345, 1) = 5
	GetNthDigitFromRight(12345, 2) = 4
	GetNthDigitFromRight(12345, 3) = 3

Parameters:
  - inputNumber: The number to inspect.
  - digitPosition: The digit position (1-based, from the right).

Returns:
  - The digit at the specified position.
  - An error if digitPosition is less than 1.
*/
func GetNthDigitFromRight(inputNumber int64, digitPosition int) (int, error) {
	if digitPosition < 1 {
		return 0, errors.New("digit position must be greater than 0")
	}

	// Handle negative numbers.
	absoluteNumber := int64(math.Abs(float64(inputNumber)))

	// Calculate 10^(digitPosition - 1) using integer arithmetic.
	var divisor int64 = 1

	for positionIndex := 1; positionIndex < digitPosition; positionIndex++ {
		divisor *= 10
	}

	// Shift the desired digit to the ones place.
	shiftedNumber := absoluteNumber / divisor

	// Extract the digit.
	digit := int(shiftedNumber % 10)

	return digit, nil
}

func main() {
	inputNumber := int64(12745)

	firstDigit, err := GetNthDigitFromRight(inputNumber, 1)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}

	thirdDigit, err := GetNthDigitFromRight(inputNumber, 3)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}

	fifthDigit, err := GetNthDigitFromRight(inputNumber, 5)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}

	fmt.Printf("Number: %d\n", inputNumber)
	fmt.Printf("1st digit from right: %d\n", firstDigit)
	fmt.Printf("3rd digit from right: %d\n", thirdDigit)
	fmt.Printf("5th digit from right: %d\n", fifthDigit)
}



/*
run:

Number: 12745
1st digit from right: 5
3rd digit from right: 7
5th digit from right: 1

*/

 



answered 21 hours ago by avibootz
0 votes
package main

import (
	"errors"
	"fmt"
)

/*
GetNthDigitFromRight returns the N-th digit of a number counting from the right.

Examples:

	GetNthDigitFromRight(12345, 1) = 5
	GetNthDigitFromRight(12345, 2) = 4
	GetNthDigitFromRight(12345, 3) = 3

Parameters:
  - inputNumber: The number to inspect.
  - digitPosition: The digit position (1-based, from the right).

Returns:
  - The digit at the specified position.
  - An error if digitPosition is less than 1.
*/
func GetNthDigitFromRight(inputNumber int64, digitPosition int) (int, error) {
	if digitPosition < 1 {
		return 0, errors.New("digit position must be greater than 0")
	}

	if inputNumber < 0 {
		inputNumber = -inputNumber
	}

	var divisor int64 = 1

	for positionIndex := 1; positionIndex < digitPosition; positionIndex++ {
		divisor *= 10
	}

	return int((inputNumber / divisor) % 10), nil
}

func main() {
	inputNumber := int64(12745)

	firstDigit, err := GetNthDigitFromRight(inputNumber, 1)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}

	thirdDigit, err := GetNthDigitFromRight(inputNumber, 3)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}

	fifthDigit, err := GetNthDigitFromRight(inputNumber, 5)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}

	fmt.Printf("Number: %d\n", inputNumber)
	fmt.Printf("1st digit from right: %d\n", firstDigit)
	fmt.Printf("3rd digit from right: %d\n", thirdDigit)
	fmt.Printf("5th digit from right: %d\n", fifthDigit)
}


/*
run:

Number: 12745
1st digit from right: 5
3rd digit from right: 7
5th digit from right: 1

*/

 



answered 20 hours ago by avibootz
...