TCS Coding Questions with Solutions
TCS Coding Questions : This section focuses on TCS coding questions with Answers. These programming questions are asked in previous TCS NQT Exams and will help you to prepare for upcoming TCS NQT exam.
TCS Coding Question - 1
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.
Explantion : 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 the nth 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.
// Find the Nth term in the series #include <stdio.h> #include <math.h> int main() { int n; printf("Enter the Nth term"); scanf("%d", &n); //Read from STDIN if(n % 2 == 1) { int a = 1; int r = 2; int term_in_series = (n+1)/2; int res = pow(2, term_in_series-1); printf("%d ",res); } else { int a = 1; int r = 3; int term_in_series = n/2; int res = pow(3, term_in_series-1); printf("%d ",res); } return 0; }
Output
Enter the Nth term : 8 27
TCS Coding Question - 2
Oddly Even:
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
Explantion : 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
// Find the Nth term in the series #include <stdio.h> #include <string.h> #include <stdlib.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("Difference between the sum of odd and even position digits is %d",abs(a-b)); //print the difference of odd and even return 0; }
Output
Enter the number:4567 Difference between the sum of odd and even position digits is :2
TCS Coding Question - 3
Consider the following 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.
Explantion : The value of n is a positive integer that should be read from STDIN and the nth term that is to be 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 10th 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.
// Find the Nth term in the series #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; }
Output
Enter the number:10 4
TCS Coding Question - 4
Prime Number: 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?
Explantion : A number that is divisible by only and only 1 and itself is known as a Prime Number.
For example: 11 is only divisible by 1, so 11 is prime, while 10 is divisible by 1, 2, and 5 so 10 is not a prime number.
// Prime Number #include <stdio.h> void prime(int num){ int count=0; for(int i=2;i<num;i++){ if(num%i==0){ count++; break; } } if(count==0){ printf("Prime"); } else{ printf("Not Prime"); } } int main(){ int n; printf("Enter the number: "); scanf("%d",&n); if(n>0){ prime(n); } else{ printf("negative number.Please enter a postive number"); } return 0; }
Output
Enter the number:7 Prime
TCS Coding Question - 5
SWEET SEVENTEEN:Given a maximum of four digit to the base 17 (10 - A, 11 - B, 12 - C, 13 - D .... 16 - G} as input, output its decimal value.
Case 1:
Input - 1A
Expected Output - 27
Explantion : The value A will be multiplied by 17^0 and value 1 will be multiplied by 17^1.
=> ((1*17^1) + (A*17^0))
=> ((1*17^1) + (10*17^0)) //As A equals 10
=> 17 + 10 => 27
So the output will be 27
// SWEET SEVENTEEN #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; }
Output
1A 27
Other Programming Questions
1. Program to reverse a string.
2. Program to check whether the given number is palindrome or not.
3. Program to check whether the given number is prime number or not.
4. Program to check whether the given year is leap year or not.
5. Program to print fibonacci series.
6. Program to find GCD of two numbers.
7. Program to find factorial of a number.
8. Program to check armstrong number.
9. Program to swap two numbers without using temporary variable.
10. Program to check whether the number is even or odd.