Showing posts with label Core Java. Show all posts
Showing posts with label Core Java. Show all posts

How to reverse the given number

SDET

 We are going to see how to reverse the number is Java.

Suppose We have number 24586 so reverse of this number will be 68542.
To reverse the number we are going to use following logic.
1. Divide the number by 10 to find remainder using modulus operator(%) .
2. Define reverse number variable and initialize 0 value
3. Multiply reverse number by 10 and add remainder to it.
4.Divide the number by 10.

Let's see Java code for this Problem.
  1. public class ReverseNumber {  
  2. public static void main(String[] args)  {  
  3. int number = 24586, reverse = 0;  
  4. while(number != 0{  
  5. int remainder = number % 10;  
  6. reverse = reverse * 10 + remainder;  
  7. number = number/10;  
  8. }  
  9. System.out.println("The reverse of the given number is: " + reverse);  
  10. }  
  11. }  

Given an array of ints, return true if the number of 1's is greater than the number of 4's

SDET

Let's understand the problem by following examples.

Ex1: [1, 4, 1] → true
Ex2: [1, 4, 1, 4] → false
Ex3: [1, 1] → true
Ex4: [1, 4, 4, 4] → false
Ex51: [1, 1, 1, 4] → true

Solution.

public class arrayProblem {

public static void main(String[] args) {

int [] a = {1,1,1,4,4};

int countof_1=0, countof_4=0;

for (int i=0; i<a.length; i++){

if (a[i]==1){

countof_1++;
}

else if (a[i]==4){

countof_4++;
}
}

if (countof_1>countof_4){

System.out.println(true);
}

else {

System.out.println(false);
}
}
}


Write Java Program to find sum of digits of given number.

SDET

 Let's we have a number 65847 so sum of digits of given number is 30(6+5+8+4+7). We are going to use following logic to solve this problem.

1. we'll break the number using modulus operator by using while loop.
2.Then add all digits.

  1. public class SumOfDigits{
  2. public static void main(String [] args){
  3. int number = 65847;
  4. int sum = 0, digit;
  5. while(number!=0){
  6. remainder = number%10;
  7. sum = sum + digit;
  8. number = number/10;
  9. }
  10. System.out.println("Sum of digits of the given number is "+sum); // 30
  11. }
  12. }

How to check whether given number is prime or not in Java

SDET

 As we know that prime number is a natural number which is greater than 1 and divisible by 1 and itself. So it means that it has only two factors. We are going to use this logic to check whether number is prime or not. 

Solution:-

public class primeNumber {

public static void main(String[] args) {

int number=19;
int factor=0;
if(number>1){
for (int i=0; i<=number; i++){
if (number%b==0){
factor++;
}
}
}
if (factor==2){
System.out.println("Given number is prime number");
}else if (factor>2){
System.out.println("Given number is non prime number");
}
}
}



Return an array where each zero value in the array is replaced by the largest odd value to the right of the zero in the array.

SDET

  Understand the problem by following Examples.

Ex1: [0, 7, 0, 3] → [7, 7, 3, 3]
Ex2: [0, 2, 0, 3] → [3, 2, 3, 3]
Ex3: [0, 1, 0] → [1, 1, 0]

Solution:-

public class array1 {

    public static void main(String[] args) {

        int [] a = {0,2,0,3};

        for (int i=0; i<a.length; i++){

        if (a[i]==0){

        for (int j=i+1; j<a.length; j++){

        if (a[j]%2!=0){

        a[i]=a[j];

        break;
           }
                }
            }
        }

        for (int k=0; k<a.length; k++){

            System.out.print(a[k] + " ");

        }
}}