TCS Coding Programming Questions with Answers for Freshers

31

TCS Coding Programming Questions with Answers

This article will cover all the important and previously asked TCS coding
questions 2020 
in their exams. 

As we all know that the TCS is the world largest consultancy service company so there are
many
students who are preparing for the TCS, so you need to do something very special and work
very
hard for it.  In this article, we also cover the TCS previous question papers.

TCS coding questions are especially one section during the exam and you
must be prepared for this section to clear the TCS test and one interesting thing that you
must
need to know is that TCS will be asked repeated questions in their exams.
The recruitment process of
TCS
, the coding section is very important.

So this article will cover all the previously asked questions in
TCS 
and it will definitely very helpful for you.

Important Instruction

  • Only one question for 30
    minutes
     and 30 marks.
  • There are 6 test cases from of which 3 are shown and 3 are
    hidden.
  • Each test case carries 5 marks.
  • Languages allow for coding are C, C++, Java, Perl and Python.
  • Concerning Java, the class name should be named
    as Maze.
  • scientific calculator will be on the top
    left
     of the dashboard.

TCS Coding Question with Answer – 1

Program for Hexadecimal number to Decimal number

Write a program to input hexadecimal number from user and convert it into the Decimal
number.

Example

Input

Input hexadecimal: 1A

Output

Decimal number: 26

Solution in C


#include<stdio.h>

#include<string.h>

#include<math.h>

int main(){

    char inp[20];

    scanf(“%s”, &inp);

    int len = strlen(inp), i;

    int arr[len], j=len-1, res[len], temp=0;

    for(i=0;i<len;i++){

        if(inp[i]==’A’)

            arr[i] = 10;

        else if(inp[i]==’B’)

            arr[i] = 11;

        else if(inp[i]==’C’)

            arr[i] = 12;

        else if(inp[i]==’D’)

            arr[i] = 13;

        else if(inp[i]==’E’)

            arr[i] = 14;

        else if(inp[i]==’F’)

            arr[i] = 15;

        else

            arr[i] = inp[i] –
‘0’;

    }

    for(i=0;i<len;i++){

        res[i] = arr[i] * pow(16, j);

        j–;

    }

    for(i=0;i<len;i++){

        temp = temp + res[i];

    }

    printf(“%d”, temp);

    return 0;

}

Solution in C++


#include<iostream>

#include<string.h>

#include<math.h>

using namespace std;

int main(){

    char inp[20];

    cin>>inp;

    int len = strlen(inp);

    long arr[len], j=len-1, res[len], temp=0;

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

        if(inp[i]==’A’)

            arr[i] = 10;

        else if(inp[i]==’B’)

            arr[i] = 11;

        else if(inp[i]==’C’)

            arr[i] = 12;

        else if(inp[i]==’D’)

            arr[i] = 13;

        else if(inp[i]==’E’)

            arr[i] = 14;

        else if(inp[i]==’F’)

            arr[i] = 15;

        else

            arr[i] = inp[i] –
‘0’;

    }

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

        res[i] = arr[i] * pow(16, j);

        j–;

    }

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

        temp = temp + res[i];

    }

    cout<<temp;

    return 0;

}

Output


1A

26

TCS Coding Question with Answer – 2

Program to check whether the number is Prime or Not

Write a program to check whether the number is prime or not. Condition use function
check()
to find whether the entered number is positive or negative if negative then enter the
number, and if yes pass number 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.

To know in detail about Prime Number.

Solution in C


#include<stdio.h>

void takeInput();

void check(int);

void isPrime(int);

int main(){

takeInput();

    return 0;

}

void takeInput(){

    int num;

    scanf(“%d”,&num);

    check(num);

}

void check(int n){

    if(n < 0){

        printf(“please enter the positive
number:
“);

        takeInput();

    }

    else{

        isPrime(n);

    }

}

void isPrime(int n){

    int flag = 0, i;

    for(i=2; i<=n/2; i++){

        if(n%i == 0){

            flag = 1;

            break;

        }

    }

    if(flag == 1){

        printf(“Not Prime”);

    }

    else{

        printf(“Prime”);

    }

}

Solution in C++


#include<iostream>

using namespace std;

void takeInput();

void check(int);

void isPrime(int);

int main(){

    takeInput();

    return 0;

}

void takeInput(){

    int num;

    cin>>num;

    check(num);

}

void check(int n){

    if(n < 0){

        cout<<“please enter the positive number”;

        takeInput();

    }

    else{

        isPrime(n);

    }

}

void isPrime(int n){

    bool flag = false;

    for(int i=2; i<n/2; i++){

        if(n%i == 0){

            flag = true;

            break;

        }

    }

    if(flag == true){

        cout<<“Not Prime”;

    }

    else{

        cout<<“Prime”;

    }

}

