Download as pdf or txt
Download as pdf or txt
You are on page 1of 34

TCS NQT Sheet

TRAILING ZEROS

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

int fact(int n) {
if ((n==0)||(n==1))
return 1;
else
return n*fact(n-1);
}

int main() {
int n = 5;
int factorial = fact(n);
int result = 0;

while (factorial % 2 == 0)
{
result++;
factorial = factorial / 10;
}
cout<<"Trailing zeros "<<result;

return 0;
}

Check Penulty

#include <iostream>
#include <cmath>
using namespace std;

int check_penulty (int arr[], int n) {


int i;
int sum=0;
for( i=1;i<n;i++)
{
int penulty = arr[i] - arr[i-1];
sum = sum + penulty;
}
return sum;

int main() {
int arr[5], n = 3;
cout<<"Enter "<<n<<" elements"<<endl;
for(int i=0; i < n; i++)
{
cin>>arr[i];
}
cout<<"Penulty is "<<check_penulty(arr,n);

Check number is Palindrome Number or not

#include<iostream>
using namespace std;

// A function to check if n is palindrome


bool isPalindrome(int n)
{
int reverse = 0,last_digit;
int num = n;
while(num>0) {
last_digit = num % 10;
reverse = reverse*10 + last_digit;
num = num/10;
}
if(n==reverse)return true;
return false;
}

int main()
{
cout<<isPalindrome(121);
return 0;
}

Find all Palindrome Numbers in a given range

#include<iostream>
using namespace std;

// A function to check if n is palindrome


bool isPalindrome(int n)
{
int reverse = 0;
int temp = n;
while(temp>0) {
reverse = reverse*10 + temp%10;
temp = temp/10;
}

// If n and reverse are same,


// then n is palindrome
if(n==reverse)return true;
return false;
}

int main()
{
int min = 100;
int max = 150;
for(int i=min; i<=max; i++) {
if(isPalindrome(i)) {
cout<<i <<" ";
}
}
return 0;
}

C++ Program to Check Armstrong Number

#include<iostream>
using namespace std;

// A function to check if n is palindrome


bool isPalindrome(int n)
{
int sum = 0,last_digit;
int num = n;
while(num>0) {
last_digit = num % 10;
sum = sum + (last_digit * last_digit *
last_digit);
num = num/10;
}
if(n==sum)return true;
return false;
}

int main()
{
cout<<isPalindrome(153);
return 0;
}

Prime Numbers in a given range

#include <iostream>
#include <math.h>
using namespace std;
bool checkprime(int num)
{
if (num == 1)
return false;
int i = 2;
for (i = 2; i < sqrt(num); i++)
{
if (num % i == 0)
return false;
}
return true;
}
void PrintPrimesbwrange(int a, int b)
{
for (int i = a; i <= b; i++)
{
if (checkprime(i))
{
cout << i << " ";
}
}
}
int main()
{
int a = 11, b = 17;
PrintPrimesbwrange(a, b);
return 0;
}

Check whether a number is Perfect Number or not

#include <iostream>
using namespace std;

bool isPerfect(int n) {

int sum = 0;
for (int i = 1; i <= n - 1; i++) {
if (n % i == 0)
sum = sum + i;
}
if (sum == n)
return true;
else return false;
}

int main() {

if (isPerfect(6))
{
cout<<"It is a perfect number";
}
else
cout<<"It's not a perfect number";

return 0;
}

Sum of first N Natural Numbers


#include <iostream>

using namespace std;

int Sum_of_N_Natural_Numbers(int n)
{

int sum = 0;
for (int i = 1; i <= n; i++) {
sum = sum + i;

}
return sum;
}

int main()
{
int num = 10;
cout<<Sum_of_N_Natural_Numbers(num);
return 0;
}

Find Sum of AP Series


a = first term of A.P.
d= common Difference of A.P.
n= Number of Terms in A.P.
Input:
n=4
a=2
d=2
Output: 20
Explanation: 2+4+6+8 = 20

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

// Function to find sum of series.


float sumOfAP(float a, float d, int n)
{
float sum = 0;
for (int i=0;i<n;i++)
{
sum = sum + a;
a = a + d;
}
return sum;
}

// Driver function
int main()
{
int n = 20;
float a = 2.5, d = 1.5;
cout<<sumOfAP(a, d, n);
return 0;
}

Check if given year is a leap year or not


Intuition: A year is a leap year only if it satisfies the
following condition.
The year is divisible by 400
The year is divisible by 4 but not by 100
Approach: Check if the year is divisible by 4 or 400 but
not by 100 then it is a leap year.

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

int main()
{
int year;

year=2000;

if(year % 400 == 0)
cout << year << " is a Leap Year";

else if(year % 4 == 0 && year % 100 != 0)


cout << year << " is a Leap Year";

else
cout << year << " is not a Leap Year";

return 0;
}

Maximum and Minimum Digit in a Number

#include<bits/stdc++.h>
using namespace std;
void MinMax(int n)
{
int d,mini=INT_MAX,maxi=INT_MIN;
while(n!=0)
{
d=n%10;
mini = min(mini,d);
maxi=max(maxi,d);
n=n/10;
}

cout<<"The minimum number is: "<<mini<<"\n"


<<"The maximum number is: "<<maxi;
}
int main()
{
int n = 4726;
MinMax(n);
return 0;
}

Print Fibonacci Series up to Nth term

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

int fibonacci(int N){

// Base Condition.
if(N <= 1)
{
return N;
}

// Problem broken down into 2 functional calls


// and their results combined and returned.
int last = fibonacci(N-1);
int slast = fibonacci(N-2);
return last + slast;

int main(){

// Here, let’s take the value of N to be 4.


int N = 4;
cout<<fibonacci(N)<<endl;
return 0;

}
Calculate the Power of a Number : Binary Exponentiation

#include<bits/stdc++.h>
using namespace std;
int main()
{
int n = 5, k = 3;
int ans = 1;
for (int i = 0; i < k; i++) {
ans = ans * n;
}
cout <<n<<" raised to the power "<<k<<" is "<< ans;
}

Calculate the Power of a Number : Recursion


.
.
.
.
.
.
.
.

Print all Prime Factors of the given number

#include <stdio.h>
void primefactor(int num) {
printf("Prime factors of the number : ");
for (int i = 2; num > 1; i++) {
while (num % i == 0) {
printf("%d ", i);
num = num / i;
}
}
}
int main() {
int num;
num=12;
primefactor(num);
return 0;
}

Check if a number is a Strong Number or not


When the sum of factorial of individual digits of a number
is equal to the original number the number is called a
strong number.
Strong number is also known as Krishnamurthi
number/Peterson Number.
Input: N = 145
Output: Yes
Explanation: 1! + 4! + 5! = 145. Hence 145 is a strong
number.

#include<iostream>
using namespace std;
//Function to calculate the factorial of the individual
digits
int Factorial(int n) {
int fact = 1;
for (int i = 1; i <= n; i++) {
fact = fact * i;
}
return fact;
}
//Function to compute sum of factorials
int Strong_No(int num) {
int sum = 0;
//Extract all the digits from num
while (num > 0) {
int digit = num % 10;
sum = sum + Factorial(digit);
num = num / 10;
}
return sum;
}
int main() {
int number = 145;
int answer = Strong_No(number);
if (answer == number && number != 0) {
cout << "YES";
}
else {
cout << "NO";
}
}
Check if a number is Automorphic Number
Given a number, check if it is automorphic or not. A
number is called an Automorphic number if and only if its
square ends in the same digits as the number itself.
Input Format: N = 76
Result: Automorphic Number
Explanation: Calculating 76 * 76 gives 5776, it ends with
the given number.

#include <iostream>

using namespace std;

bool isAutomorphic(int N) {

int sq = N * N;

while (N > 0) {

// Check if last digit is equal or not


if (N % 10 != sq % 10)
return false;

// Reducing the number and its square


N /= 10;
sq /= 10;
}
return true;
}

int main() {
int N = 25;
if(isAutomorphic(N))
cout<<"Automorphic Number"<<endl;
else
cout<<"Not Automorphic Number"<<endl;
return 0;
}

Find GCD of two numbers


Using Euclidean’s theorem: Gcd is the greatest number
which is divided by both a and b.If a number is divided by
both a and b, it is should be divided by (a-b) and b as
well.

Approach:
Recursively call gcd(b,a%b) function till the base
condition is hit.
b==0 is the base condition.When base condition is hit
return a,as gcd(a,0) is equal to a.
#include<bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
int main()
{

int a = 4, b = 8;
cout <<"The GCD of the two numbers is "<<gcd(a, b);
}

Find LCM of two numbers


Approach:
First find gcd using euclidean’s algorithm,
Recursively call gcd(b,a%b) function till the base
condition is hit.
b==0 is the base condition.When base condition is hit
return a,as gcd(a,0) is equal to a.
Once we get gcd,we can find lcm using formula lcm =
(a*b)/gcd.

#include<bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
int main()
{
int a = 4, b = 8;
int g = gcd(a, b);
int lcm = (a * b) / g;
cout <<"The LCM of the two given numbers is "<<lcm;
}

Program to Find Roots of a quadratic equation


Input: a = 1, b = -3, c = -10
Output: Roots are real and different, i.e(5 , -2).
Approach:
Find discriminant of the equation.
Discriminant(D) = b^2 - 4a*c
If the discriminant is greater than 0, the roots are real
and different.
If the discriminant is equal to 0, the roots are real and
equal.
If the discriminant is less than 0, the roots are complex
and different.
#include <bits/stdc++.h>
using namespace std;

void Roots(int a, int b, int c)


{
int d = b * b - 4 * a * c;
double sqrt_val = sqrt(abs(d));

if (d > 0) {
cout << "Roots are real and different \n";
double root1 = (double)(-b + sqrt_val) / (2 * a);
double root2 = (double)(-b - sqrt_val) / (2 * a);
cout << root1 <<"\n"<< root2;
}
else if (d == 0) {
cout << "Roots are real and same \n";
double root1 = -(double)b / (2 * a);
double root2 = -(double)b / (2 * a);
cout << root1 <<"\n" <<root2;
}
else // d < 0
{
cout << "Roots are complex \n";
cout << -(double)b / (2 * a) << " + i" << sqrt_val
<< "\n"
<< -(double)b / (2 * a) << " - i" << sqrt_val;
}
}

int main()
{
int a = 1, b = -3, c = -10;
Roots(a, b, c);

return 0;
}

Check if the given number is Harshad(Or Niven) Number


Input: 378
Output: Yes it is a Harshad number.
Explanation: 3+7+8=18. 378 is divisible by 18. Therefore
378 is a harshad number.

Approach:
Maintain a variable sum to store sum of digits of the
number.
Now check if n is divisible by sum or not.
If it is divisible print yes,else print no.

#include<bits/stdc++.h>
using namespace std;
int main()
{
int n = 378;
int temp = n;
int sum = 0;
while (temp!=0) {
sum += temp % 10;
temp /= 10;
}
if (n % sum == 0) {
cout << "YES it is Harshad Number" << "\n";
}
else {
cout << "NO it is not Harshad Number" << "\n";
}
}

Check if the number is an abundant number or not


Example 1:
Input: 18
Output: Abundant Number
Explanation: Divisors of 18 are 1,2,3,6,9. 1+2+3+6+9=21,
Since 21 is greater than 18, 18 is an abundant number.

Example 2:
Input: 21
Output: Not Abundant Number
Explanation:Divisors of 21 are 1,3,7. 1+3+7=11, Since 11
is smaller than 21, 11 is not an abundant number.

#include<bits/stdc++.h>
using namespace std;
int main()
{
int n = 18;
int sum = 0;
for (int i = 1; i <= n; i++) {
if (n % i == 0) {
sum += i;
}
}
sum-=n;
if (sum > n) {
cout << "Abundant Number" << "\n";
}
else {
cout << "Not Abundant Number" << "\n";
}

Program to Add two fractions


.
.
.
.
.
.
.
.
.
.

Program for calculating permutation and combinations


.
.
.
.
.
.
.
.
.
.
.
.

Recursion

Print N numbers in Ascending and Descending order

#include <iostream>
using namespace std;

int print (int n)


{
if (n == 0)
{
return 0;
}
else
cout<<n<<" ";
print(n-1);
cout<<n<<" ";
}

int main()
{
cout<<print(5);

return 0;
}

Factorial of a number

#include <iostream>
using namespace std;

int factorial (int n)


{
if (n == 1 || n == 0)
return 1;
else
return n * factorial(n - 1);

int main()
{
cout<<factorial(4);

return 0;
}
Power Function using recursion

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

//Recursive Function
int power(int base, int x){

if(x==0) //Base Condition


return 1;

return (base*power(base, x-1));


}
int main(){
int base = 5, x = 3;
cout<<"Required Power is "<<power(base, x);
}
Convert digits/numbers to words

#include <iostream>
using namespace std;

char spellings[] [10] = {"zero","one", "two", "three",


"four","five","six","seven","eight","nine"};
int print(int n){
if(n==0){
return 0;
}
print(n/10);
cout<<spellings[n%10]<<" ";
}

int main()
{
int num = 123;
cout<<print(num);

}
Tower of Hanoi

Dry Run of the above illustration –

Move disc 1 from tower A to tower B.


Move disc 2 from tower A to tower C.
Move disc 1 from tower B to tower C.
Move Disc 3 from tower A to tower B.
Move Disc 1 from tower C to tower A.
Move Disc 2 from tower C to tower B.
Move Disc 1 from tower A to tower B.

Algorithm
Let us consider a recursive function that takes the
following argument N, the number of disks, to_Rod,
which indicates the rod which is moved to,
from_rod, denoting the rod from which rod is
removed, and aux_rod, denoting the rod which is
used for transferring rods from from_rod to to_rod.
The base case is for N = 1. Move it from source to
destination, i.e. from_rod to to_rod.
Solve the problem recursively by moving disk 1, 2,
3,…, N – 1 i.e. from_rod to aux_rod by recursively
calling the function on (N – 1, from_rod, aux_rod,
to_rod).
Now, since the top N – 1 disks have been removed
from from_rod, move the last disk from from_rod to
to_rod.
Similarly, again remove the top N – 1 disk from
aux_rod to to_rod and recursively call the function
on (N – 1, aux_rod, to_rod, from_rod)
Repeat the above steps until it reaches the base
case.

C++ Code for Recursive Approach


void towerOfHanoi(int n, char from_rod, char
to_rod, char aux_rod)
{
if (n == 1)
{
cout << "Move disk 1 from rod " << from_rod
<<
" to rod " <<
to_rod<<endl;
return;
}
towerOfHanoi(n - 1, from_rod, aux_rod, to_rod);
cout << "Move disk " << n << " from rod " <<
from_rod <<
" to rod " <<
to_rod << endl;
towerOfHanoi(n - 1, aux_rod, to_rod, from_rod);
}
solve(){
TowerOfHanoi(n, 'A', 'C', 'B'); }

You might also like