4 Important Coding Problems Based on Numbers in C

4 Important Coding Problems Based on Numbers in C

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

Coding Problems based on Numbers

Each problem is explained with the example, algorithm and code.

1. Strong Number

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 

  1. Start the program
  2. Get the input as a number
  3. Calculate the factorial of each digit in the number.
  4. Sum up the factorials of Individual digit.
  5. Compare the sum of the Factorials and the given input. If they are equal, it’s a strong number otherwise it’s not a strong number.
  6. Print the result.
  7. Stop the program.

C/C++ Program for Strong Number

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.

2. Armstrong 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

  1. Start the Program
  2. Get the input as a Number
  3. Calculate the cube of each digit of the given input
  4. Sum up the cube of each digit
  5. If the sum is equal to the given number it is an Armstrong number else it is not
  6. Print the result
  7. Stop the Program

C/C++ Program for Armstrong Number

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.

3. Palindrome 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

  1. Start the Program
  2. Get the input as a number
  3. Reverse the number
  4. Calculate the sum of digits
  5. Compare this sum with the given input. If it is equal its a palindrome number else it’s not
  6. Print the result
  7. Stop the Program

C/C++ Program for Palindrome Number

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.

4. Prime 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

  1. Start the program
  2. Get the input as a number
  3. Use a Loop to check the number of factors the number has
  4. If it has only 2 factors 1 and itself then its a prime number else it’s not a prime number
  5. Print the result
  6. Stop the Program

C/C++ Program for Prime Number

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!

Leave a Reply

Your email address will not be published. Required fields are marked *