Output

                -5
                
                please enter the positive number: 5
                
                Prime
                
                    

TCS Coding Question with Answer – 3

Generate Series such as even term and odd term creates a separate geometric series

Given a series whose even term creates a separate geometric series and odd term creates
another geometric series.

Write a program to generate such series. For example: 1, 1, 2, 3, 4, 9, 8, 27, 16, 81,
……

Solution in C

                #include<stdio.h> 
                
                void fun(int, int); 
                
                int main() 
                
                { 
                
                    int n, odd_term, even_term, i; 
                
                    scanf("%d", &n); //Enter Total Number of series 
                
                    scanf("%d", &odd_term); //Odd term series increment number 
                
                    scanf("%d", &even_term); //Even term series increment number 
                
                    int c1=1, c2=1; 
                
                    for(i=1; i<=n; i++){ 
                
                        if(i % 2 == 0){ 
                
                            //Even Position 
                
                            printf("%d ", c1); 
                
                            c1 = even_term * c1; 
                
                        } 
                
                        else{ 
                
                            //Odd Position 
                
                            printf("%d ", c2); 
                
                            c2 = odd_term * c2; 
                
                        } 
                
                    } 
                
                }
                

Solution in C++

                #include<iostream> 
                
                using namespace std; 
                
                void fun(int, int); 
                
                int main() 
                
                { 
                
                    int n, odd_term, even_term, i; 
                
                    cin>>n; //Enter Total Number of series 
                
                    cin>>odd_term; //Odd term series increment number 
                
                    cin>>even_term; // Even term series increment number 
                
                    int c1=1, c2=1; 
                
                    for(i=1; i<=n; i++){ 
                
                        if(i % 2 == 0){ 
                
                            //Even Position 
                
                            cout<<(c1)<<" "; 
                
                            c1 = even_term * c1; 
                
                        } 
                
                        else{ 
                
                            //Odd Position 
                
                            cout<<(c2)<<" "; 
                
                            c2 = odd_term * c2; 
                
                        } 
                
                    } 
                
                }
                
                

Output

                10 // Total Number of series
                
                2  // Odd term series incremental number
                
                3  // Even term series incremental number
                
                1 1 2 3 4 9 8 27 16 81
                
                

TCS Coding Question with Answer – 4

Find the Nth term in fibonacci and prime number series

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

Consider the following series:

1, 2, 1, 3, 2, 5, 3, 7, 5, 11, 8, 13, 13, 17, …

This series is a mixture of two series – all the odd terms in this series form a
Fibonacci
series and all the even terms are the prime numbers in ascending order.

The value N is 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 Nth term,
no
other characters/strings or message should be written to STDOUT.

For example, when N = 14, the 14th term in the series is 17. So only the value 17 should
be
printed to STDOUT.

Solution in C

                #include<stdio.h> 
                
                void prime(int); 
                
                void fibonacci(int); 
                
                int main() 
                
                { 
                
                    int n; 
                
                    scanf("%d", &n); 
                
                    if(n%2 == 1) 
                
                    fibonacci (n/2 + 1); 
                
                    else 
                
                    prime(n/2); 
                
                    return 0; 
                
                } 
                void prime(int n) 
                
                { 
                
                    int i, j, flag, count =0, max=1000; 
                
                    for (i=2; i<=max; i++) 
                
                    { 
                
                        flag = 0; 
                
                        for (j=2; j<i; j++) 
                
                        { 
                
                            if(i%j == 0) 
                
                            { 
                
                                flag = 1; 
                
                                break; 
                
                            } 
                
                        } 
                
                        if (flag == 0) 
                
                        { 
                
                            if(++count == n) 
                
                            { 
                
                                printf("%d",i); 
                
                                break; 
                
                            }     
                
                        } 
                
                    } 
                
                } 
                
                void fibonacci(int n) 
                
                { 
                
                    int i, t1 = 0, t2 = 1, nextTerm; 
                
                    for (i = 1; i<=n; i++) 
                
                    { 
                
                        nextTerm = t1 + t2; 
                
                        t1 = t2; 
                
                        t2 = nextTerm; 
                
                    } 
                
                    printf("%d",t1); 
                
                }
                

Solution in C++

                #include<iostream> 
                
                using namespace std; 
                
                void prime(int); 
                
                void fibonacci(int); 
                
                int main() 
                
                { 
                
                    int n; 
                
                    cin >> n; 
                
                    if(n%2 == 1) 
                
                    fibonacci (n/2 + 1); 
                
                    else 
                
                    prime(n/2); 
                
                    return 0; 
                
                }
                
                void prime(int n) 
                
                { 
                
                    int i, j, flag, count =0, max=1000; 
                
                    for (i=2; i<=max; i++) 
                
                    { 
                
                        flag = 0; 
                
                        for (j=2; j<i; j++) 
                
                        { 
                
                            if(i%j == 0) 
                
                            { 
                
                                flag = 1; 
                
                                break; 
                
                            } 
                
                        } 
                
                        if (flag == 0) 
                
                        { 
                
                            if(++count == n) 
                
                            { 
                
                                cout << i; 
                
                                break; 
                
                            }     
                
                        } 
                
                    } 
                
                } 
                
                void fibonacci(int n) 
                
                { 
                
                    int i, t1 = 0, t2 = 1, nextTerm; 
                
                    for (i = 1; i<=n; i++) 
                
                    { 
                
                        nextTerm = t1 + t2; 
                
                        t1 = t2; 
                
                        t2 = nextTerm; 
                
                    } 
                
                    cout << t1; 
                
                }
                

Output

                14
                
                17
                
                    

TCS Coding Question with Answer – 5

Program for check whether a year is leap or not

Write a program to check whether a year is leap or not.

Consider the following example:

Input: 2019

Output: Not Leap Year

Solution in C

                #include<stdio.h> 
                
                int main() 
                
                { 
                
                    int year; 
                
                    scanf("%d", &year); 
                
                    //checking for leap year 
                
                    if( ((year % 4 == 0) && (year % 100 != 0)) || (year % 400==0) ) 
                
                    { 
                
                        //input year is a leap year 
                
                        printf("Leap year"); 
                
                    } 
                
                    else 
                
                    { 
                
                        //input year is not a leap year 
                
                        printf("Not Leap year"); 
                
                    } 
                
                    return 0; 
                
                }
                
                

Solution in C++

                #include<iostream>
                
                using namespace std;
                
                int main()
                
                {
                
                    int year;
                
                    cin>>year;
                
                    
                
                    //checking for leap year
                
                    if( ((year % 4 == 0) && (year % 100 != 0)) || (year % 400==0) )
                
                    {
                
                        //input year is a leap year
                
                        cout<<"Leap year";
                
                    }
                
                    else
                
                    {
                
                        //input year is not a leap year
                
                        cout<<"Not Leap year";
                
                    }
                
                    return 0;
                
                }
                

Output

                2020
                
                Leap Year
                
                    

TCS Coding Question with Answer – 5

Two series all the odd terms in this series form even numbers in ascending order 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.

Consider the following example:

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

  • This series is a mixture of two series.
  • All the even terms in this series are derived from the previous term using the
    formula
    (x/2).
  • All the odd terms in this series are form even numbers in ascending order.


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.

Solution in C

#include<stdio.h>

int main()

{

    int x=0, y=0, i, n;

    printf(“Enter the nth number: “);

    scanf(“%d”,&n);

    

    for(i=1;i<=n;i++)

    {

        if(i%2!=0)

        {

            if(i>1)

                x = x + 2;

        }

        else

        {

            y = x / 2;

        }

    }

    if(n%2!=0)

    {

        printf(“%d”, x);

    }

    else

    { 

        printf(“%d”, y);

    }

    

    return 0;

}

Solution in C++

#include<iostream>

using namespace std;

    int main()

    {

        int x=0, y=0, n;

        cout<<“Enter the nth number: “;

        cin>>n;

        

        for(int i=1;i<=n;i++)

        {

            if(i%2!=0)

            {

                if(i>1)

                    x = x + 2;

            }

            else

            {

                y = x/2;

            }

        }

    

        if(n%2!=0)

        {

            cout<<x;

        }

        else

        { 

            cout<<y;

        }

        

        return 0;

    }

Output

                5
                
                4
                    

TCS Coding Question with Answer – 6

The program will take 3 input words one on each line from STDIN

  1. These three words will be read on three separate lines one at a time.
  1. In the first word, all vowels should be replaced by %.
  1. In the second word, all consonants should be replaced by #
  1. The third word should be converted to the upper case.
  1. Then concatenate the three words and print them without any message.

No other string/characters should or message should be written with output to STDOUT.

For example, if users give input

how are you

then the program must give output should be

h%wa#eYOU

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

Solution in C

#include <stdio.h>

#include <string.h>

int main()

{

  char a[20], b[20], c[20];

  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] != ”) {

      if (c[z] >= ‘a’ && c[z] <= ‘z’)

       {

         c[z] = c[z] – 32;

      }

      z++;

   }

   printf(“%s%s%s”, a, b, c);

}

Solution in C++

#include <iostream>

#include <string.h>

using namespace std;

int main()

{

  char a[20], b[20], c[20];

  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] != ”) {

      if (c[z] >= ‘a’ && c[z] <= ‘z’)

       {

         c[z] = c[z] – 32;

      }

      z++;

   }

   cout<<a<<b<<c;

}

LEAVE A REPLY

Please enter your comment!
Please enter your name here