Wilson Soo
Wilson Soo

Reputation: 11

Lucky number with User Input

I'm facing troubles solving the following question: I suppose to get the user to input a number and check if it is a lucky number. A lucky number is the sum of squares of even-positioned digit (starting from the second position) is a multiple of 7.

Following is the example of my codes, when i run the program it will stuck at user input, please advise how do i get it to run:

public class Tester {

public static void main(String[] args) {
            
    Scanner scanner = new Scanner(System.in);

    System.out.println("Input a number: ");
    int number = scanner.nextInt();
    
    int count = 0;
    while(number!=0) {
        number/=10;
        ++count;
    }
    
    int[] array = new int[count];
    int sum = 0;
    
    for (int i=0; i<count; i++) {
        array[i] = scanner.nextInt();
    }

    for (int i=0; i<count; i++) {
        if(array[i]%2==0) {
            sum+=(array[i]*array[i]);
        }
        else {
            continue;
        }
    }
    
    if (sum%7==0) {
        System.out.println("The number: " +number+ "is a Lucky number");
    }
    else {
        System.out.println("Oops! Not a Lucky number");
    }
    
    scanner.close();
}
}

Upvotes: 0

Views: 6123

Answers (3)

CryptoFool
CryptoFool

Reputation: 23129

If I understand your description correctly, here's a program that does what you want:

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.print("Input a number: ");
    System.out.flush();
    int number = scanner.nextInt();

    int count = 0;
    int n = number;
    int sum = 0;
    while(n!=0) {
        int d = n % 10;
        n/=10;
        ++count;
        if (count % 2 == 0) {
            System.out.printf("sum = %d + %d^2 = %d\n", sum, d, sum + d * d);
            sum += d * d;
        }
    }

    if (sum%7==0) {
        System.out.printf("The number: %d is a Lucky number (%d = 7 * %d)", number, sum, sum / 7);
    }
    else {
        System.out.println("Oops! Not a Lucky number");
    }

    scanner.close();
}

A lucky result:

Input a number: 123456
sum = 0 + 5^2 = 25
sum = 25 + 3^2 = 34
sum = 34 + 1^2 = 35
The number: 123456 is a Lucky number (35 = 7 * 5)

Upvotes: 0

Kaviarasu
Kaviarasu

Reputation: 58

If you want even positioned digits,then you can directly get it in while loop.

while(number!=0) {
    if(count%2 !=0){
      int value = number %10;   // even positioned values
      // Do whatever you need to do with this value
    }
    number/=10;
    ++count;
}

If you want to convert the number into an digit array,then first find number of digits using log function and then store it array in reverse order.

int noOfDigits =(int) Math.floor(Math.log10(number)+1); // Finding No of digits
int[] array = new int[noOfDigits];

while(--noOfDigits>=0){
    array[noOfDigits] = number/10;         // storing every digits in reverse order
    number%=10;
}

I don't know below code will be helpful for your core logic,yet I record this too.

If you want Sum of Squares of even positioned digits in number which is represented as array, then you can use below code.

int sum = 0;
for (int i=1; i<array.length; i+=2) {
    sum += array[i] * array[i];
}

if (sum%7==0) {
    // print number is lucky
}
else {
   // print number is not lucky
}

Upvotes: 0

Gurusharan S
Gurusharan S

Reputation: 385

I believe the culprit is the below loop:

for (int i=0; i<count; i++) {
  array[i] = scanner.nextInt();
}

I think your intention was to get each of the digits into an array. However, you are getting an input from the scanner (which in this case is the user input) for count number of times.

While there are several ways of getting the number of digits and each digit into an array. I'm going to give you two ways. Also, I see no validations on the input integer (such as negative numbers, etc.) and I am going to ignore them right now.

Approach 1: Your for loop corrected

You just get the ith digit of the number using a formula.

for (int i=1; i<=count; i++) {
  array[i] = (int) (number / Math.pow(10, count-i)) % 10;
}

Approach 2: Converting the numbers to String and back using streams

List<Integer> digits = Arrays.toStream(number.toString().split("")).map(
  digitChar -> Integer.parseInt(digitChar)
).collect(Collectors.toList());

Note: You need to import the classes java.util.Arrays and java.util.stream.Collectors

Upvotes: 1

Related Questions