# TCS Coding Question 1 | Our hoary culture had several great persons ….

## Problem Statement

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. ### 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.

### Test Cases

#### Case 1

• Input : 3
• Expected Output :-20 20

#### Case 2

• Input: 4
• Expected Output: -20 -20

#### Case 3

• Input : 5
• Expected Output : 30 -20

#### Case 4

• Input : 7
• Expected Output : 90 -20 ```#include <stdio.h>
#include <stdlib.h>

int main()
{
int n;
scanf("%d", &n);

char c = 'R';
int x = 0, y = 0;
int distance = 10;

while(n)
{
switch(c)
{
case 'R':
x = x + distance;
c = 'U';
distance = distance + 10;
break;

case 'U':
y = y + distance;
c = 'L';
distance = distance + 10;
break;

case 'L':
x = x - distance;
c = 'D';
distance = distance + 10;
break;

case 'D':
y = y - distance;
c = 'A';
distance = distance + 10;
break;

case 'A':
x = x + distance;
c = 'R';
distance = distance + 10;
break;
}
n--;
}

printf("%d %d",x,y);
return 0;
}```
```#include <iostream>

using namespace std;
int main()
{
int n;
cin >> n;

char c = 'R';
int x = 0, y = 0;
int distance = 10;

while(n)
{
switch(c)
{
case 'R':
x = x + distance;
c = 'U';
distance = distance + 10;
break;

case 'U':
y = y + distance;
c = 'L';
distance = distance + 10;
break;

case 'L':
x = x - distance;
c = 'D';
distance = distance + 10;
break;

case 'D':
y = y - distance;
c = 'A';
distance = distance + 10;
break;

case 'A':
x = x + distance;
c = 'R';
distance = distance + 10;
break;
}
n--;
}

cout << x << " " << y <<endl;
return 0;
}```
```import java.util.Scanner;

public class Main {

public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int testCase = sc.nextInt();
getDistance(testCase);
}

public static void getDistance(int a) {
int distance = 10;
int x = 0;
int y = 0;
char ch = 'R';
while(a > 0)
{
switch(ch)
{
case 'R':
x = x + distance;
ch = 'U';
distance = distance+10;
break;

case 'U':
y = y + distance;
ch = 'L';
distance = distance + 10;
break;

case 'L':
x = x - distance;
ch = 'D';
distance = distance + 10;
break;

case 'D':
y = y - distance;
ch = 'A';
distance = distance + 10;
break;

case 'A':
x = x + distance;
ch = 'R';
distance = distance + 10;
break;
}
a--;
}
System.out.println(x+ " , "+y);
}

}```
```n = int(input())
c = 'R'
dis = 10
x,y=0,0
for i in range(n):
if c=='R':
x=x+dis
c='U'
dis=dis+10
elif c=='U':
y=y+dis
c='L'
dis=dis+10
elif c=='L':
x=x-dis
c='D'
dis=dis+10
elif c=='D':
y=y-dis
c='A'
dis=dis+10
elif c=='A':
x=x+dis
c='R'
dis=dis+10
print(x,y)```
```\$n=<>;
\$n=\$n*1;
\$c=0;
\$dis=10;
\$x=0;
\$y=0;
for(my \$i=0;\$i<\$n;\$i++)
{
if(\$c==0)
{
\$x=\$x+\$dis;
\$c=1;
\$dis=\$dis+10;
}
elsif(\$c==1)
{
\$y=\$y+\$dis;
\$c=2;
\$dis=\$dis+10;
}
elsif(\$c==2)
{
\$x=\$x-\$dis;
\$c=3;
\$dis=\$dis+10;
}
elsif(\$c==3)
{
\$y=\$y-\$dis;
\$c=4;
\$dis=\$dis+10;
}
elsif(\$c==4)
{
\$x=\$x+\$dis;
\$c=0;
\$dis=\$dis+10;
}
}
print \$x;
print " ";
print \$y;```

### TCS Coding Questions

All Coding Questions for TCS

Very Important

### 14 comments on “TCS Coding Question 1 | Our hoary culture had several great persons ….”

