Most repeated patterns are basic logic + arrays + strings + series
SECTION 1: NUMBER BASED QUESTIONS
1. Prime Number Check
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
if(n <= 1) {
cout << "Not Prime";
return 0;
}
for(int i = 2; i * i <= n; i++) {
if(n % i == 0) {
cout << "Not Prime";
return 0;
}
}
cout << "Prime";
}
2. Factorial
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
long long fact = 1;
for(int i = 1; i <= n; i++)
fact *= i;
cout << fact;
}
3. Fibonacci Series
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int a = 0, b = 1, c;
for(int i = 0; i < n; i++) {
cout << a << " ";
c = a + b;
a = b;
b = c;
}
}
(Fibonacci follows recurrence relation Fn = Fn-1 + Fn-2 (GeeksforGeeks))
4. Reverse Number
#include <iostream>
using namespace std;
int main() {
int n, rev = 0;
cin >> n;
while(n > 0) {
rev = rev * 10 + n % 10;
n /= 10;
}
cout << rev;
}
5. Palindrome Number
#include <iostream>
using namespace std;
int main() {
int n, temp, rev = 0;
cin >> n;
temp = n;
while(n > 0) {
rev = rev * 10 + n % 10;
n /= 10;
}
if(temp == rev)
cout << "Palindrome";
else
cout << "Not Palindrome";
}
6. Armstrong Number
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int n, temp, sum = 0;
cin >> n;
temp = n;
while(n > 0) {
int digit = n % 10;
sum += pow(digit, 3);
n /= 10;
}
if(sum == temp)
cout << "Armstrong";
else
cout << "Not Armstrong";
}
7. HCF (GCD)
#include <iostream>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
while(b != 0) {
int temp = b;
b = a % b;
a = temp;
}
cout << a;
}
8. LCM
#include <iostream>
using namespace std;
int gcd(int a, int b) {
while(b) {
int t = b;
b = a % b;
a = t;
}
return a;
}
int main() {
int a, b;
cin >> a >> b;
cout << (a * b) / gcd(a, b);
}
9. Leap Year
#include <iostream>
using namespace std;
int main() {
int year;
cin >> year;
if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
cout << "Leap Year";
else
cout << "Not Leap Year";
}
SECTION 2: ARRAY QUESTIONS
10. Largest Element
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int arr[n];
for(int i = 0; i < n; i++)
cin >> arr[i];
int max = arr[0];
for(int i = 1; i < n; i++)
if(arr[i] > max)
max = arr[i];
cout << max;
}
11. Second Largest
int largest = INT_MIN, second = INT_MIN;
for(int i = 0; i < n; i++) {
if(arr[i] > largest) {
second = largest;
largest = arr[i];
} else if(arr[i] > second && arr[i] != largest) {
second = arr[i];
}
}
cout << second;
12. Reverse Array
for(int i = n-1; i >= 0; i--)
cout << arr[i] << " ";
13. Sum of Array
int sum = 0;
for(int i = 0; i < n; i++)
sum += arr[i];
cout << sum;
14. Rotate Array (Right by 1)
int last = arr[n-1];
for(int i = n-1; i > 0; i--)
arr[i] = arr[i-1];
arr[0] = last;
SECTION 3: STRING QUESTIONS
15. Reverse String
#include <iostream>
using namespace std;
int main() {
string s;
cin >> s;
for(int i = [Link]()-1; i >= 0; i--)
cout << s[i];
}
16. Palindrome String
string rev = s;
reverse([Link](), [Link]());
if(s == rev)
cout << "Palindrome";
17. Count Vowels & Consonants
int v = 0, c = 0;
for(char ch : s) {
if(isalpha(ch)) {
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
v++;
else
c++;
}
}
cout << v << " " << c;
18. Remove Spaces
string result = "";
for(char ch : s)
if(ch != ' ')
result += ch;
cout << result;
19. Character Frequency
int freq[256] = {0};
for(char ch : s)
freq[ch]++;
for(int i = 0; i < 256; i++)
if(freq[i] > 0)
cout << char(i) << " " << freq[i] << endl;
20. Anagram Check
string s1, s2;
cin >> s1 >> s2;
sort([Link](), [Link]());
sort([Link](), [Link]());
if(s1 == s2)
cout << "Anagram";
else
cout << "Not Anagram";
SECTION 4: PATTERN QUESTIONS
21. Star Pyramid
*
***
*****
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n-i; j++)
cout << " ";
for(int j = 1; j <= 2*i-1; j++)
cout << "*";
cout << endl;
}
22. Inverted Pyramid
for(int i = n; i >= 1; i--) {
for(int j = 1; j <= n-i; j++)
cout << " ";
for(int j = 1; j <= 2*i-1; j++)
cout << "*";
cout << endl;
}
23. Diamond Pattern
// upper
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n-i; j++) cout<<" ";
for(int j = 1; j <= 2*i-1; j++) cout<<"*";
cout<<endl;
}
// lower
for(int i = n-1; i >= 1; i--) {
for(int j = 1; j <= n-i; j++) cout<<" ";
for(int j = 1; j <= 2*i-1; j++) cout<<"*";
cout<<endl;
}
SECTION 5: IMPORTANT PYQ/MOCK QUESTIONS
24. Push Zeros to End (Very Common)
int j = 0;
for(int i = 0; i < n; i++)
if(arr[i] != 0)
arr[j++] = arr[i];
while(j < n)
arr[j++] = 0;
(Direct TCS question (PREP INSTA))
25. Sum of Prime Numbers till N
bool isPrime(int n) {
if(n <= 1) return false;
for(int i = 2; i*i <= n; i++)
if(n % i == 0) return false;
return true;
}
int sum = 0;
for(int i = 1; i <= n; i++)
if(isPrime(i)) sum += i;
cout << sum;
26. Fibonacci + Prime Series (TCS PYQ)
// odd index -> Fibonacci
// even index -> Prime
(Asked in TCS series question (FACE Prep))
Actual TCS-style coding questions written in the exact format you’ll see in TCS NQT / Ninja / Digital
tests.
These are based on real PYQs + mock patterns, not random practice.
Each question includes:
• Problem Statement
• Input Format
• Output Format
• Sample Test Case
1. Sum of Prime Numbers
Problem Statement
Given a number N, find the sum of all prime numbers from 1 to N (inclusive).
Input Format
• An integer N
Output Format
• Print the sum of all prime numbers
Sample Input
10
Sample Output
17
C++ Code
#include <iostream>
using namespace std;
bool isPrime(int n){
if(n <= 1) return false;
for(int i = 2; i * i <= n; i++)
if(n % i == 0) return false;
return true;
}
int main(){
int n, sum = 0;
cin >> n;
for(int i = 2; i <= n; i++)
if(isPrime(i)) sum += i;
cout << sum;
}
2. Move Zeros to End
Problem Statement
Given an array of size N, move all zeros to the end while maintaining the order of non-
zero elements.
Input Format
• First line: integer N
• Second line: N space-separated integers
Output Format
• Modified array
Sample Input
5
0 1 0 3 12
Sample Output
1 3 12 0 0
C++ Code
#include <iostream>
using namespace std;
int main(){
int n;
cin >> n;
int arr[n];
for(int i = 0; i < n; i++) cin >> arr[i];
int j = 0;
for(int i = 0; i < n; i++)
if(arr[i] != 0)
arr[j++] = arr[i];
while(j < n) arr[j++] = 0;
for(int i = 0; i < n; i++)
cout << arr[i] << " ";
}
3. Check Armstrong Number
Problem Statement
Check whether a number is an Armstrong number.
Input Format
• Integer N
Output Format
• Print "Armstrong" or "Not Armstrong"
Sample Input
153
Sample Output
Armstrong
C++ Code
#include <iostream>
#include <cmath>
using namespace std;
int main(){
int n, temp, sum = 0;
cin >> n;
temp = n;
while(n > 0){
int d = n % 10;
sum += pow(d, 3);
n /= 10;
}
if(sum == temp) cout << "Armstrong";
else cout << "Not Armstrong";
}
4. Alternate Prime-Fibonacci Series (VERY IMPORTANT)
Problem Statement
Generate a series where:
• Odd positions → Fibonacci numbers
• Even positions → Prime numbers
Input Format
• Integer N
Output Format
• Print N terms
Sample Input
6
Sample Output
021315
C++ Code
#include <iostream>
using namespace std;
bool isPrime(int n){
if(n <= 1) return false;
for(int i = 2; i * i <= n; i++)
if(n % i == 0) return false;
return true;
}
int main(){
int n;
cin >> n;
int a = 0, b = 1;
int prime = 2;
for(int i = 1; i <= n; i++){
if(i % 2 != 0){ // Fibonacci
cout << a << " ";
int c = a + b;
a = b;
b = c;
}
else{ // Prime
while(!isPrime(prime)) prime++;
cout << prime << " ";
prime++;
}
}
}
5. Second Largest Element
Problem Statement
Find the second largest element in an array.
Input Format
• Integer N
• N elements
Output Format
• Second largest element
Sample Input
5
10 20 4 45 99
Sample Output
45
C++ Code
#include <iostream>
#include <climits>
using namespace std;
int main(){
int n;
cin >> n;
int arr[n];
for(int i = 0; i < n; i++) cin >> arr[i];
int largest = INT_MIN, second = INT_MIN;
for(int i = 0; i < n; i++){
if(arr[i] > largest){
second = largest;
largest = arr[i];
}
else if(arr[i] > second && arr[i] != largest){
second = arr[i];
}
}
cout << second;
}
6. Reverse Words in a String
Problem Statement
Reverse each word in a string while maintaining word order.
Input Format
• A string
Output Format
• Modified string
Sample Input
Hello World
Sample Output
olleH dlroW
C++ Code
#include <iostream>
#include <sstream>
using namespace std;
int main(){
string s, word;
getline(cin, s);
stringstream ss(s);
while(ss >> word){
for(int i = [Link]()-1; i >= 0; i--)
cout << word[i];
cout << " ";
}
}
7. Count Vowels, Consonants, Digits
Problem Statement
Count number of vowels, consonants, and digits in a string.
Input Format
• String
Output Format
• Vowels Consonants Digits
Sample Input
Hello123
Sample Output
233
C++ Code
#include <iostream>
using namespace std;
int main(){
string s;
cin >> s;
int v=0, c=0, d=0;
for(char ch : s){
if(isdigit(ch)) d++;
else if(isalpha(ch)){
ch = tolower(ch);
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u') v++;
else c++;
}
}
cout << v << " " << c << " " << d;
}
8. Check Anagram
Problem Statement
Check if two strings are anagrams.
Input Format
• String1
• String2
Output Format
• "Anagram" or "Not Anagram"
Sample Input
listen
silent
Sample Output
Anagram
C++ Code
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
string s1, s2;
cin >> s1 >> s2;
sort([Link](), [Link]());
sort([Link](), [Link]());
if(s1 == s2) cout << "Anagram";
else cout << "Not Anagram";
}
9. Replace 0 with 1
Problem Statement
Replace all 0s in a number with 1.
Input Format
• Integer N
Output Format
• Modified number
Sample Input
1020
Sample Output
1121
C++ Code
#include <iostream>
using namespace std;
int main(){
int n;
cin >> n;
int result = 0, place = 1;
while(n > 0){
int d = n % 10;
if(d == 0) d = 1;
result += d * place;
place *= 10;
n /= 10;
}
cout << result;
}
10. Missing Number in Array
Problem Statement
Find the missing number from 1 to N.
Input Format
• Integer N
• N-1 elements
Output Format
• Missing number
Sample Input
5
1235
Sample Output
4
C++ Code
#include <iostream>
using namespace std;
int main(){
int n;
cin >> n;
int arr[n-1];
for(int i = 0; i < n-1; i++)
cin >> arr[i];
int total = n*(n+1)/2;
int sum = 0;
for(int i = 0; i < n-1; i++)
sum += arr[i];
cout << total - sum;
}
11. Check Palindrome String
Problem Statement
Check if a string is a palindrome.
Input Format
• String
Output Format
• "Palindrome" or "Not Palindrome"
Sample Input
Madam
Sample Output
Palindrome
C++ Code
#include <iostream>
using namespace std;
int main() {
string s;
cin >> s;
string rev = "";
for(int i = [Link]() - 1; i >= 0; i--) {
rev += s[i];
}
if(s == rev)
cout << "Palindrome";
else
cout << "Not Palindrome";
}
12. Array Rotation (Right Shift)
Problem Statement
Rotate array to the right by 1 position.
Input Format
• Integer N
• N elements
Output Format
• Rotated array
•
Sample Input
4
1234
Sample Output
4123
C++ Code
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int arr[n];
for(int i = 0; i < n; i++)
cin >> arr[i];
int last = arr[n - 1];
for(int i = n - 1; i > 0; i--) {
arr[i] = arr[i - 1];
}
arr[0] = last;
for(int i = 0; i < n; i++)
cout << arr[i] << " ";
}
13. ASCII Sum of String
Problem Statement
Find the sum of ASCII values of all characters.
Input Format
• String
Output Format
• Integer sum
Sample Input
ABC
Sample Output
198
C++ Code
#include <iostream>
using namespace std;
int main() {
string s;
cin >> s;
int sum = 0;
for(char ch : s) {
sum += (int)ch;
}
cout << sum;
}
14. Strong Number
Problem Statement
Check if a number is a Strong number (sum of factorial of digits).
Sample Input
145
Sample Output
Strong
C++ Code
#include <iostream>
using namespace std;
int factorial(int n) {
int fact = 1;
for(int i = 1; i <= n; i++)
fact *= i;
return fact;
}
int main() {
int n, temp, sum = 0;
cin >> n;
temp = n;
while(n > 0) {
int digit = n % 10;
sum += factorial(digit);
n /= 10;
}
if(sum == temp)
cout << "Strong";
else
cout << "Not Strong";
}
15. Largest Word in String
Problem Statement
Find the longest word in a sentence.
Sample Input
I love programming
Sample Output
Programming
C++ Code
#include <iostream>
#include <sstream>
using namespace std;
int main() {
string s, word, longest = "";
getline(cin, s);
stringstream ss(s);
while(ss >> word) {
if([Link]() > [Link]()) {
longest = word;
}
}
cout << longest;
}
MOST REPEATED:
• Move zeros
• Prime/Fibonacci combo
• String manipulation
• Array rotation
• Missing number
Small TCS Tips (important)
• Use getline() for string sentences (very common mistake )
• Avoid unnecessary STL unless needed (keep it simple )
• Always handle edge cases (empty, single element, etc.)