TCS Digital Coding Question

TCS Digital Programming Questions with Answers

TCS Digital Programming Test Questions are not like a general Coding Round Questions with Solutions it is all together different from C programming.

Here below you will find similar type programming questions that are asked constantly in TCS Digital test.

 

TCS DIgital Coding Questions and Anwers
Coding Question For
TCS Digital
Number of Questions1
Time Allotted60 Mins
Negative MarkingNo
Cut Off
Output is Necessary

TCS Digital Programming Questions

  • The languages that you can use in the test are –

  1. C
  2. C++
  3. JAVA
  4. Python
  5. Perl
  • TCS Digital Coding Questions marking Scheme

TCS Digital Recruitment Drive has a Advance Coding round. This round is a bit different from the normal coding round that is asked in TCS. There will be 1 problem statement with a total of 3-4 visible and approx 8-12 hidden test cases, instead of total of 6 test cases. 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.

TCS DIgital Coding Questions and Answers 2020
Coding Marks
Marks
Type
0 Test Case
0 Marks
NA
1 Test Case
5 Marks
Shown
2 Test Case
8 Marks
Shown
3 Test Case
12 Marks
Shown
4 Test Case
18 Marks
Shown
5 Test Case
22 Marks
Hidden
6 Test Case
25 Marks
Hidden

Difference between TCS NQT and TCS Digital Coding Round

 

TCS NQTTCS Digital
1 problem statement1 problem statement
55 mins60 mins
basic coding questionsadvance coding questions(dealing with algorithms and real word problems)

If you are new to programming than you can watch the below video and can start practicing after that, or if you are CS/IT branch student or you know the basics, move to the advance coding questions

Subscribe on YouTube

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 <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 + 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);
    }
    
    return 0;
}
#include<iostream>

using namespace std;
int main() { int i, n, a=0, b=0; cout << "enter number : "; cin >> 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) { cout << a; } else { cout << b; } return 0; }
//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);
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(b);
}
else
{
for(int i = 1 ; i < (n-2) ; i = i+2)
{
a = a + 2;
b = a / 2;
}
a = a + 2;
System.out.print(a);
}
}
}
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 <stdio.h>
#include <string.h>
int main()
{
  char a[10], b[10], c[10];
  int i,j;
  int x, y, z;

  scanf("%s",a);
  scanf("%s",b);
  scanf("%s",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] = '#';
      }
      
      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);
}
#include <iostream>
#include <string.h>

using namespace std;
int main() { char a[10], b[10], c[10]; int i,j; int x, y, z; cin >> a;
cin >> b;
cin >> 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] = '#'; } 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++; } cout << a << b << c;

return 0; }

 

import java.util.*;
public 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;
}

 

Sweet Seventeen

Problem Description

Given a maximum of four digits to the base 17(10 -> A, 11-> B,12 -> C, 16 -> G) as input, output its decimal value

  • Input 1
    • 1A
  • Expected output
    • 27
  • Input 2
    • 23 GF
  • Expected output
    • 10980

 

 

#include <stdio.h>
#include <math.h>
#include <string.h>

int main(){

  char hex[17];
  long long decimal, place;

  int i = 0, val, len;
  decimal = 0;
  place = 1;

  scanf("%s",hex);

  len = strlen(hex);
  len--;

  for(i = 0;hex[i]!='\0';i++)
  {
    if(hex[i]>='0'&& hex[i]<='9'){

      //48 to 57 are ascii values of 0 - 9
      //say value is 8 its ascii will be 56
      //val = hex[i] - 48 => 56 - 48 => val = 8

      val = hex[i] - 48;
    }
    else if(hex[i]>='a'&& hex[i]<='g'){

      //97 to 103 are ascii values of a - g
      //say value is g its ascii will be 103
      //val = hex[i] - 97 + 10 => 103 - 97 + 10=> val = 16
      //10 is added as g value is 16 not 6 or a value is 10 not 0

      val = hex[i] - 97 + 10;
    }
    else if(hex[i]>='A'&& hex[i]<='G'){

      //similarly, 65 to 71 are values of A - G
      val = hex[i] - 65 + 10;
    }

    decimal = decimal + val * pow(17,len);
    len--;
  }

  printf("%lld",decimal);

  return 0;
}
#include  <iostream>
#include  <math.h>
#include  <string.h>

