IBM Previous Years Solved Sample Placement Papers
Find GCD or HCF of Two Numbers
#include <iostream>
using namespace std;
int gcd(int a, int b)
{
int result = min(a, b);
while (result > 0) {
if (a % result == 0 && b % result == 0) {
break;
}
result--;
}
return result;
}
int main()
{
int a = 98, b = 56;
cout << "GCD of " << a << " and " << b << " is " << gcd(a, b);
return 0;
}
Check if a Year is Leap Year
#include <bits/stdc++.h>
using namespace std;
bool checkYear(int year)
{
if (year % 400 == 0)
return true;
if (year % 100 == 0)
return false;
if (year % 4 == 0)
return true;
return false;
}
int main()
{
int year = 2000;
checkYear(year) ? cout << "Leap Year" : cout << "Not a Leap Year";
return 0;
}
Reverse Digits of a Number
#include <bits/stdc++.h>
using namespace std;
int reverseDigits(int num)
{
int rev_num = 0;
while (num > 0) {
rev_num = rev_num * 10 + num % 10;
num = num / 10;
}
return rev_num;
}
int main()
{
int num = 4562;
cout << "Reverse of no. is " << reverseDigits(num);
getchar();
return 0;
}
Check if a String is Palindrome
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = { "abbba" };
int l = 0;
int h = strlen(str) - 1;
while (h > l) {
if (str[l++] != str[h--]) {
printf("%s is not a palindrome\n", str);
return 0;
}
}
printf("%s is a palindrome\n", str);
return 0;
}
Print All Prime Numbers Less Than or Equal to N
#include <bits/stdc++.h>
using namespace std;
bool isPrime(int n)
{
if (n <= 1)
return false;
for (int i = 2; i < n; i++)
if (n % i == 0)
return false;
return true;
}
void printPrime(int n)
{
for (int i = 2; i <= n; i++)
if (isPrime(i))
cout << i << " ";
}
int main()
{
int n = 7;
printPrime(n);
}
Sum of the Digits of a Number
#include <iostream>
using namespace std;
class PrepBytes {
public:
int getSum(int n)
{
int sum;
for (sum = 0; n > 0; sum += n % 10, n /= 10)
;
return sum;
}
};
int main()
{
PrepBytes g;
int n = 687;
cout << g.getSum(n);
return 0;
}
Concatenate Two Strings in C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
char init[] = "this is init";
char add[] = " added now";
// concatenating the string.
strcat(init, add);
cout << init << endl;
return 0;
}
Find LCM of Two Numbers
#include <iostream>
using namespace std;
long long gcd(long long int a, long long int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
long long lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
int main()
{
int a = 15, b = 20;
cout << "LCM of " << a << " and " << b << " is " << lcm(a, b);
return 0;
}
Swap Two Numbers Without a Temporary Variable
#include <bits/stdc++.h>
using namespace std;
int main()
{
int x = 10, y = 5;
// Code to swap 'x' and 'y'
x = x + y; // x now becomes 15
y = x - y; // y becomes 10
x = x - y; // x becomes 5
cout << "After Swapping: x =" << x << ", y=" << y;
}
Check if a Number is Palindrome
#include <iostream>
using namespace std;
int oneDigit(int num)
{
return (num >= 0 && num < 10);
}
bool isPalUtil(int num, int* dupNum)
{
if (oneDigit(num))
return (num == (*dupNum) % 10);
if (!isPalUtil(num / 10, dupNum))
return false;
*dupNum /= 10;
return (num % 10 == (*dupNum) % 10);
}
int isPal(int num)
{
if (num < 0)
num = -num;
int* dupNum = new int(num);
return isPalUtil(num, dupNum);
}
int main()
{
int n = 12321;
isPal(n) ? cout << "Yes\n" : cout << "No" << endl;
n = 12;
isPal(n) ? cout << "Yes\n" : cout << "No" << endl;
n = 88;
isPal(n) ? cout << "Yes\n" : cout << "No" << endl;
n = 8999;
isPal(n) ? cout << "Yes\n" : cout << "No";
return 0;
}