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)
Difficulty Intermediate (index: 4 / 10)

Concepts

Beta Feature

This concept detection system is still in beta and may occasionally show incorrect or incomplete results.

  • Array
  • Pointers
  • Iteration
  • Sorting (possible)
  • Recursion

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;
}

Explanation

Explain with AI

Copy the prompt below and paste it into any AI assistant.

    You are explaining a C programming code to a beginner.

    STRICT RULES:

    - Only use the given code. Do NOT assume anything not present.

    - Do NOT add extra examples.

    - Keep explanation clear and short.

    - If something is unclear, say "Not clear from code".

    - Follow the exact format below. Do NOT change headings.

    FORMAT:

    [START]

    ## What it does

    (Explain the overall purpose in 1-2 sentences)

    ## Step-by-step

    (Explain how the code works in steps, simple language)

    ## Key Concepts

    (List concepts like loop, condition, function, etc.)

    ## Notes

    (Mention any limitations, errors, or assumptions)

    [END]

    CODE (P063.c):

    #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;
    }