Skip to content

p063.c

Problem Statement

Write a C program to find all Niven in an array. Define a user-defined function int isNiven(int num) that returns 1 if the numbers is a Niven number, otherwise returns 0. A Niven number (also known as a Harshad Number) is an integer that is divisible by the sum of its digits.

Metadata

Property Detail
Author Amit Dutta amitdutta4255@gmail.com
Date 16 Dec 2025
License MIT License (See the LICENSE file for details)

Actions

Raw View on GitHub

💡 You can print or save this file by opening Raw and using your browser.

Source Code

#include <stdio.h>
#include <stdlib.h>

void inputarr(int[], int);
int isNiven(int);

int main()
{
    int n, *arr, i;
    printf("How many element do you want to enter: ");
    scanf("%d", &n);
    arr = (int *)malloc(n * sizeof(int));
    inputarr(arr, n);
    for (i = 0; i < n; i++)
    {
        if (isNiven(arr[i]))
        {
            printf("\n%d is a Niven number.", arr[i]);
        }
        else
        {
            printf("\n%d is a NOT Niven number.", arr[i]);
        }
    }
    free(arr);
    return 0;
}

void inputarr(int arr[], int n)
{
    int i;
    for (i = 0; i < n; i++)
    {
        printf("Enter Element %d: ", i + 1);
        scanf("%d", &arr[i]);
    }
}

int isNiven(int num)
{
    int i, sum = 0, temp = num;
    while (temp > 0)
    {
        sum += temp % 10;
        temp /= 10;
    }
    return num % sum == 0;
}