• Ashutosh

java code is wrong , And Not able to find Target..
cmperr
Main.java:20: error: illegal start of expression
public static void getDistance(int a) {
^
Main.java:20: error: illegal start of expression
public static void getDistance(int a) {
^
Main.java:20: error: ‘;’ expected
public static void getDistance(int a) {
^
Main.java:20: error: ‘.class’ expected
public static void getDistance(int a) {
^
Main.java:20: error: ‘;’ expected
public static void getDistance(int a) {
^
Main.java:65: error: reached end of file while parsing
}
^
6 errors 0
• Sagnik

I’ve have solved it in this way, quite similar to the solution but have not used menu driven functions..

import java.util.*;
public class move
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int x=0,y=0,a=10,count=0;
char ch=’s’;
while(true)
{
if(ch==’s’)
{
x=x+a;
a=a+10;
ch=’u’;
count++;
}
if(count==n)
{
break;
}
if(ch==’u’)
{
y=y+a;
a=a+10;
ch=’l’;
count++;
}
if(count==n)
{
break;
}
if(ch==’l’)
{
x=x-a;
a=a+10;
ch=’d’;
count++;
}
if(count==n)
{
break;
}
if(ch==’d’)
{
y=y-a;
a=a+10;
ch=’s’;
count++;
}
if(count==n)
{
break;
}
if(ch==’s’)
{
x=x+a;
a=a+10;
count++;
}
if(count==n)
{
break;
}
}
System.out.println(x+” “+y);
}
} 0
• Man of

#include
using namespace std;

pair astrology(int n){
pair x;
x.first=0;
x.second=0;
int flag=0;
for(int i=1;i>n;
pair pos=astrology(n);
cout<<pos.first<<" "<<pos.second<<endl;
return 0;
} 0
• Man of

yes 0
• Satya Sai Srija

i think test case for 7 as input has got wrong result given
1 -> right -> positive coordinate addition to x axis -> (0+10,0) -> (10,0)
2 -> up -> positive coordinate addition to y axis -> (10,0+20) -> (10,20 )
3 ->left -> negative coordinate addition -> (10-30,20) -> (-20,20)
4 -> down -> negative coordinate addition y axis -> (-20 , 20-40) -> (-20,-20)
5 -> right -> positive coordinate addition x axis -> (-20+50,-20) -> (30,-20)
6 -> up -> positive coordinate addition to y axid-> (30,-20+60) -> (30, 40)
7 -> left -> negative coordinate addition to x axis-> (30-70,40) -> (-40,40)
************************** my code ***************************************************
lis=[0,0]
T=int(input()) #taking input
counter=10 #increment in distance everytime
for i in range (1,T+1): #iteration loop to each time adding
c=i%4 #since we have 4 options right up left and down repeating
if c==1:
lis=lis+counter
if c==2:
lis=lis+counter
if c==3:
lis=lis-counter
if c==0:
lis=lis-counter
counter=counter+10
print(tuple(lis))

code is in python 1
• nidhi

thankyou for all the free materials …this is really helpful. 14
• Anwesha

how we get coding questions in the written exam?? 9
• HelpPrepInsta

Coding is the last section in the exam Anwesha, there will be 2 coding question, which you have to code for 8
• Nisha

Are codevita questions same for all in kolkata throughout the exam ?? 1
• HelpPrepInsta

yes Nisha the questions are same for everyone 0
• Roopa

can u plz explain this code ? 0
• Vaibhav Jain

hey Roopa, this is simple in the code, we have used a switch case, in which every time we are adding 10 more units to the total distance which he has covered, after checking that in which direction he previously was. Thus ‘R’, ‘U’, ‘L’, ‘D’, ‘A’ are the different directions on the basis of which we are adding 10 units to the total distance. I hope you get it, please comment below and let me know if there are still any doubts 6
• Rishika

Can i get an elaborated explanation for the code? 2
• Aman Varshney

To know the solutions in brief then you can just join the online courses, where we will discuss each and everything in a very segregated manner. 6