From 96fb1704b47abba180288274895337a76264d263 Mon Sep 17 00:00:00 2001 From: Rajalakshmi Date: Thu, 1 Oct 2020 11:11:47 +0530 Subject: [PATCH] Niven --- CODE in C/Mathematical/Niven.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 CODE in C/Mathematical/Niven.cpp diff --git a/CODE in C/Mathematical/Niven.cpp b/CODE in C/Mathematical/Niven.cpp new file mode 100644 index 0000000..7cd760e --- /dev/null +++ b/CODE in C/Mathematical/Niven.cpp @@ -0,0 +1,27 @@ +#include +using namespace std; + +// A Niven number is one in which the sum of the digits is divisible by n +bool checkNiven(int n) +{ + // calculate sum of digits + int sum = 0; + for (int temp = n; temp > 0; temp /= 10) + sum += temp % 10; + + // Return true if it is a Niven number + return (n % sum == 0); +} + +int main() +{ + int n; + cout<<"Program to check if it is a Niven number \nEnter a number : "; + cin>>n; + bool result = checkNiven(n); + if(result) + cout<<"Yes\n"; + else + cout<<"No\n"; + return 0; +} \ No newline at end of file