using namespace std;
int main(){

  char hex[17];
  long long decimal, place;

  int i = 0, val, len;
  decimal = 0;
  place = 1;

  cin>> hex;

  len = strlen(hex);
  len--;

  for(i = 0;hex[i]!='\0';i++)
  {
    if(hex[i]>='0'&& hex[i]<='9'){

       //48 to 57 are ascii values of 0 - 9
      //say value is 8 its ascii will be 56
      //val = hex[i] - 48 => 56 - 48 => val = 8

      val = hex[i] - 48;
    }
    else if(hex[i]>='a'&& hex[i]<='g'){

       //97 to 103 are ascii values of a - g
      //say value is g its ascii will be 103
      //val = hex[i] - 97 + 10 => 103 - 97 + 10=> val = 16
      //10 is added as g value is 16 not 6 or a value is 10 not 0

      val = hex[i] - 97 + 10;
    }
    else if(hex[i]>='A'&& hex[i]<='G'){

       //similarly, 65 to 71 are values of A - G
      val = hex[i] - 65 + 10;
    }

    decimal = decimal + val * pow(17,len);
    len--;
  }

  cout<< decimal;

   return 0;
}
import java.util.*;
public class Main
{
	public static void main(String[] args) {
		HashMap<Character,Integer> hmap = new HashMap<Character,Integer>();
		hmap.put('A',10);
		hmap.put('B',11);
		hmap.put('C',12);
		hmap.put('D',13);
		hmap.put('E',14);
		hmap.put('F',15);
		hmap.put('G',16);
		hmap.put('a',10);
		hmap.put('b',11);
		hmap.put('c',12);
		hmap.put('d',13);
		hmap.put('e',14);
		hmap.put('f',15);
		hmap.put('g',16);
		Scanner sin = new Scanner(System.in);
		
                String s = sin.nextLine();
		long  num=0;
		int k=0;
		
                for(int i=s.length()-1;i>=0;i--)
		{
		    if((s.charAt(i)>='A'&&s.charAt(i)<='Z')||(s.charAt(i)>='a' &&s.charAt(i)<='z'))
		       {
		           num = num + hmap.get(s.charAt(i))*(int)Math.pow(17,k++);
		       }
		    else
		       {
		        num = num+((s.charAt(i)-'0')*(int)Math.pow(17,k++));
		}
		}
		System.out.println(num);
	}
}
'''The int() function converts the specified value into an integer number.
We are using the same int() method to convert the given input.
int() accepts two arguments, number and base.
Base is optional and the default value is 10.
In the following program we are converting to base 17'''

num = str(input())
print(int(num,17))

A Sober Walk

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;

Oddly Even

Problem Statement

Given a maximum of 100 digit numbers as input, find the difference between the sum of odd and even position digits.

Case 1

  • Input: 4567
  • Expected Output: 2

Explanation : Odd positions are 4 and 6 as they are pos: 1 and pos: 3, both have sum 10. Similarly, 5 and 7 are at even positions pos: 2 and pos: 4 with sum 12. Thus, difference is 12 – 10 = 2

Case 2

  • Input: 5476
  • Expected Output: 2

Case 3

  • Input: 9834698765123
  • Expected Output: 1

Solution

(When using Strings as input)

#include <stdio.h>
#include <stdlib.h>
#include  <string.h>

int main()
{
    int a = 0,b = 0,i = 0, n;
    char num[100];
   
    printf("Enter the number:");
    scanf("%s",num);    //get the input up to 100 digit
    n = strlen(num);
    while(n>0)
    {
        if(i==0)         //add even digits when no of digit is even and vise versa
        {
            a+=num[n-1]-48;
            n--;
            i=1;
        }
        else            //add odd digits when no of digit is even and vice versa
        {
            b+=num[n-1]-48;
            n--;
            i=0;
        }
    }
    printf("%d",abs(a-b)); //print the difference of odd and even

    return 0;
}
#include <iostream>
#include <string.h>
#include <stdlib.h>

using namespace std;

int main()
{
    int a = 0,b = 0,i = 0, n;
    char num[100];
   
    cout<< "Enter the number:";
    cin>> num;    //get the input up to 100 digit
    n = strlen(num);
    while(n>0)
    {
        if(i==0)         //add even digits when no of digit is even and vise versa
        {
            a+=num[n-1]-48;
            n--;
            i=1;
        }
        else            //add odd digits when no of digit is even and vice versa
        {
            b+=num[n-1]-48;
            n--;
            i=0;
        }
    }
    cout<< abs(a-b); //print the difference of odd and even

    return 0;
}
import java.util.*;
public class Main
{
	public static void main(String[] args) {
		Scanner sin = new Scanner(System.in);
		String s=sin.nextLine();
		
                long num = 0, num1 = 0;
		num=num + s.charAt(0)-'0';
		
                for(int i=1;i<s.length();i++)
		{
		    if(i%2==0)
		        num = num + s.charAt(i)-'0';
		   else
		     num1 = num1 + s.charAt(i)-'0';
		}
		System.out.println(Math.abs(num-num1));
	}
}
num = [int(d) for d in str(input("Enter the number:"))]
even,odd = 0,0
for i in range(0,len(num)):
    if i % 2 ==0:
        even = even + num[i]
    else:
        odd = odd + num[i]

print(abs(odd-even))

Solution (when using long long as input)

#include <stdio.h>
#include <stdlib.h>
#include  <string.h>

int main()
{
    int odd = 0,even = 0,i = 0, n,diff;
    long long num;
    scanf("%lld",&num);    //get the input up to 100 digit
    
    while(num != 0){
        if(i%2==0){
            even = even + num%10;
            num = num/10;
            i++;
        }
        else{
            odd = odd + num%10;
            num = num/10;
            i++;
        }
        
    }
    
    printf("%d",abs(odd - even));
    
    return 0;
}
#include<iostream>
#include<string.h>
#include <stdlib.h>

using namespace std;
int main()
{
    int odd = 0,even = 0,i = 0, n,diff;
    long long num;
    cin>>num;    //get the input up to 100 digit
    
    while(num != 0){
        if(i%2==0){
            even = even + num%10;
            num = num/10;
            i++;
        }
        else{
            odd = odd + num%10;
            num = num/10;
            i++;
        }
        
    }
    
    cout<< abs(odd - even);
    
    return 0;
}
num = [int(d) for d in str(input("Enter the number:"))]
even,odd = 0,0
for i in range(0,len(num)):
    if i % 2 ==0:
        even = even + num[i]
    else:
        odd = odd + num[i]

print(abs(odd-even))

Word is Key

Problem Statement

One programming language has the following keywords that cannot be used as identifiers: Break, case, continue, default, defer else, for, func, goto, if, map, range, return, struct, type, var

  • Write a program to find the given word is a keyword or not

 

Case 1

  • Input – defer
  • Expected Output – defer is a keyword

Case 2

  • Input – While
  • Expected Output – while is not a keyword
#include<stdio.h>
#include<string.h>

int main(){
    
    char str[16][10] = {"break", "case", "continue", "default", "defer", "else","for", 
    "func", "goto", "if", "map", "range", "return", "struct", "type", "var"};
    
    char input[20];
    
    int flag = 0;
    scanf("%s",input);
    
    for(int i = 0; i<16;i++){
        if(strcmp(input,str[i]) == 0){
            flag = 1;
            break;
        }
    }
    
    if(flag==1){
        printf("%s is a keyword",input);
    }
    else{
        printf("%s is not a keyword",input);
    }
    return 0;
}
#include<iostream>
#include<string.h>

using namespace std;
int main(){
    
char str[16][10] = {"break", "case", "continue", "default", "defer", "else","for", 
    "func", "goto", "if", "map", "range", "return", "struct", "type", "var"};

    
    char input[20];
    
    int flag = 0;
    cin >> input;
    
    for(int i = 0; i<16;i++){
        if(strcmp(input,str[i]) == 0){
            flag = 1;
            break;
        }
    }
    
    if(flag==1){
        cout << input << " is a keyword";
    }
    else{
        cout << input << " is not a keyword";
    }
    return 0;
}
import java.util.Scanner;
public  class Main
{
    public static void main(String args[])
    {

     String str[]= {"break", "case", "continue", "default", "defer", "else","for", 
    "func", "goto", "if", "map", "range", "return", "struct", "type", "var"};
    int flag = 0;
    Scanner sc = new Scanner(System.in);
    String input=sc.nextLine();

    for(int i = 0; i<16;i++){

        if(str[i].equals(input)){
            flag = 1;
            break;
        }
    }

    if(flag==1){
        System.out.println(input+" is a keyword");
    }
    else{
        System.out.println(input+" is not a keyword");
    }

}
}
keyword = {"break", "case", "continue", "default", "defer", "else","for", 
    "func", "goto", "if", "map", "range", "return", "struct", "type", "var"}

input_var = input()
if input_var in keyword:
    print(input_var+ " is a keyword")
else:
    print(input_var+ " is a not keyword")

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 <string.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);

}

Paid Material: Study Your TCS Digital Coding Latest Paid Material Here

TCS Digital Coding Questions and Solutions

Languages allowed – 

  1. C
  2. C++
  3. Java
  4. Perl
  5. Python

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

Paid Material

According to the latest syllabus of TCS Digital.

TCS Digital Coding Round Questions

TCS Digital Coding Round Questions in Test Pattern and Syllabus

  • Number of Questions – 1
  • Total time to Solve – 60 mins
  • Difficulty Level – Moderate to high Questions
  • Cut-off – Run more than 5 – 6 test cases

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 Digital 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 Digital 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  Digital command line Argument Type Questions and TCS Digital C Programming Questions and Answers.

TCS Digital 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 Digital 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  Digital command line Argument Type Questions and TCS Digital C Programming Questions and Answers.

TCS Digital Programming Test Facts and Questions

TCS  Digital coding test based facts and tricks, TCS Digital Programming Questions, TCS DigitalProgramming 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 Digital 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 Digital 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  // this is used to print the result using printf
#include  // 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;
  }
}

Additional Information (FAQ's)

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

The difficulty level of the TCS Programming round is medium. You should have good command over C/C++ input/output or pattern based programs.

 What languages can we use in TCS  Digital Coding Round Questions?

In TCS Digital Coding Round Questions currently you can only use C/C++ and Java for coding

How to clear the TCS Digital coding round?

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 Digital C Programming Questions and Answers

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