• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2022
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

4 Important Coding Problems Based on Numbers in C

R.Manoj Aiyer/2655/0
C / C++Code

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

  • if-else statement in C
  • loop statements in C

Coding Problems based on Numbers

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

Table of Contents

  • 1. Strong Number
  • 2. Armstrong Number
  • 3. Palindrome Number
  • 4. Prime Number

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!

coding challenge
R.Manoj Aiyer
I am a highly active and passionate guy who loves trending technologies and wants to help others learn and use them to solve real-life problems. I am a tech come Management guy loves blogging, programming and marketing. I’m also a Developer students club lead powered by Google developers.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Leave a Reply Cancel reply

C Programming

  1. C- Introduction
  2. C- Compile & Execute Program
  3. C- Data Types
  4. C- if-else statement
  5. C- While, do-while, for loop
  6. C- Array
  7. C- Function (Types/Call)
  8. C- strlen() vs sizeof()
  9. C- Nested Switch Statement
  10. C- Recursion
  11. C- Dynamic Programming
  12. C- Storage Classes
  13. C- Creating Header File
  14. C- Null Pointer
  15. C- Stack and Queue
  16. C- Implement Stack using Array
  17. C- Implement Linked List in C
  18. C- File Handling
  19. C- Makefile Tutorial

Object Oriented Concepts in C++

  • C++: C vs OOPs Language
  • C++: Introduction to OOPs Concepts
  • C++: Inheritance

Sorting Algorithms

  • Different Types of Sorting Algo
  • Selection Sort
  • Bubble Sort
  • Quick Sort

Programming for Practice

  1. Online C/C++ Compiler

String Handling:

  1. Remove White Spaces from String
  2. Implement strstr Function in C
  3. Convert String to Int – atoi()
  4. Check if String is Palindrome
  5. Check if Two Strings are Anagram
  6. Split String in using strtok_r()
  7. Undefined reference to strrev

Array:

  1. Check if Array is Sorted

Bit Manipulation:

  1. Count Number of 1’s in Binary

Linked List:

  1. Reverse a Linked List Elements

Number System:

  1. Program to Find 2’s Complement
  2. Convert Decimal to Binary in C

Tricky Questions:

  1. Add Two Numbers without Operator
  2. Find Next Greater Number
  3. Swap values without temp variable
  4. Print 1 to 100 without Loop

Interview Coding Questions

  • 50+ Interview Coding Questions

© 2022 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard