TCS Coding Questions with Answers

coding

TCS Programming Questions with Answers

TCS Programming Test Questions are not like a general Coding Round Questions with Solutions it is all together different from C programming. We have analysed over 100+ TCS Programming Questions. Below you will find Similar pattern based TCS Programming Round Questions, TCS Coding Questions that are asked constantly in TCS Placement test. The languages that you can use in the test are –

  • C
  • C++
  • Java
  • Python
  • Perl

If you’re looking for TCS Programming Logic questions visit this page.

Subscribe on YouTube

TCS Coding Questions marking Scheme

There will be total of 6 test cases, out of which results of 3 will be shown to you and 3 will be hidden. Test cases are basically compiler generated input which will check if your output is coming as expected. Total marks for coding section are 30 marks.

Coding MarksMarksType
0 Test Case0 marksNA
1 Test Case5 marksShown
2 Test Case10 marksShown
3 Test Case15 marksShown
4 Test Case20 marksHidden
5 Test Case25 marksHidden
6 Test Case30 marksHidden

Checking if a given year is leap year or not

Explanation:

To check whether a year is leap or not

Step 1:

  • We first divide the year by 4.
  • If it is not divisible by 4 then it is not a leap year.
  • If it is divisible by 4 leaving remainder 0 

Step 2:

  • We divide the year by 100
  • If it is not divisible by 100 then it is a leap year.
  • If it is divisible by 100 leaving remainder 0

Step 3:

  • We divide the year by 400
  • If it is not divisible by 400 then it is a leap year.
  • If it is divisible by 400 leaving remainder 0 

Then it is a leap year

 

#include<stdio.h>
int leapprog(int year)
{
//checking divisibility by 4
    if(year%4 == 0)
    {
//checking divisibility by 100
        if( year%100 == 0)
        {
//checking divisibility by 400
            if ( year%400 == 0)
                printf("%d, the year entered happens to be a leap year", year);
            else
                printf("%d is surely not a leap year", year);
        }
        else
            printf("%d, the year entered happens to be a leap year", year );
    }
    else
        printf("%d is surely not a leap year", year);
      return 0;
}
int main()
{
    int input_year, val;
    printf("Enter the year that you want to check"); //enter the year to check
    scanf("%d",&input_year);
    val = leapprog(input_year);    
return 0;
}
//C++ Program
//Leap year or not
#include<stdio.h>
using namespace std;
//main program
int main()
{
//initialising variables
int year;
cout<<"Enter year to check: ";
//user input
cin>>year;
//checking for leap year
if( ((year % 4 == 0)&&(year % 100 != 0)) || (year % 400==0) )
{
//input is a leap year
cout<<year<<" is a leap year";
}
else
{
//input is not a leap year
cout<<year<< " is not a leap year";
}
return 0;
}

Output:

Enter year to check: 2019

2019 is not a leap year

/*Java program to check whether a year entered by user is a leap year or not and a leap year is a year
which is completely divisible by 4,but the year should not be a century year except it is divisible by 400*/

import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
//scanner class declaration
Scanner sc=new Scanner(System.in);
//input year from user
System.out.println("Enter a Year");
int year = sc.nextInt();
//condition for checking year entered by user is a leap year or not
if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
System.out.println(year + " is a leap year.");
else
System.out.println(year + " is not a leap year.");
}
}
#python program to check if a year number taken from the user is a leap year or not, using nested if-else.

num = int(input("Enter the year you want to check if is leap year or not: "))

#take input year from the user to check if it is a leap year

if(num%4 == 0):

 #check if the number is divisible by 4 and if true move to next loop

   if(num%100 == 0):

     #check if the input year is divisible by 100 and if true move to next loop

       if(num%400 == 0):

           print("The year {} is a leap year".format(num))

           #the input year is divisible by 4, 100 and 400, hence leap year.

       else:

           print("The year {} is Not a leap year".format(num))

   else:

       print("The year {} is a leap year".format(num))

       #if the number is divisible by both 4 and 100 it is a leap year

else:

   print("The year {} is Not a leap year".format(num))

   #if the input num is not divisible by 4 then it can not be a leap year altogether.
# perl Script
# leap year
print "Enter Year: ";
$year=;
# condition to check for leap year
if( (0 == $year % 4) && (0 != $year % 100) || (0 == $year % 400) )
{
print "Leap year";
}
else
{
print "Not a leap year";
}
Output
Enter Year: 2016
Leap year

