Books Online Courses
Free Tutorials  Go to Your University  Placement Preparation 
Latest:- Important tips to get an Off Campus Placements
0 like 0 dislike
1.9k views
in Coding Questions by Goeduhub's Expert (5.8k points)

Daisy has a Locker which contains a combination number lock. It needs a pair of numbers to open the lock. As a measure of safety, Daisy uses to change the key numbers once in a month. At the same time she is also afraid of forgetting it, and hence she writes the first number alone in a paper and keeps it secretly. From the first number she can easily find the second number because the second number is formed by considering each digit’s next odd /even sequence.

For example if the first number is 9278, then the second number is 1490.

Write a program that gets the first number from the user and gives the second number.

NOTE: If the input is 8888 or a negative number display "Invalid Input".

If input is 0053, the output should be 75.

If input is 8874, the output should be 96.

Sample input

Enter the first number

3648

Sample output

The second number is: 5860

Advertisement

1 Answer

0 like 0 dislike
by Goeduhub's Expert (5.8k points)
 
Best answer

Java Program

Locker.java
import java.util.*;;
public class Locker{
     public static void main (String[] args) {
         Scanner in=new Scanner(System.in);
         System.out.println("Enter the first number");
         int n=in.nextInt();
         if(n==8888||n<0||n>9999)
         {
             System.out.println("Invalid Input");
         }
         else{
             ArrayList<Integer> arr=new ArrayList<Integer>();
             int dig=0,rev=0;
             while(n!=0)
             {
                 dig=n%10;
                 rev=rev*10+dig;
                 n=n/10;
             }
             while(rev!=0)
             {
                 dig=rev%10;
                 if(dig%2==0){
                     if(dig==8){
                         dig=0;
                     }
                     else{
                         dig=dig+2;
                     }
                 }
                 else{
                     if(dig==9){
                         dig=1;
                     }
                     else{
                         dig=dig+2;
                     }
                 }
                 arr.add(dig);
                 rev=rev/10;
             }
             System.out.print("The second number is: ");
             String str="";
             for(int i=0;i<arr.size();i++)
                 str=str+arr.get(i);
             int k=0;
             while(k<str.length() && str.charAt(k)=='0')
             k++;
             StringBuffer sb=new StringBuffer(str);
             sb.replace(0,k,"");
             System.out.print(sb.toString());
         } 
     }
 }

For any queries refer to comment section

3.3k questions

7.1k answers

395 comments

4.6k users

Related questions

0 like 0 dislike
1 answer 17.3k views
asked May 11, 2020 in Coding Questions by Ankit Yadav Goeduhub's Expert (5.8k points)
0 like 0 dislike
1 answer 1.6k views
asked May 9, 2020 in Coding Questions by Ankit Yadav Goeduhub's Expert (5.8k points)
0 like 0 dislike
1 answer 4.1k views
asked May 9, 2020 in Coding Questions by Ankit Yadav Goeduhub's Expert (5.8k points)
0 like 0 dislike
1 answer 8.4k views
asked May 12, 2020 in Coding Questions by Ankit Yadav Goeduhub's Expert (5.8k points)
0 like 0 dislike
1 answer 25.5k views
asked May 12, 2020 in Coding Questions by Ankit Yadav Goeduhub's Expert (5.8k points)

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy || Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 
...
We and our partners share information on your use of this website to help improve your experience.