[Solved] Co-Partners in Train with Java, C++

Co-Partners in Train: Tim and Bob are off to a famous Education Fair “Knowledge Forum 2017” at Uzhlanda. This time they have to travel without their guardians. Tim got very interested in the arrangement of seats inside the train coach.
The entire coach could be viewed as an arrangement of consecutive blocks of size 8.

 BerthNumberCompartment
1-81
9-162
17-243
… and so on 

Each of these size-8 blocks are further arranged as:

 1LB,  2MB,  3UB,  4LB,  5MB,  6UB,  7SL,  8SU
 9LB, 10MB, …
…….
…….

Here LB denotes lower berth, MB middle berth and UB upper berth.

The following berths are called Co-Partners in Train:

3 UB6 UB
2 MB5 MB
1 LB4 LB
7 SL8 SU

and the pattern is repeated for every set of 8 berths. 
Tim and Bob are playing this game of finding the co-partner in train of each berth. Write a program to do the same.

Input Format:
The input consists of an integer N, which corresponds to the berth number whose neighbor is to be found out.

Output Format:
The output is to display the berth of the neighbor of the corresponding seat.
Refer sample input and output for formatting specifications.

Sample Input 1:
1

Sample Output 1:
4LB

Sample Input 2:
5

Sample Output 2:
2MB

Solution

import java.util.Scanner;
public class Main

{  
    public static void main(String[] args)
{
     Scanner sc=new Scanner(System.in);
     int n = sc.nextInt();
     if(n%8>0&&n%8<7)
     {
         int r = n%8;
         if(r<=3) 
         {
              n=n+3; 
              if(n%8==4) 
              {   
                 System.out.println(n+"LB");
              }
                 //sauravhathi
         else if(n%8==5) 
          { 
              System.out.println(n+"MB"); 
          } 
          else 
          { 
              System.out.println(n+"UB"); 
              
          } 
             
      }
      else 
      { 
          n=n-3; 
          if(n%8==1) 
          { 
              System.out.println(n+"LB"); 
          }
          else if(n%8==2) 
          {  
              System.out.println(n+"MB");
          }
          else 
          { 
              System.out.println(n+"UB");
          } 
          
       }
       
     }
     
     else 
     {
         int r=n%8;
     
     if(r==7) 
     {   
         n=n+1; 
         System.out.println(n+"SU"); 
     }     
     else 
     { 
         n=n-1; 
         System.out.println(n+"SL");
     }
     
}
}

}
#include <stdio.h>
int main(){
    int n,d,o;
    scanf("%d",&n);
    d = n%8;
    if(d!=0){
      if(d<4){
        o = n+3;
      }else if (d!=7){
        o=n-3;
      }
    }
    switch(d){
        case 0:
            printf("%dSL",n-1);
            break;
        case 1: case 4: 
            printf("%dLB",o);
            break;
        case 2: case 5:
            printf("%dMB",o);
            break;
        case 3: case 6:
            printf("%dUB",o);
            break;
        case 7:
            printf("%dSU",n+1);
    }
    return 0;
}

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

πŸ“Œ Nodejs and Android 😎
πŸ“Œ Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *