TCS NQT Coding Questions with Solutions | Most Asked Questions in TCS NQT

The TCS National Qualifier Test (NQT) is an important exam for candidates seeking jobs at Tata Consultancy Services (TCS). One of the key parts of this test is the coding section, which evaluates your problem-solving and programming skills. Solve these previously asked questions before sitting for the exam!


Problem 1: String Reversal

Problem Statement:

Write a C program to reverse a given string.

Example:

Input:

hello

Output:

olleh

Solution:

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

void reverseString(char str[]) {
    int len = strlen(str);
    for (int i = 0; i < len / 2; i++) {
        char temp = str[i];
        str[i] = str[len - i - 1];
        str[len - i - 1] = temp;
    }
}

int main() {
    char str[100];
    scanf("%s", str);
    reverseString(str);
    printf("%s", str);
    return 0;
}

Problem 2: Palindrome Check

Problem Statement:

Write a C program to check if a string is a palindrome.

A palindrome is a word, number, or phrase that reads the same forward and backward. Examples include “madam,” 121, and “racecar.” If reversing it doesn’t change it, it’s a palindrome!

Example:

Input:

madam

Output:

Yes, it is a palindrome

Solution:

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

int isPalindrome(char str[]) {
    int len = strlen(str);
    for (int i = 0; i < len / 2; i++) {
        if (str[i] != str[len - i - 1]) {
            return 0;
        }
    }
    return 1;
}

int main() {
    char str[100];
    scanf("%s", str);
    if (isPalindrome(str))
        printf("Yes, it is a palindrome\n");
    else
        printf("No, it is not a palindrome\n");
    return 0;
}

Problem 3: Sorting an Array

Problem Statement:

Write a C program to sort an array of integers in ascending order.

Example:

Input:

5
4 2 9 1 5

Output:

1 2 4 5 9

Solution:

#include <stdio.h>

void sortArray(int arr[], int n) {
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}

int main() {
    int n;
    scanf("%d", &n);
    int arr[n];
    for (int i = 0; i < n; i++)
        scanf("%d", &arr[i]);
    
    sortArray(arr, n);
    
    for (int i = 0; i < n; i++)
        printf("%d ", arr[i]);
    return 0;
}

Problem 4: Prime Number Check

Problem Statement:

Write a C program to check if a number is prime.

A prime number is a number greater than 1 that has only two factors: 1 and itself. This means it cannot be divided evenly by any other number. For example, 2, 3, 5, 7, and 11 are prime numbers.

Example:

Input:

7

Output:

Prime Number

Solution:

#include <stdio.h>

int isPrime(int num) {
    if (num <= 1) return 0;
    for (int i = 2; i * i <= num; i++) {
        if (num % i == 0)
            return 0;
    }
    return 1;
}

int main() {
    int num;
    scanf("%d", &num);
    if (isPrime(num))
        printf("Prime Number\n");
    else
        printf("Not a Prime Number\n");
    return 0;
}

Here are the solutions for problems 5 to 10:


Problem 5: Fibonacci Sequence

Problem Statement:

Write a C program to generate the Fibonacci sequence up to a given number.

The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones. It starts with 0 and 1. Example: 0, 1, 1, 2, 3, 5, 8, 13…..

Example:

Input:

10

Output:

0 1 1 2 3 5 8

Solution:

#include <stdio.h>

void fibonacci(int n) {
    int a = 0, b = 1, next;
    printf("%d %d ", a, b);
    next = a + b;
    while (next <= n) {
        printf("%d ", next);
        a = b;
        b = next;
        next = a + b;
    }
}

int main() {
    int n;
    scanf("%d", &n);
    fibonacci(n);
    return 0;
}

Problem 6: Factorial Calculation

Problem Statement:

Write a C program to calculate the factorial of a given number.

Example:

Input:

5

Output:

120

Solution:

#include <stdio.h>

long long factorial(int n) {
    if (n == 0 || n == 1) return 1;
    return n * factorial(n - 1);
}

int main() {
    int n;
    scanf("%d", &n);
    printf("%lld", factorial(n));
    return 0;
}

Problem 7: Pattern Printing

Problem Statement:

Write a C program to print a pyramid pattern of stars.

Example:

Input:

5

Output:

    *    
   ***   
  *****  
 ******* 
*********

Solution:

#include <stdio.h>

void printPattern(int n) {
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n - i; j++)
            printf(" ");
        for (int k = 1; k <= (2 * i - 1); k++)
            printf("*");
        printf("\n");
    }
}

int main() {
    int n;
    scanf("%d", &n);
    printPattern(n);
    return 0;
}

Problem 8: Anagram Check

Problem Statement:

Write a C program to check if two strings are anagrams of each other.

An anagram is a word or phrase formed by rearranging the letters of another word or phrase. The letters must be used exactly once, and the words should have the same length.

Example:

Input:

listen
silent

Output:

Anagram

Solution:

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

void sortString(char str[]) {
    int len = strlen(str);
    for (int i = 0; i < len - 1; i++) {
        for (int j = i + 1; j < len; j++) {
            if (str[i] > str[j]) {
                char temp = str[i];
                str[i] = str[j];
                str[j] = temp;
            }
        }
    }
}

bool isAnagram(char str1[], char str2[]) {
    if (strlen(str1) != strlen(str2))
        return false;
    
    sortString(str1);
    sortString(str2);

    return strcmp(str1, str2) == 0;
}

int main() {
    char str1[100], str2[100];
    scanf("%s %s", str1, str2);
    if (isAnagram(str1, str2))
        printf("Anagram\n");
    else
        printf("Not an Anagram\n");
    return 0;
}

Problem 9: Matrix Transposition

Problem Statement:

Write a C program to find the transpose of a given matrix.

Example:

Input:

2 3  
1 2 3  
4 5 6  

Output:

1 4  
2 5  
3 6  

Solution:

#include <stdio.h>

void transposeMatrix(int rows, int cols, int matrix[rows][cols]) {
    for (int i = 0; i < cols; i++) {
        for (int j = 0; j < rows; j++)
            printf("%d ", matrix[j][i]);
        printf("\n");
    }
}

int main() {
    int rows, cols;
    scanf("%d %d", &rows, &cols);
    int matrix[rows][cols];

    for (int i = 0; i < rows; i++)
        for (int j = 0; j < cols; j++)
            scanf("%d", &matrix[i][j]);

    transposeMatrix(rows, cols, matrix);
    return 0;
}

Problem 10: GCD Calculation

Problem Statement:

Write a C program to calculate the greatest common divisor (GCD) of two numbers.

Example:

Input:

24 36

Output:

12

Solution:

#include <stdio.h>

int gcd(int a, int b) {
    while (b != 0) {
        int temp = b;
        b = a % b;
        a = temp;
    }
    return a;
}

int main() {
    int a, b;
    scanf("%d %d", &a, &b);
    printf("%d", gcd(a, b));
    return 0;
}

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top