Showing posts with label Number Programs. Show all posts
Showing posts with label Number Programs. Show all posts

Saturday, July 13, 2019

Find All Palindrome Numbers from an Array in Java

In this article, we will see how to find all palindrome from an array. This is a very basic questions in interview, the interviewer will ask the same questions in different way. So, its good to know all possible questions from palindrome. Also there is a question to find all palindrome number from a list.  Find few more collection interview question.  Today we will see how to check a number is palindrome or not. 

FindAllPalindrome.java

package com.techbyteslearn.lab.basic;

public class FindAllPalindrome {
public static void main(String[] args) {
int numberArray[] = { 120, 990, 121, 777, 808, 1280 };
for (int i = 0; i < numberArray.length; i++) {
printOnlyPalindrom(numberArray[i]);
}
}
private static void printOnlyPalindrom(int number) {
int finalNumber = 0;
int oldNumber = number;
// Repeat the loop until the number became zero.
while (number != 0) {
// Get the First Digit (i.e. 1)
int firstDigit = number % 10;
// Get the Result number.
finalNumber = (finalNumber * 10) + firstDigit;
// Now get the remaining digits , after finding the first digit
number = number / 10;
}
// Now compare the finalNumber and oldNumber both are same or not.
if (finalNumber == oldNumber)
System.out.println(finalNumber + " is a Palindrome.");
}
}

Output -

121 is a Palindrome.
777 is a Palindrome.
808 is a Palindrome.

How to Check Whether a Number Is a Palindrome in Java


In this article, we will see how to check if a number is palindrome or not. This is a very basic questions in interview. But, you never know about what kind of question the interviewer will ask. So, better you prepare for every certain questions. Also there is a question to find all palindrome number from a list.  Find few more collection interview question.  Today we will see how to check a number is palindrome or not. 


Palindrome.java

package com.techbyteslearn.lab.basic;

public class Palindrome {

    public static void main(String[] args) {
        int number = 121;
        int temp = number;
        int finalNumber = 0;

        // Repeat the loop until the number becomes zero.
        while (number != 0) {

            // Get the last digit.
            int lastDigit = number % 10;

            // Build the reversed number.
            finalNumber = (finalNumber * 10) + lastDigit;

            // Remove the last digit from the number.
            number = number / 10;
        }

        // Compare the reversed number with the original number.
        if (finalNumber == temp) {
            System.out.println("This number is a Palindrome.");
        } else {
            System.out.println("This number is not a Palindrome.");
        }
    }
}

Output:

This number is a Palindrome.
 
 

One important correction from the original comments: % 10 gets the last digit, not the first digit. Also, number has already become 0 by the time of the final comparison, so comparing with temp is the correct approach.



Happy Learning.

Wednesday, July 10, 2019

Find One Missing Number from a List Using Java


In this article, we will see how to find the missing number from a list using java.  This is one of important common interview question asked in interview. You can see, how to find all missing numbers from a list. In this program we will use core java or the traditional way using for loop for finding the miss number from a list. Earlier post we had seen how to use stream for finding the missing number. Now we will see how to find one missing number using traditional core java style. 


The logic is very simple here, see the below.

  • At first we need to find the MAX number from the list. We need this MAX number because , we need to calculate the SUM of all natural number up to that max number. 
  • Then , we need to calculate the sum of all those natural number.
  • Then we will subtract each element from the given list from sumOfNaturalNumbers.
  • Now, the at the last  the value inside sumOfNaturalNumbers is the missing number.

FindOneMissingNumber.java

package com.techbyteslearn.lab.basic;

import java.util.ArrayList;
import java.util.Arrays;

public class FindOneMissingNumber {

    public static void main(String[] args) {
        ArrayList<Integer> numberList = new ArrayList<>(
                Arrays.asList(10, 3, 2, 4, 5, 6, 7, 9, 8, 14, 1, 11, 13));

        int sumOfNaturalNumbers = getSumUptoMax(findMax(numberList));

        for (int i = 0; i < numberList.size(); i++) {
            /*
             * Subtract each element from the list from sumOfNaturalNumbers.
             * The final value will be the missing number.
             */
            sumOfNaturalNumbers = sumOfNaturalNumbers - numberList.get(i);
        }

        int missingNumber = sumOfNaturalNumbers;
        System.out.println("Missing Number is :: " + missingNumber);
    }

    // Find the sum of all natural numbers up to limitNumber.
    private static int getSumUptoMax(int limitNumber) {
        int sum = 0;

        for (int i = 1; i <= limitNumber; i++) {
            sum = sum + i;
        }

        return sum;
    }

    // Find the greatest value from the list.
    private static int findMax(ArrayList<Integer> numberList) {
        int largest = numberList.get(0);

        for (int i = 1; i < numberList.size(); i++) {
            if (numberList.get(i) > largest) {
                largest = numberList.get(i);
            }
        }

        return largest;
    }
}

Output:

Missing Number is :: 12

 

This approach assumes the list contains the numbers from 1 through the maximum value, with exactly one number missing.


Happy Learning.