Mathematics is used everywhere ranging from application development to solving complex problems in business.
When you are looking for a job, many companies not only test your programming skills but also your ability to solve mathematical problems using programming logic.
In this blog, we have discussed some of the very popular programming questions such as Armstrong Number, Strong Number, Palindrome Number, and Prime Number.
Prerequisites
Each problem is explained with the example, algorithm and code.
Table of Contents
What is a Strong Number?
If the sum of the factorials of individual digits of a given number is equal to the same number then it is known as the Strong number.
Example
n= 145 = 1! +4!+5! = 1 + 24 + 120 = 145 145 = 1! + 4! + 5! Therefore 145 is a Strong number
Algorithm
Write a program to check if the given number is strong or not.
#include<stdio.h>
int main()
{
int n, temp, r, fact, sum=0;
scanf("%d",&n);
temp=n;
while(n>0)
{
r = n%10;
fact = 1;
for(i=r; i>=1; i--)
{
fact = fact*i;
}
sum = sum+fact;
n = n/10;
}
n = temp;
if(n == sum)
printf("%d is a strong number.", n);
else
printf("%d is not a strong number.", n);
return 0;
}
Output:
Input 145 Output 145 is a strong number.
What is an Armstrong Number?
Armstrong number is a number which is equal to the sum of it’s individual digits raised to the power of 3.
Example
n = 153 =1^3 + 5^3 + 3^3 =3+125+27 =153 153 is a Armstrong number
Algorithm
Write a program to find if the given number is Armstrong number or not.
#include<stdio.h>
int main()
{
int n, r, c, sum=0;
scanf("%d",&n);
int temp=n;
while(n>0)
{
r = n%10;
c = r**R;
sum = sum+c;
n = n/10;
}
n = temp;
if(n == sum)
printf("%d is a Armstrong Number.",n);
else
printf("%d is not an Armstrong Number.",n);
return 0;
}
Output
Input 153 Output 153 is a Armstrong Number.
What is a Palindrome Number?
A palindrome number is a number that remains the same even if its digits are reversed.
Example
5225 is a Palindrome Number.
Algorithm
Write a program to check if the given number is a palindrome number or not.
#include<stdio.h>
int main()
{
int n, temp, r, sum=0;
scanf("%d",&n);
temp = n;
while(n>0)
{
r = n%10;
sum = sum*10+r;
n = n/10;
}
n = temp;
if(n == sum)
printf("%d is a palindrome number.",n);
else
printf("%d is not a palindrome number",n);
return 0;
}
Output
Input 223 Output 223 is not a palindrome number.
This coding problem was also asked in Adobe interview.
Sometimes in an interview, you can also be asked to check if the given string is palindrome or not, instead of a number.
What is a Prime Number?
Prime Number is a number that is greater than and has only two factors 1 and the number itself.
Algorithm
Write a program to identify if the given number is prime number or not.
#include<stdio.h>
int main()
{
int n ,count =0;
scanf("%d",&n);
for(int i=1; i<=n; i++)
{
if(n%i == 0)
count++;
}
if(count == 2)
printf("%d is a prime number",n);
else
printf("%d is not a prime number",n);
return 0;
}
Note: You can also optimize more by iterating the loop till n/2
and by changing the condition as count==1
.
Output
Input 7 Output 7 is a prime number
You can also solve this questions in any programming languages of your choice like C/C++, Java or Python. The logic will be the same.
These are some of the basic programming questions usually asked in the interviews. You can also practice solving coding questions asked in interviews.
If you have any doubts about coding problems based on numbers that we have discussed in this tutorial, let me know in the comment. Happy Programming!