Join Regular Classroom : Visit ClassroomTech

TCS Digital Sample Question Paper | Coding Questions

←← Previous Page

TCS Digital Coding Questions:

Question 1:

Write a program to find the count of numbers that consists of unique digits.
Input:
Input consists of two Integer lower and upper value of a range
Output:
The output consists of a single line, print the count of unique digits in a given range. Else Print”No Unique Number
.

Solution:

//codewindow.in
#include<bits/stdc++.h>

using namespace std;

void printUnique(int l, int r)
{
int count=0;

for (int i=l ; i<=r ; i++)
{
int num = i;
bool visited[10] = {false};
    
while (num)
{
if (visited[num % 10])
break;
            
visited[num%10] = true;
num = num/10;
}
        
if (num == 0)
count++;
}
    
if(count>0)
cout<<count;
else
cout<<"No Unique Number";
}
    
int main()
{
int l,r;
cin>>l>>r;
printUnique(l, r);
return 0;
}
//end

Question 2:

There is a range given n and m in which we have to find the count all the prime pairs whose difference is 6. We have to find how many sets are there within a given range.
Output:
The output consists of a single line, print the count prime pairs in a given range. Else print”No Prime Pairs”.

Constraints:
2 ≤ n ≤ 1000
n ≤ m ≤ 2000
Sample Input:
4
30
Sample Output:
6
Explanation:
(5, 11) (7, 13) (11, 17) (13, 19) (17, 23) (23, 29) . we have 6 prime pairs.

Solution:

//codewindow.in
#include <bits/stdc++.h>
using namespace std;

void count_prime(int l, int r)
{
int count=0;
bool prime[r + 1];
memset(prime, true, sizeof(prime));

for (int p = 2; p * p <= r; p++) 
{
if (prime[p] == true) 
 {
  for (int i = p * 2; i <= r; i += p)
  prime[i] = false;
 }
}
for (int i = l; i <= r - 6; i++)
 if (prime[i] && prime[i + 6])
    
count++;

if(count>0)
cout<<count;
else
cout<<"No Prime Pairs";
}

int main()
{
int n,m;
cin>>n>>m;
count_prime(n, m);
return 0;
}
//end

Question 3:

Write a program to print all the combinations of the given word with or without meaning (when unique characters are given).
Sample Input:
abc
Sample Output:
abc
acb
bac
bca
cba
cab

Solution:

//codewindow.in
#include<bits/stdc++.h>
using namespace std;

void permute(string a, int l, int r)
{
if (l == r)
 cout<<a<<endl;
else
{
for (int i = l; i <= r; i++)
{
 swap(a[l], a[i]);
 permute(a, l+1, r);swap(a[l], a[i]);
 }
}
}

int main()
{
string str;
cin>>str;
int n = str.size();
permute(str, 0, n-1);
return 0;
}
//end

Question 4:

Bastin once had trouble finding the numbers in a string. The numbers are distributed in a string across various test cases. There are various numbers in each test case you need to find the number in each test case. Each test case has various numbers in sequence. You need to find only those numbers which do not contain 9. For eg, if the string contains “hello this is alpha 5051 and 9475”.You will extract 5051 and not 9475. You need only those numbers which are consecutive and you need to help him find the numbers. Print the largest number.
Note: Use long long for storing the numbers from the string.

Input Format :
The first line consists of T test cases and next T lines contain a string.
Output Format:
For each string output the number stored in that string if various numbers are there print the largest one. If a string has no numbers print -1.

Constraints:
1 ≤
T ≤ 100
1 ≤
|S| ≤ 10000

Sample Input:
1
This is alpha 5057 and 97

Sample Output:
5057

Solution:

//codewindow.in
#include<bits/stdc++.h>
using namespace std;

int main()
{
int t;
cin >> t;
cin.ignore();

while(t--)
{
string s;
getline(cin, s);
int n = s.length();
int n9 = 0;
string res="", num = "";
    
for(int i = 0; i < n; i++)
{
n9 = 0;
num = "";
        
while(s[i] >= '0' && s[i] <= '9')
{
if(s[i] == '9')
n9 = 1;
num = num + s[i];
i++;
}
        
if(!n9 && num != "")
{
long long a = stoll(num);
long long b = -1;
if(res != "")
    b = stoll(res);
if(a> b)
    res = num;
}
}
    
if(res == "")
    cout << "-1";
else
    cout <<res << endl;
}

return 0
//end

←← Previous Page


Follow Us

Also Checkout