
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Check if a number is a Krishnamurthy Number or not in C++
Here we will see how to check a number is Krishnamurty number or not. A number is Krishnamurty number, if the sum of the factorial of each digit is the same as the number. For example, if a number is 145, then sum = 1! + 4! + 5! = 1 + 24 + 120 = 145. So this is a Krishnamurty number,
The logic is simple, we have to find the factorial of each number, and find the sum, then if that is the same as a given number, the number is Krishnamurty number. Let us see the code to get a better idea.
Example
#include <iostream> #include <cmath>> using namespace std; long factorial(int n){ if(n <= 1){ return 1; } return n * factorial(n - 1); } bool isKrishnamurty(int number) { int temp = number; int sum = 0; while(number > 0){ sum += factorial(number % 10); number /= 10; } if(sum == temp){ return true; } return false; } int main() { int n = 145; if(isKrishnamurty(n)){ cout << n << " is Krishnamurty Number"; } else { cout << n << " is not Krishnamurty Number"; } }
Output
145 is Krishnamurty Number
- Related Articles
- How to Check Whether a Number is Krishnamurthy Number or Not in Java?
- Check if a number is an Unusual Number or not in C++
- Check if a number is an Achilles number or not in Python
- Check if a number is an Achilles number or not in C++
- Check if a number is jumbled or not in C++
- Swift Program to Check If a Number is Spy number or not
- Check if a number is a Pythagorean Prime or not in C++
- Check if a number is Quartan Prime or not in C++
- Check if a given number is sparse or not in C++
- Check if a number is Primorial Prime or not in Python
- Check if a number is Primorial Prime or not in C++
- Check if given number is Emirp Number or not in Python
- Check if a number is in given base or not in C++
- Check Whether a Number is a Coprime Number or Not in Java
- Check whether a number is a Fibonacci number or not JavaScript

Advertisements