Prime Numbers with a Twist

Ques. Write a code to check whether no is prime or not. Condition use function check() to find whether entered no is positive or negative ,if negative then enter the no, And if yes pas no as a parameter to prime() and check whether no is prime or not?

  • Whether the number is positive or not, if it is negative then print the message “please enter the positive number”
  • It is positive then call the function prime and check whether the take positive number is prime or not.

 

#include<stdio.h>
void prime(int n)
{
int c=0;
    for(int i=2;i<n;i++)
    {
        if(n%i==0)
        c = c+1;
    }
    if(c>=1)
        printf("%d is not a prime number",n);
    else
        printf("%d is a prime number",n);
}
void main()
{
int n;
printf("Enter no : "); //enter the number
scanf("%d",&n);
if(n<0)
    {
    printf("Please enter a positive integer");
    }
else
    prime(n);
}
//C++ Program
//Prime Number
#include
using namespace std;
//function declaration
void enter();
void check(int);
void prime(int);
//main program
int main()
{
enter();
return 0;

}
//function to enter value
void enter()
{
int num;
cout<<"Enter number:";
cin>>num;
check(num);
}
//function to check whether the input is positive or negative
void check(int num)
{
if(num<0)
{
cout<<"invalid input enter value again"<<endl;
enter();
}
else
{
prime(num);
}
}
//function to check for prime number
void prime(int num)
{
int i,div=0;
for(i=1;i<=num;i++)
{
if(num%i==0)
{
div++;
}
}
//prime number only have two divisors
if(div==2)
{
cout<<num<<" is a prime number";
}
//not a prime number
else
{
cout<}

Output:

Enter number:29

29 is a prime number.
/*Java program to check whether a number entered by user is prime or not for only positive numbers,
if the number is negative then ask the user to re-enter the number*/

//Prime number is a number which is divisible by 1 and another by itself only.

import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
//input a number from user
System.out.println("Enter the number to be checked : ");
int n = sc.nextInt();
//create object of class CheckPrime
Main ob=new Main();
//calling function with value n, as parameter
ob.check(n);
}
//function for checking number is positive or negative
void check(int n)
{
if(n<0)
System.out.println("Please enter a positive integer");
else
prime(n);
}
//function for checking number is prime or not
void prime(int n)
{
int c=0;
for(int i=2;i<n;i++)
{
if(n%i==0)
++c;
}
if(c>=1)
System.out.println("Entered number is not a prime number");
else
System.out.println("Entered number is a prime number");
}
}
def prime(n):

   if n > 1:

    for i in range(2, n):

        if (n % i) == 0:

            print(n, “is not a prime number”)

            break

    else:

        print(n, “is a prime number”)

num = int(input(“enter a number: “))

if (num > 0):

    prime(num)

else:

    print(“please enter a positive number”)

#Perl Script
#Prime Number or Not
print "Enter a number";
$n=<STDIN>;
$d=0;
if($n<0)
{
print "Ivalid Input!!! Enter Value Again";
}
else
{
#Loop to find number of divisors
for($c=1;$c<=$n;$c++)
{
if($n%$c==0)
{
$d=$d+1;
}
}
#checking for prime numbers
if($d==2)
{
print "Prime Number";
}
else
{
print "Not a Prime number";
}
}

Output

Enter a number31
Prime Number

Number Series with a Twist – 1

Find the 15th term of the series?

0,0,7,6,14,12,21,18, 28

Explanation : In this series the odd term is increment of 7 {0, 7, 14, 21, 28, 35 – – – – – – }

                        And even term is a increment of 6 {0, 6, 12, 18, 24, 30 – – – – – – }

 

#include
int main()
{
   int i, n, a=0, b=0;
   printf("enter number : ");
   scanf("%d",&n);
   for(i=1;i<=n;i++)
{
      if(i%2!=0)
{
           a = a + 7;
}
       else
      {    
           b = b + 6;
}
}
    if(n%2!=0)
{
           printf("%d term of series is %d\t",n,a-7);
}
      else
  {    
printf("%d term of series is %d\t",n,b-6);
}
return 0;
}
//C++ Program
#include
using namespace std;
int main()
{
//initialising variables
int n,d;
cout<<"Enter the position: ";
//user input
cin>>n;
//logic to find nth element of the series
if(n==1||n==2)
{
cout<<0;
return 0;
}
else if(n%2==0)
{
n=n/2;
d=6;
}
else
{
n=n/2+1;
d=7;
}
//logic ends here
//printing output
cout<<(n-1)*d;
return 0;
}

Output:

Enter the position: 15
49
//Java program to find 15th element of the series
class Main
{
public static void main(String[] args)
{
int a = 7, b = 0,c;
System.out.println("Series :");
for(int i = 1 ; i < 8 ; i++)
{
c = a * b;
System.out.print(c+" "+(c-b)+" ");
b++;
}
c = a * b;
System.out.println(c);
System.out.print("15th element of the series is = "+c);
}
}

Output :

Series :
0 0 7 6 14 12 21 18 28 24 35 30 42 36 49
15th element of the series is = 49
num = int(input('enter the number: '))

a=0

b=0

for i in range(1,num+1):

    if(i%2!=0):

        a= a+7

    else:

        b = b+6

if(num%2!=0):

    print(‘ {} term of series is {}’.format(num,a-7))

else:

    print(‘{} term of series is {}’.format(num,b-6))

#perl Script
print"Enter position: ";
$n=;
$term=0;
$d=0;
if($n==0||$n==1)
{
$term=0;
}
else
{
if(0==$n%2)
{
$n=($n/2);
$d=6;
}
else
{
$n=($n/2) + 0.5;
$d=7;
}
$term= ($n-1) * $d;
}
print"$term.";

Output

Enter position: 15
49.

Number Series with a Twist 2

Link to this Question

Consider the following series: 1, 1, 2, 3, 4, 9, 8, 27, 16, 81, 32, 243, 64, 729, 128, 2187 …

This series is a mixture of 2 series – all the odd terms in this series form a geometric series and all the even terms form yet another geometric series. Write a program to find the Nth term in the series.

The value N in a positive integer that should be read from STDIN. The Nth term that is calculated by the program should be written to STDOUT. Other than value of n th term,no other character / string or message should be written to STDOUT. For example , if N=16, the 16th term in the series is 2187, so only value 2187 should be printed to STDOUT.

You can assume that N will not exceed 30.

 

#include<stdio.h>
int main()
{
   int i, n, a=1, b=1;
   printf("enter number : ");
   scanf("%d",&n);
   for(i=1;i<=n;i++)
   {
       if(i%2!=0)
       {
           a = a * 2;
       }
       else
       {    
            b = b * 3;
       }
   }
    if(n%2!=0)
       {
           printf("\n%d term of series is %d\t",n,a/2);
       }
       else
       {    
           printf("\n%d term of series is %d\t",n,b/3);
       }
return 0;
}
//C++ Program
#include
#include
using namespace std;
int main()
{
//initialising variables
int n,r,term;
cout<<"Enter the position: ";
//user input
cin>>n;
//logic to find nth element of the series
if(n==1||n==2)
{
cout<<1;
return 0;
}
else if(n%2==0)
{
n=n/2-1;
r=3;
}
else
{
n=n/2;
r=2;
}
//logic ends here
//printing output
cout<<(int)(pow(r,n));
return 0;
}

Output:

Enter the position: 17
256

Code :

//Java program to find nth element of the series
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
//input value of n
System.out.print("Enter the value of n : ");
int n = sc.nextInt();
int a = 1, b = 1;
//statement for even value of n
if(n % 2 == 0)
{
for(int i = 1 ; i <= (n-2) ; i = i+2)
{
a = a * 2;
b = b * 3;
}
System.out.print(n+" element of the series is = "+b);
}
//statement for odd value of n
else
{
for(int i = 1 ; i < (n-2) ; i = i+2)
{
a = a * 2;
b = b * 3;
}
a = a * 2;
System.out.print(n+" element of the series is = "+a);
}
}
}

Output :

Enter the value of n : 14
14 element of the series is = 729
n = int(input('enter the number: '))

a= 1

b= 1

for i in range(1, n+1):

    if(i%2!=0):

       

        a = a*2

    else:      

        b = b*3

if(n%2!=0):

    print('\n{} term of series is {}\t'.format(n,a/2))

else:

    print('\n{} term of series is {}\t'.format(n,a/2))
#Perl Script
$a=1;
$b=1;
print("enter number : ");
$n=<STDIN>;
for($i=1;$i<=$n;$i++)
{
if($i%2!=0)
{
$a = $a * 2;
}
else
{
$b = $b * 3;
}
}
if($n%2!=0)
{
print("$n term of series is ",$a/2);
}
else
{
print("$n term of series is ",$b/3);
}

Output

enter number : 17
17 term of series is 256

Number Series with a Twist 3

Link to this Question – 

Consider the below series :

0, 0, 2, 1, 4, 2, 6, 3, 8, 4, 10, 5, 12, 6, 14, 7, 16, 8

This series is a mixture of 2 series all the odd terms in this series form even numbers in ascending order and every even terms is derived from the previous  term using the formula (x/2)

Write a program to find the nth term in this series.

The value n in a positive integer that should be read from STDIN the nth term that is calculated by the program should be written to STDOUT. Other than the value of the nth term no other characters /strings or message should be written to STDOUT.

For example if n=10,the 10 th term in the series is to be derived from the 9th term in the series. The 9th term is 8 so the 10th term is (8/2)=4. Only the value 4 should be printed to STDOUT.

You can assume that the n will not exceed 20,000.

 

#include<stdio.h>
int main()
{
int i, n, a=0, b=0;
printf("enter number : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(i%2!=0)
{
if(i>1)
a = a + 2;
}
else
{
b = a/2;
}
}
if(n%2!=0)
{
printf("%d",a);
}
else
{
printf("%d",b);
}
}

or

#include<stdio.h>
int main()
{
   int i, n, a=0, b=0;
   printf("enter number : ");
   scanf("%d",&n);
   for(i=1;i<=n;i++)
   {
       if(i%2!=0)
       {
           a = a + 2;
       }
       else
       {
            b = b + 1;
       }
   }
    if(n%2!=0)
       {
           printf("%d",a-2);
       }
       else
       {    
           printf("%d",b-1);
       }
}

This series is a mixture of 2 series all the odd terms in this series form even numbers in ascending order and every even terms is derived from the previous term using the formula (x/2)

Write a program to find the nth term in this series.

//C++ Program
#include<stdio.h>
using namespace std;
//main program
int main()
{
//initialising variables
int n;
cout<<"Enter position: ";
//user input
cin>>n;
//logic to find nth term
if(n==1||n==2)
cout<<0;
else if(n%2==0)
cout<<(n-1)/2;
else
cout<<n-1;
//logic ends
return 0;
}

Output:

Enter position: 198
98
//Java program to find nth element of the series
import java.u
//Java program to find nth element of the series
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the value of n : ");
int n = sc.nextInt();
int a = 0, b = 0;
if(n % 2 == 0)
{
for(int i = 1 ; i <= (n-2) ; i = i+2)
{
a = a + 2;
b = a / 2;
}
System.out.print(n+" element of the series is = "+b);
}
else
{
for(int i = 1 ; i < (n-2) ; i = i+2)
{
a = a + 2;
b = a / 2;
}
a = a + 2;
System.out.print(n+" element of the series is = "+a);
}
}
}

Output :

Enter the value of n : 10
10 element of the series is = 4

Enter the value of n : 7
7 element of the series is = 6

n = int(input('enter the number:'))

a=0

b=0

for i in range(1,n+1):

    if(i%2!=0):

        a= a+2

    else:

        b= b+1

if(n%2!=0):

    print(‘{}’.format(a-2))

else:

    print(‘{}’.format(b-1))

String with a Twist

Link to this Questions

1. The program will recieve 3 English words inputs from STDIN

  1. These three words will be read one at a time, in three separate line
  2. The first word should be changed like all vowels should be replaced by %
  3. The second word should be changed like all consonants should be replaced by #
  4. The third word should be changed like all char should be converted to upper case
  5. Then concatenate the three words and print them

Other than these concatenated word, no other characters/string should or message should be written to STDOUT

For example if you print how are you then output should be h%wa#eYOU.

