Skip to main content

Posts

Showing posts from May, 2022

22 may 2022 TCS CPA JAVA CODING QUESTION SOLVED.(reverse number if odd,else print cannot reverse.)

 Q1.Write main method in the MyClass class. In the main method,read an int value.Reverse the number and print it  only if the given number is odd.Else print the String "Cannot reverse". for example if the given number is 123 which is an odd number, we need to reverse the number and output should be 321. sample input1: 123 output: 321 sample input2: 788 output: Cannot reverse. *********************************************************************************** code: import java.util.Scanner; public class MyClass { public static void main (String[] args) { Scanner sc= new Scanner(System. in ) ; int num=sc.nextInt() ; sc.nextLine() ; if (num% 2 == 0 ) { System. out .println( "Cannot reverse" ) ; } else { int res= 0 ; int temp=num ; while (temp> 0 ) { int rem=temp% 10 ; res=res* 10 +rem ; temp=temp/ 10