Wednesday, March 3, 2021

Java Code Solution | TCS NQT Previous Year Coding Question and Solution

1. A Sober Walk

Our hoary culture had several great persons since time immemorial and king vikramaditya’s nava ratnas (nine gems) belongs to this ilk.They are named in the following shloka:


Among these, Varahamihira was an astrologer of eminence and his book Brihat Jataak is recokened as the ultimate authority in astrology. He was once talking with Amarasimha,another gem among the nava ratnas and the author of Sanskrit thesaurus, Amarakosha. Amarasimha wanted to know the final position of a person, who starts from the origin 0 0 and travels per following scheme.


  • He first turns and travels 10 units of distance
  • His second turn is upward for 20 units
  • Third turn is to the left for 30 units
  • Fourth turn is the downward for 40 units
  • Fifth turn is to the right(again) for 50 units

… And thus he travels, every time increasing the travel distance by 10 units.


Constraints:

2<=n<=1000


Input:

3


Solution

import java.util.Scanner;
class Main{
    
    
    public static void main(String[] args){
        
        
        System.out.println("Enter the number of steps taken by Mr. Vikrant");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int x=0;
        int y=0;
        for(int i=1;i<=n;i++){
            
            
            
            if(i%2==0){
                if(i%2==0 && i%4!=0){
                    
                    y=y+(i*10);
    
                }
                else{
                
                    y=y-(i*10);
                
                }
                
            }
            else{
               
                
                if(i%4==1){
                    
                    x=x+i*10;
                    
                }
                else{
                    
                    x=x-(i*10);
                }
            }
            }
           System.out.println(x+","+y);
       
    }
}


2. Rock, Paper and Secissor Game

In this game, user gets the first chance to pick the option among Rock, paper and scissor. After that computer select from remaining two choices(randomly), then winner is decided as per the rules.

Winning Rules as follows :

Rock vs paper-> paper wins
Rock vs scissor-> Rock wins
paper vs scissor-> scissor wins

Solution

import java.util.Scanner;
import java.util.Random;

public class Main{
    
    public static void main(String args[]){
        
        int rock=0;
        int scissor=1;
        int paper=2;
        
        Scanner sc=new Scanner(System.in);
        Random ran=new Random();
        int winning=0;
        int losing=0;
        
        
        for(int i=0;i<=5;i++){
            
            System.out.print("Choose-> 0 for rock, 1 for scissor, 2 for paper: ");
            int user = sc.nextInt();
            int comp= ran.nextInt(3);
            if(user==0 && comp==1 || user==1 && comp==2 || user==2 && comp==0){
                winning=winning+1;
                System.out.println("Congrates! you won the game");
                
            }
            
            else{
                System.out.println("Sorry, You lost");
                losing=losing+1;
            }
            
            System.out.println("Play Again");
            
            
        }
        System.out.println("Total winning= "+winning);
        System.out.println("Total losing= "+losing);
    }
}

No comments:

Post a Comment

Java Code Solution | TCS NQT Previous Year Coding Question and Solution

1. A Sober Walk Our hoary culture had several great persons since time immemorial and king vikramaditya’s nava ratnas (nine gems) belongs t...