You can assume that input of each word will not exceed more than 5 chars

 
#include 
#include
int main()
{
  char a[10], b[10], c[10];
  int i,j;
  int x, y, z;
  gets(a);
  gets(b);
  gets(c);
  x = strlen(a);
  y = strlen(b);
  for(i=0;i<x;i++)
  {
      if(a[i]=='a'||a[i]=='e'||a[i]=='i'||a[i]=='o'||a[i]=='u')
      {
          a[i] = '%';
      }
  }
  for(j=0;j<y;j++)
  {
      if(b[j]=='b'||b[j]=='c'||b[j]=='d'||b[j]=='f'||b[j]=='g'||b[j]=='h'||b[j]=='j'||b[j]=='k'||b[j]=='l'||
b[j]=='m'||b[j]=='n'||b[j]=='p'||b[j]=='q'||b[j]=='r'||b[j]=='s'||b[j]=='t'||b[j]=='v'||b[j]=='w'||
b[j]=='x'||b[j]=='y'||b[j]=='z')
      {
          b[j] = '#';
      }
  }
  z=0;
   while (c[z] != '\0') {
      if (c[z] >= 'a' && c[z] <= 'z')
       {
         c[z] = c[z] - 32;
      }
      z++;
   }
   printf("%s %s %s",&a,&b,&c);
}
//C++ program
#include<iostream>
#include<string.h>
using namespace std;
//main program;
int main()
{
//initialising Strings
char ch1[5],ch2[5],ch3[5];
cout<<"Enter 1st word: ";
//user input1;
gets(ch1);
cout<<"Enter 2nd word: ";
//user input2
gets(ch2);
cout<<"Enter 3rd word: ";
//user input3
gets(ch3);
//processing first word
for(int i=0;i<strlen(ch1);i++)
{
if(ch1[i]=='a'||ch1[i]=='e'||ch1[i]=='i'||ch1[i]=='o'||
ch1[i]=='u'||ch1[i]=='A'||ch1[i]=='E'||ch1[i]=='I'||ch1[i]=='O'||
ch1[i]=='U')
ch1[i]='%';
}
//processing second word
for(int i=0;i<strlen(ch2);i++)
{
if((ch2[i]>='A'&&ch2[i]<='Z'||ch2[i]>='a'&&ch2[i]<='z' )&&!
( ch2[i]=='a' || ch2[i]=='e' || ch2[i]=='i' || ch2[i]=='o' ||
ch2[i]=='u' ||ch2[i]=='A'||ch2[i]=='E'||ch2[i]=='I'||ch2[i]=='O'||ch2[i]=='U'))
ch2[i]='#';
}
//processing third word
for(int i=0;i<strlen(ch3);i++)
{
if(ch3[i]>='a' && ch3[i]<='z')
{

ch3[i]=ch3[i]-32;
}
}
strcat(ch1,strcat(ch2,ch3));
//output
cout<<ch1;
return 0;
}
mport java.util.*;
class main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter three words : ");
String s1 = sc.next();
String s2 = sc.next();
String s3 = sc.next();
int l1 = s1.length();
int l2 = s2.length();
String str1 = "";
String str2 = "";
String str3 = "";
char c;
for(int i = 0 ; i < l1 ; i++)
{
c = s1.charAt(i);
if(c == 'A' || c == 'a' || c == 'E' ||
c == 'e' || c == 'I' || c == 'i' || c == 'O' || c == 'o' || c == 'U' || c == 'u')
str1 = str1 + "%";
else
str1 = str1 + c;
}
for(int i = 0 ; i < l2 ; i++)
{
c = s2.charAt(i);
if((c >= 'A' && c <= 'Z')||(c >= 'a' && c <= 'z'))
{
if(c == 'A' || c == 'a' || c == 'E' || c == 'e' ||
c == 'I' || c == 'i' || c == 'O' || c == 'o' || c == 'U' || c == 'u')
str2 = str2 + c;
else
str2 = str2 + "#";
}
else
str2 = str2 + c;
}
str3 = s3.toUpperCase();
System.out.println(str1+str2+str3);
}
}

Addition of two numbers a Twist

1. Using a method, pass two variables and find the sum of two numbers.

Test case:

Number 1 – 20

Number 2 – 20.38

Sum = 40.38

There were a total of 4 test cases. Once you compile 3 of them will be shown to you and 1 will be a hidden one. You have to display error message if numbers are not numeric.

 

