w3resource

C Exercises: Check whether a given number with base b is a Niven number or not

C Numbers: Exercise-38 with Solution

Write a C programming to check whether a given number with base b (2 <= b<= 10) is a Niven number or not.
From Wikipedia,
In recreational mathematics, a harshad number (or Niven number) in a given number base, is an integer that is divisible by the sum of its digits when written in that base. Harshad numbers in base n are also known as n-harshad (or n-Niven) numbers. Harshad numbers were defined by D. R. Kaprekar, a mathematician from India. The word "harshad" comes from the Sanskrit harṣa (joy) + da (give), meaning joy-giver. The term “Niven number” arose from a paper delivered by Ivan M. Niven at a conference on number theory in 1977. All integers between zero and n are n-harshad numbers.
The number 18 is a harshad number in base 10, because the sum of the digits 1 and 8 is 9 (1 + 8 = 9), and 18 is divisible by 9 (since 18/9 = 2, and 2 is a whole number).

Test Data
Input: base 10: Number 3
Output: 3 is a Niven Number
Input: base 10: Number 18
Output: 18 is a Niven Number
Input: base 10: Number 15
Output: 15 is not a Niven Number

Sample Solution:

C Code:

#include <stdio.h>
#include <string.h>

void add(char m[],char n[],int* m_len,int n_len)
{
int i,k;
int j = 0;
for(i = 0; i< *m_len&&i<n_len; i++)
    {
        k = m[i]+n[i]+j;
m[i] = k%10;
        j = k/10;
    }
for(;i< *m_len; i++)
    {
        k = m[i]+j;
m[i] = k%10;
        j = k/10;
    }
for(;i<n_len; i++)
    {
        k = n[i]+j;
m[i] = k%10;
        j = k/10;
        (*m_len)++;
    }
if(j)
    {
m[i] = j;
        (*m_len)++;
    }
}
void mult(char n[],int m, int* len)
{
int i,j,k;
    j = 0;
for(i = 0; i< *len; i++)
    {
        k = n[i]*m+j;
        j = k/10;
n[i] = k%10;
    }
for(;j;j /= 10)
    {
n[i] = j%10;
        (*len)++;
    }
}
char div(char n[],int m, int len)
{
int i,j;
j = 0;
for(i = len-1; i>= 0; i--)
    {
        j = j % m;
j *= 10;
        j += n[i];
    }
return j % m;
}
int main()
{
int base,d,i,j;
char num[4][1024];
int len[4];
printf("Input the base and the number(separated by a space)(0 to exit):\n");
scanf("%d",&base);
while(base)
    {
getchar();
scanf("%s",num[0]);
len[1] = strlen(num[0]);
        d = 0;
for(i = 0; i<len[1]; i++)
        {
num[1][len[1]-1-i] = num[0][i]-'0';
            d += num[1][len[1]-1-i];
num[0][i] = 0;
num[2][i] = 0;
        }
num[0][0] = 1;
len[0] = 1;
len[2] = 1;
for(i = 0; i<len[1]; i++)
        {
for(j = 0; j <len[0]; j++)
            {
num[3][j] = num[0][j];
            }
len[3] = len[0];
mult(num[3],num[1][i],len+3);
add(num[2],num[3],len+2,len[3]);
mult(num[0],base,len);
        }
if(div(num[2],d,len[2]))
        {
printf("It is not a Niven number.\n");
        }
else
printf("It is a Niven number.\n");
scanf("%d",&base);
    }
return 0;
}

Sample Output:

 Input the base and the number(separated by a space)(0 to exit): 10 3 0
It is a Niven number.

Flowchart:

Flowchart: Check whether a given number with base b is a Niven number or not

C Programming Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C to check if a number is Keith or not(with explanation).
Next: C String Exercises Home

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



C Programming: Tips of the Day

Why do C and C++ compilers allow array lengths in function signatures when they're never enforced?

It is a quirk of the syntax for passing arrays to functions.

Actually it is not possible to pass an array in C. If you write syntax that looks like it should pass the array, what actually happens is that a pointer to the first element of the array is passed instead.

Since the pointer does not include any length information, the contents of your [] in the function formal parameter list are actually ignored.

Ref : https://bit.ly/3fhlvdH