Ibm Coding Questions
Ibm Coding Questions
if (n % i == 0) {
return false;
}
}
return true;
}
int main() {
int n = 10;
if (isPrime(n))
cout << "Prime" << endl;
else
cout << "Not Prime" << endl;
return 0;
}
Ques:- Given a number x, determine whether the
given number is Armstrong Number or not.
#include <iostream>
using namespace std;
int main() {
int n = 153;
int temp = n;
int p = 0;
while (n > 0) {
int rem = n % 10;
p = (p) + (rem * rem * rem);
n = n / 10;
}
if (temp == p) {
cout<<("Yes. It is Armstrong No.");
}
else {
cout<<("No. It is not an Armstrong No.");
}
return 0;
}
Ques:- Write a program to find HCF of two numbers
without using recursion.
#include<stdio.h>
int gcd(int,int);
int main()
{
int m,n,ans;
scanf("%d",&m);
scanf("%d",&n);
while(m!=n)
{
if(m>n)
{
m=m-n;
}
else
{
n=n-m;
}
}
printf("%d",m);
return 0;
}
Ques:- Program to check if a given 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;
}
Ques:- Write a program to 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;
Ques:- Program to Check if a Given 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;
}
Ques:- 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);
}
Ques:- Write a program which concatenates
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;
}
Ques:- Program to 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;
}