IBM Previous Years Solved Sample Placement Papers
1. Find HCF of Two Numbers Without Using Recursion
#include <iostream>
using namespace std;
int main()
{
int m, n;
scanf("%d", &m);
scanf("%d", &n);
while (m != n)
{
if (m > n)
{
m = m - n;
}
else
{
n = n - m;
}
}
printf("%d", m);
return 0;
}
2. Properly Compress a String
import java.util.*;
public class Main {
public static String properCompression(String s) {
StringBuilder compressedStr = new StringBuilder();
for (int i = 0; i < s.length(); i += 2) {
char c = s.charAt(i);
int count = Character.getNumericValue(s.charAt(i + 1));
while (count > 0) {
compressedStr.append(c);
count--;
}
}
return compressedStr.toString();
}
public static void main(String[] args) {
String inputStr = "a3b5c2a2";
String compressedResult = properCompression(inputStr);
System.out.println(compressedResult); // Output: "aaabbbbbcc"
}
}
3. Convert Decimal Number to Binary
#include <iostream>
using namespace std;
int main()
{
int a[10], n, i;
cout << "Enter the number to convert: ";
cin >> n;
for (i = 0; n > 0; i++)
{
a[i] = n % 2;
n = n / 2;
}
cout << "Binary of the given number= ";
for (i = i - 1; i >= 0; i--)
{
cout << a[i];
}
}
4. Generate Fibonacci Triangle
#include <iostream>
using namespace std;
int main()
{
int a = 0, b = 1, i, c, n, j;
cout << "Enter the limit: ";
cin >> n;
for (i = 1; i <= n; i++)
{
a = 0;
b = 1;
cout << b << "\t";
for (j = 1; j < i; j++)
{
c = a + b;
cout << c << "\t";
a = b;
b = c;
}
cout << "\n";
}
return 0;
}
5. Output of the Program
#include <iostream>
using namespace std;
int main()
{
int a = 5, b = 10, c = 15;
int* arr[] = { &a, &b, &c };
cout << arr[1];
return 0;
}
// Output: It will print the address of variable b.
6. Output of the Program
#include <iostream>
using namespace std;
int main()
{
char arr[20];
int i;
for (i = 0; i < 10; i++)
*(arr + i) = 65 + i;
*(arr + i) = 0;
cout << arr;
return 0;
}
// Output: ABCDEFGHIJ
7. Output of the Program
#include <iostream>
using namespace std;
int main()
{
char* ptr;
char str[] = "abcdefg";
ptr = str;
ptr += 5;
cout << ptr;
return 0;
}
// Output: fg