TCS NQT Menu9>
- Placement Papers
- What is TCS NQT?
- How To Apply
- Foundation Section
- Aptitude Questions
- English Verbal
- Reasoning Ability
- Advanced Section
- Advanced Quantitative Ability
- Advanced Reasoning Ability
- Advanced Coding
- Coding Questions
- Syllabus 2024
- Recruitment Process
- Registration Process
- Eligibility Criteria
- How to prepare for TCS NQT?
- Interview Questions
- Hiring Process
PREPINSTA PRIME
TCS Coding Questions 2022 Day1 Slot 1
Coding Question 1 for 2022 (September slot)
In this article, we will discuss about the TCS Coding Question which is asked in the TCS placement test. This type of Coding Questions will help you to crack your upcoming TCS exam as well as during your inteview process.
TCS Coding Question Day 1 Slot 1 – Question 1
Problem Statement – An automobile company manufactures both a two wheeler (TW) and a four wheeler (FW). A company manager wants to make the production of both types of vehicle according to the given data below:
- 1st data, Total number of vehicle (two-wheeler + four-wheeler)=v
- 2nd data, Total number of wheels = W
The task is to find how many two-wheelers as well as four-wheelers need to manufacture as per the given data.
Example :
Input :
- 200 -> Value of V
- 540 -> Value of W
Output :
- TW =130 FW=70
Explanation:
130+70 = 200 vehicles
(70*4)+(130*2)= 540 wheels
Constraints :
- 2<=W
- W%2=0
- V<W
Print “INVALID INPUT” , if inputs did not meet the constraints.
The input format for testing
The candidate has to write the code to accept two positive numbers separated by a new line.
- First Input line – Accept value of V.
- Second Input line- Accept value for W.
The output format for testing
- Written program code should generate two outputs, each separated by a single space character(see the example)
- Additional messages in the output will result in the failure of test case
#include <bits/stdc++.h>
using namespace std;
int main ()
{
int v, w;
cin >> v >> w;
float x = ((4 * v) - w) / 2;
if ((w & 1) || w < 2 || w <= v)
{
cout << "INVALID INPUT";
return 0;
}
cout << "TW=" << x << " " << "FW=" << v - x;
}
import java.util.*;
public class Solution
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int v=sc.nextInt();
int w=sc.nextInt();
float res=((4*v)-w)/2;
if(w>=2 && (w%2==0) && v<w )
System.out.println("TW= "+(int)(res)+" FW= "+(int)(v-res));
else
System.out.println("INVALID INPUT");
}
}
v=int(input())
w=int(input())
if (w&1)==1 or w<2 or w<=v:
print("INVALID INPUT")
else:
x=((4*v) -w)//2
print("TW={0} FW={1}".format(x,v-x))
C#
#include
int main()
{
int a,b,v,w;
printf(“enter values for v and w :”);
scanf(“%d,%d”,&v,&w);
a=((4*v)-w)/2;
b=v-a;
printf(“No of TW :%d\nNo of FW :%d”,a,b);
return 0;
}
Kindly join our Discord channel for all the technical doubts.
SMALL & CLEAN CODE
#include
using namespace std ;
int main ()
{
int v , w;
cin >> v>>w;
if(ww )
{
cout << "INVALID INPUT"<<endl;
}
else {
int FW = (w – 2*v) / 2;
int TW= v-(FW);
cout << TW << " " << FW << endl;
}
}
v=int(input(“enter the value of v”))
w=int(input(“enter the value of w”))
if w < 2 or w <= v or w % 2!= 0:
print("invalid input")
else:
y = (w – 2 * v)//2
x=(v-y)
print(y,x)
#include
using namespace std;
#define ll long long;
int Num_wheel(int n)
{
// cout<<"Enter the number of two wheel and four wheel"<<n<>n;
int two_wheel =0;
int four_wheel =0;
cout<<"how many wheel you requrid"<<endl<<"how many four wheel you req"<>two_wheel;
cin>>four_wheel;
int data1 = 0;
int data2 = 0;
int find = two_wheel + four_wheel;
//find = data1;
int wheel = two_wheel*2 + four_wheel*4;
// wheel = data2;
cout<<find<< " value is correct"<<endl;
cout<<wheel<<" wheel is correct"<<endl;
return 0;
}
int main()
{
int n;
cout<<Num_wheel(n);
return 0;
}
v, w = map(int, input().split())
if (w % 2 != 0) or (v >= w) or (w < 2):
print("INVALID INPUT")
else:
fw = (w – 2*v) / 2
tw = v – fw
print(int(tw), int(fw))
in python
You can solve this problem using simple algebraic equations.
First, you need to check if the input values for V and W meet the constraints. If not, print "INVALID INPUT".
Then, you can use the given information to set up the following equations:
TW + FW = V (total number of vehicles)
2TW + 4FW = W (total number of wheels)
You can then use the first equation to solve for one of the variables, say TW. Substituting TW = V – FW into the second equation and solving for FW, you will get the values for TW and FW that meet the constraints.
Here is some sample Python code that implements this solution:
Note that this code assumes that the input values for v and w are integers, as per the problem statement. If the input values can be floats, you will need to make some adjustments to the code accordingly.
total_vehicles = int(input())
wheels = int(input())
tw_wheels = (wheels//2)
tw = abs(total_vehicles – tw_wheels)
fw = abs(total_vehicles – tw)
print(f”TW={tw} and FW={fw}”)
#include
using namespace std;
int main()
{
int v , w ; cin>>v>>w;
if(w<=v || w%2!=0 || w<2)
{
cout<<"INVALID_OUTPU"<<endl;
return 0;
}
int f;
f=(w-2*v)/2;
cout<<"T= "<<v-f<<" F= "<<f<<endl;
return 0;
}
//C++ Lang code
#include
using namespace std;
int main(){
int vehicle,wheels;
cout<>vehicle;
cout<>wheels;
if(vehicle>wheels || wheels%2!=0){
cout<<"invalid input";
}
else{
int two_wheelers, four_wheelers;
int a=wheels/2;
four_wheelers=a-vehicle;
two_wheelers=vehicle-four_wheelers;
cout<<"Four Wheelers "<<four_wheelers<<endl;
cout<<"Two Wheelers "<<two_wheelers<<endl;
}
}
#include
using namespace std;
int main ()
{
int v, w;
cin >> v >> w;
int sum_f=0;
int sum_t=0;
for(int i=1;i<v;i++){
sum_f=i*4;
sum_t=(v-i)*2;
if(sum_f+sum_t==w){
cout<<"TW="<<v-i<<" "<<"FW="<<i;
}
}
//cout << "TW=" << x << " " << "FW=" << v – x;
}
C Lang code
#include
int main()
{
int v,w;
//V is total of four wheeler and two wheeler
printf(“enter value of v:”);
scanf(“%d”, &v);
//W is total of wheels of four wheeler and two wheeler
printf(“enter value of w”);
scanf(“%d”, &w);
if(v>w || w%2!=0 || 2>=w)
{
printf(“you enter invalid input”);
}
else
{
printf(“you enter valid input”);
int t, f;
int a=w/2;
f=a-v;
t=v-f;
printf(“total number of four wheeler is: %d”,f);
printf(“total number of two wheeler is : %d\n”,t);
}
}
C Lang code
#include
int main()
{
int v,w;
printf(“enter value of v”);
scanf(“%d”, &v);
printf(“enter value of w”);
scanf(“%d”, &w);
if(v>w || w%2!=0 || 2>=w)
{
printf(“invalid input”);
}
else
{
printf(“valid input”);
int t, f;
int a=w/2;
f=a-v;
t=v-f;
printf(“this is fw %d”,f);
printf(“tw %d\n”,t);
}
}
#include
using namespace std;
int main()
{
int v,w;
float x,y;
cout<<"enter the number of vehicles and wheels respectively"<>v>>w;
if(w>=2 || w%2==0 || v<w){
x=2*v-(w/2);
y=(w/2)-v;
}
else{
cout<<"invalid output"<<endl;
}
cout<<"the number of 2 wheelers are: "<<x<<endl;
cout<<"the number of 4 wheelers are: "<<y<<endl;//x+2y=w/2 and x+y=v x=2v-w/2 and y=w/2-v
}