#include<stdio.h>
addition(int x, float y)
{
    float ans;
    ans = (float)x + y;
    printf("Answer : %.2f",ans);
}
int main()
{
   int a;
   float b;
   printf("enter first number : ");
   scanf("%d",&a);
   printf("enter second number : ");
   scanf("%f",&b);
   addition(a, b);
}
//C++ Program
//Sum of two numbers using function
#include<stdio.h>
using namespace std;
//function to add two numbers
float sum(int a, float b)
{
return (float)(a+b);
}
//main program
int main()
{
//initialising variables
int a;
float b;
cout<<"Enter two numbers";
//user input
cin>>a;
cin>>b;
//call function to find sum
cout<<"Sum of "<<a<<" and "<<b<<" is "<<sum(a,b);
return 0;
}

Output

Enter two numbers 10
98.43
Sum of 10 and 98.43 is 108.43
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Number 1 : ");
int num1 = sc.nextInt();
System.out.print("Number 2 : ");
float num2 = sc.nextFloat();
float sum = num1 + num2;
System.out.println("Sum = "+sum);
}
}

Consider the below series :

0, 0, 2, 1, 4, 2, 6, 3, 8, 4, 10, 5, 12, 6, 14, 7, 16, 8

This series is a mixture of 2 series all the odd terms in this series form even numbers in ascending order and every even terms is derived from the previous  term using the formula (x/2)

Write a program to find the nth term in this series.

The value n in a positive integer that should be read from STDIN the nth term that is calculated by the program should be written to STDOUT. Other than the value of the nth term no other characters /strings or message should be written to STDOUT.

For example if n=10,the 10 th term in the series is to be derived from the 9th term in the series. The 9th term is 8 so the 10th term is (8/2)=4. Only the value 4 should be printed to STDOUT.

You can assume that the n will not exceed 20,000.

Code:

#include<stdio.h>

int main() {
//code
int n;
scanf(“%d”, &n);
if(n % 2 == 1)
{
int a = 1;
int r = 2;
int term_in_series = (n+1)/2;
int res = 2 * (term_in_series – 1);
printf(“%d “, res);
}
else
{
int a = 1;
int r = 3;
int term_in_series = n/2;

int res = term_in_series – 1;
printf(“%d “, res);
}

return 0;
}

Online Classes

  1. Free Paid Materials also given
  2. Click here to join Online Live Class

TCS Paid Material –

In Online class you will get preparation paid materials access also, but if you just want materials then you can buy the ones below –

or

  1. Click here to join Online Live Class
Minutes
Seconds

Free Materials

We have a lots of free materials don’t worry. Check them out below.

TCS Coding Section used to be based on Command Line Argument based Coding Questions it is available no where else on the internet. We will try help you with learning command line argument based coding for TCS.

From this year there is no command Line programming but you have to use C, C++, Java, Python and Perl. We have the latest set of questions for the new pattern

Question 0

Find the nth term of the series.

1,1,2,3,4,9,8,27,16,81,32,243,….

#include<stdio.h>

#include<math.h>

int three(n)

{

int x,i;

for(i=0;i<100;i++)

{

x=pow(3,i);

if(i==n)

printf("%d",x);

}

}




int two(n)

{

int x,i;

for(i=0;i<100;i++)

{

x=pow(2,i);

if(i==n)

printf("%d",x);

}

}




int main()

{

int n;

scanf("%d",&n);

if(n%2==0)

three(n/2);

else

two(n/2+1);

}

TCS Coding Questions and Solutions

Languages allowed – 

  1. C
  2. C++
  3. Command Line in C
  4. Java
  5. Perl
  6. Python

Platform will be eclipse based compiler. You can code on onlinegdb.com. All your codes working on this website would run perfectly in TCS compiler.

Non Command Line Programs

  1. TCS Coding Question – 1
  2. TCS Coding Question – 2
  3. TCS Coding Question – 3
  4. TCS Coding Question – 4
  5. TCS Coding Question – 5

Command Line Programs (Not in Syllabus anymore

Paid Material

According to the latest syllabus of TCS

TCS Coding Round Questions in Test Pattern and Syllabus

  • Number of Questions – 1
  • Total time to Solve – 20 mins
  • Difficulty Level – 1 Easy Questions
  • Cut-off – Solve 1 question completely or partial output

Though command Line Programming is not there you should study it for C MCQ section as 1-2 MCQ would be there for command Line program.

TCS C Command Line Programming Basics

  • Keywords like getc, scanf, getch, getchar etc can not be used.
  • Instead we use command line arguments to fetch values.

Before reading further for TCS Coding Round Questions, I will suggest not to freak out if you don’t understand in first go. Read everything once and then when you read again things will start to make sense. It is the best resource on the internet to know about TCS command line Argument Type Questions and TCS C Programming Questions and Answers.

TCS Programming Test Facts and Questions

TCS coding test based facts and tricks, TCS Programming Questions, TCS Programming Test –
 
TCS Coding Round TopicsDifficultyTime to solve
LCM HCFMedium20 mins
Swapping and ReversalHard20 mins
Factorial and SeriesMedium20 mins
Operations on NumbersEasy20 mins
MiscMedium – Hard20 mins

TCS Coding Questions with Answers

Let us consider this, if you wanted to write a basic C program then you would’ve written a main function that would’ve looked like in compiler for TCS Coding Round Questions – 

int Main(){

// some code

}}

However in command line arguments we write like this – 

int main(int argc, char *argv[]){

  • argc – It is known as Argument Count and as clear from the name it stores the Count of number of Arguments.
  • argv[] – Pointer, contains location of all the values(arguments).
  • *argv[] – Array of values of all the arguments.
  • They are parameters/arguments supplied to the program when it is invoked.

Thus, now we have two things

  1. Total Count of number of Arguments.
  2. All the values/pointer location of arguments stored in an array.

Now, you will need one more thing i.e. atoi();

  • atoi(); – Converts string into int and atoi(argv[i]); will give the value of argument at ith location in int type format.

Now you can use an int val = atoi(argv[i]); to store the value and then print it with printf(); function.

Check out this video Below to learn how to Solve Command Line Programming.

Quick Facts

argv[0] contains the name, not the value so –

  • All for loops must start from i=1.
  • You must use the following condition

if(argc == 1){

// do nothing since, there are no arguments, maybe ask for arguments?

}else{

// code to apply logic and print values in TCS Coding Round Questions.

}

  • provided+1 +1 for file.exe
  • argv[argc] is a NULL pointer.
  • argv[0] holds the name of the program.
  • argv[1] points to the first command line argument and argv[n] points last argument.

TCS Programming Questions and Answers

TCS Coding Questions TypeNo of times askedDifficulty
String Reversals14Very High
Number Series39Medium
Matrix based51Medium
HCF/GCD Question66Medium
Command Line Arguments here – Read this properly.
 
// Program to print all value of
// command line argument
// once we get the value from command
// line we can use them to solve our problem.
#include<stdio.h>  // this is used to print the result using printf
#include<stdlib.h>  // this is used for function atoi() for converting string into int

// argc tells the number of arguments
// char *argv[] is used to store the command line 
//arguments in the pointer to char array i.e string format
int main(int argc, char *argv[])
{
  // means only one argument   exist that is file.exe
  if (argc == 1) {
     printf("No command line argument exist Please provide them first \n");
    return 0;
    } 

  else {
    int i;
    // actual arguments starts from index 1 to (argc-1)
    for (i = 1; i < argc; i++) {
      int value = atoi(argv[i]);
      // print value using stdio.h library's printf() function
      printf("%d\n", value);
    }
  return 0;
  }
}

TCS Programming Test Based FAQ’s

Ques. What is the level of difficulty of the question asked in TCS programming round?

Ans. They are easy but you need to have good grip over basic C/C++ input/output or pattern based programs.

Logics like Recursion, For loops, If loops etc

Ques. What languages can we use in TCS Coding Round Questions?

Ans. In TCS Coding Round Questions currently you can only use C/C++ and Java for coding in TCS based Compiler that is mostly GCC type of compiler.

Ques. Is PrepInsta enough to prepare for TCS Coding Round and Questions asked in the exams?

Ans. Yes, it is the best resource out there in the internet to prepare for TCS Coding section paper.

Rules for TCS Coding Round Questions Section:

  • There is only one question for 20 minutes.
  • It has 10 attempts(We can compile only 10 times).
  • We must start our code from the scratch.
  • The coding platform is divided into two, one for writing the code and other for output. We should write the whole
    program.
  • We must only print exact output.
  • Output must not be re-framed by extra words.
  • If there is any error, the error will be shown in the output dialog box.
  • The errors are clearly mentioned.
  • If there are no errors, a message like “compiled successfully” will be printed.
  • Along with that they will mention four test cases are ‘passed’ or ‘failed’. They are indicated like private and public test cases. They have not mentioned what is the test case, which is difficult to understand.

Don’t Compile again and again since compiler takes 25 seconds and each time you compile 25 seconds will become lesser in the time you have to code in TCS Placement Papers Programming.

Ques. How to clear the TCS coding round?

Ans. First you must learn command line programming from our coding dashboard and then try sample questions given on the dashboard to know how to clear the tcs coding round  and TCS C Programming Questions and Answers

Ques. What are some other websites where we can find more questions?

Ans. You can find more questions on MyGeekMonkey website

Disclaimer: The Words Like “Placement Papers” and “Previous Year Papers” are used here for Google Search Purposes only and may not be. All these questions could be freely available on the internet, we are only charging students for the PrepInsta’s Mock Test experiences and Analytics as well as preparation for the exam. Prepinsta does not guarantee any recurrence of the questions in the exam however we believe that from our practise questions the exam should atleast be similar in pattern as per syllabus or difficulty. These are only practise mock questions. PrepInsta has compiled these from various internet sources and made them as per mock experience for students ease and are offering analytics as per performance
Summary
Review Date
Reviewed Item
TCS Coding Questions
Author Rating
51star1star1star1star1star

Please Login/Signup to comment

34 comments on “TCS Coding Questions with Answers”


  • Aashutosh

    n=input(‘enter the string’)
    p=n.split()
    l=[]
    s=len(p)
    for x in p[0]:
    if x in ‘aeiouAEIOU’:
    p[0]=p[0].replace(x,’$’)
    for x in p[1]:
    if x not in ‘aeiou’:
    p[1]=p[1].replace(x,’#’)
    p[2]=p[2].upper()
    l=”.join(p)
    print(l)


  • Abdul

    #include
    #include
    int main()
    {
    char str1[20], str2[20], str3[20];
    int i;
    gets(str1);
    gets(str2);
    gets(str3);
    for(i=0;str1[i]!=’\0′;i++)
    {
    if(str1[i]==’a’ || str1[i]==’e’ || str1[i]==’i’ || str1[i]==’o’ || str1[i]==’u’ ||
    str1[i]==’A’ || str1[i]==’E’ || str1[i]==’I’ || str1[i]==’O’ || str1[i]==’U’)
    {
    str1[i] = ‘*’;
    }
    }
    for(i=0;str2[i]!=’\0′;i++)
    {
    if(str2[i]==’a’ || str2[i]==’e’ || str2[i]==’i’ || str2[i]==’o’ || str2[i]==’u’ ||
    str2[i]==’A’ || str2[i]==’E’ || str2[i]==’I’ || str2[i]==’O’ || str2[i]==’U’)
    continue;
    else{
    str2[i] = ‘@’;
    }
    }
    for(i=0;str3[i]!=’\0′;i++)
    {
    if(str3[i]>=’a’ && str3[i]<='z')
    str3[i] -= 32;
    }
    strcat(str1,str2);
    strcat(str1,str3);
    printf("%s",str1);
    return 0;
    }


  • anusha anusha

    s1,s2,s3=map(str,input().split(” “))
    for i in s1:
    if i in “aeiou”:
    s1=s1.replace(i,’%’)
    for j in s2:
    if j in “bcdfghjklmnpqrstvwxyz”:
    s2=s2.replace(j,’#’)
    s3=s3.upper()
    print(s1+””+s2+””+s3)


    • Prep Insta

      The difficulty is going to be slight higher this year as digital selection also is via the same exam


  • Bhanu A

    Thanks sir the coding question given on the page was repeated for me in the exam thanks so much prepinsta


  • Aritra Dutta

    Shortest possible code for String with a twist python

    # how are you
    # h%wa#eYOU

    s1=input()
    s2=input()
    s3=input()

    for elem in s1:
    if elem in (‘a’,’e’,’i’,’o’,’u’):
    s1=s1.replace(elem,’%’)
    for elem in s2:
    if elem not in (‘a’,’e’,’i’,’o’,’u’):
    s2=s2.replace(elem,’#’)
    s3=s3.upper()
    print(s1+” “+s2+” “+s3)


    • Mohd Saif

      s1,s2,s3=map(str,input().split(” “))
      for i in s1:
      if i in “aeiou”:
      s1=s1.replace(i,%)
      for j in s2:
      if j in “”bcdfghjklmnpqrstvwxyz”:
      s2=s2.replace(j,#)
      s3=s3.upper()
      print(s1+””+s2+””+s3)