Posts

Showing posts with the label java 2024

Check if String contains only Digit's - Java 2024 - Technical Interview

 Check String contains only Digit's           Get the input from the user using Scanner class. Then create a method to check the following condition. 1. Check if the string is null or empty, if it is directly return false. 2. Iterate the String using for loop inside the for loop, use the if for condition checking. 3. Condition is check the Digit's only, for this use the Character Wrapper class, in that we have isDigit() method to check the Digit's. 4. This method will return true or false based on it is Digit's or not. Code : package com.dinatechy.challange; import java.util.Scanner; public class CheckDigitsOnly { public static void main(String[] args ) { //To check if the String contains only Digit's Scanner scan = new Scanner(System. in ); String data = scan .next(); scan .close(); boolean isDigits = checkDigits ( data ); System. out .println( isDigits ? "Yes it is" : "No it's not" ); } privat...

Check the number is Prime using Java - Technical Interview - MNC - 2024

 Check the number is Prime                    First we need to check whether the number is 1 or 0, if it is straight away you can say it is Prime Number.                    Next we need to divide the number by 2, because if we couldn't divide a number by more than half of a number.                    Final Thing is start the for loop and inside for loop modules the given number by loop variable, if the remainder is zero it is not Prime, if not it is Prime Number. Code :  package com.dinatechy.challange; import java.util.Scanner; public class PrimeNumber { public static void main(String[] args ) { //Check if the given number is Prime or not Scanner scan = new Scanner(System. in ); int number = scan .nextInt(); scan .close(); boolean result = isPrime ( number ); System. out .pri...