Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How do I solve these kinds of questions, I need a detailed explanation Write a s

ID: 3582244 • Letter: H

Question

How do I solve these kinds of questions, I need a detailed explanation

Write a static Java method called reverseSame to compare two integers to see if the first integer contains the same digits as the second integer but in reverse order.

For example,

- if num1 = 1234 and num2 = 4321 then reverseSame ( num1 , num2 ) return s true

-if num1 = 1234 and num2 = 456 1 then reverseSame(num1, num2) return s false

Note: you are only allowed to use additional variables of primitive types. In particular, you are not allowed to use any String variables.

Explanation / Answer

solution

package com.nu.test;

import java.util.Scanner;

public class NPTest {
  
  
   public static boolean reverseSame(int num1,int num2)
   {
       int reversenumber=0;
       //the loop continues upto num1 not equal to zero
   while( num1 != 0 )
   {
       reversenumber = reversenumber * 10;
       reversenumber = reversenumber + num1%10;
   num1 = num1/10;
   }
   //if reverse of num1 equals to num2 it returns true
       if(reversenumber==num2)
       {
             
           return true;
       }
       //if reverse of num1 not equals to num2 it returns false
       else
           return false;
      
   }
  
   public static void main(String[] args) {
      
       Scanner scanner=new Scanner(System.in);
      
       System.out.println("enter number one");
      
       int num1=scanner.nextInt();
      
       System.out.println("enter number two");
      
       int num2=scanner.nextInt();
       //the function takes the num and reverse of a num1
       boolean flag=reverseSame(num1,num2);
      
       if(flag==true)
       {
           System.out.println("the given no and reverse of a given no is same");
       }else
       {
           System.out.println("the given no and reverse of a given no is not same");
       }
      
       scanner.close();
      
   }

}

output

enter number one
1234
enter number two
4321
the given no and reverse no is same

enter number one
1234
enter number two
4561
the given no and reverse no is not same

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote