P063.c (1328B)
1 /* 2 * Author : Amit Dutta <amitdutta4255@gmail.com> 3 * Date : 16 Dec 2025 4 * Repo : https://github.com/notamitgamer/bsc 5 * License : MIT License (See the LICENSE file for details) 6 */ 7 8 /* Write a C program to find all Niven in an array. Define a user-defined function int isNiven(int num) 9 that returns 1 if the numbers is a Niven number, otherwise returns 0. A Niven number (also known as a 10 Harshad Number) is an integer that is divisible by the sum of its digits. */ 11 12 #include <stdio.h> 13 #include <stdlib.h> 14 15 void inputarr(int[], int); 16 int isNiven(int); 17 18 int main() 19 { 20 int n, *arr, i; 21 printf("How many element do you want to enter: "); 22 scanf("%d", &n); 23 arr = (int *)malloc(n * sizeof(int)); 24 inputarr(arr, n); 25 for (i = 0; i < n; i++) 26 { 27 if (isNiven(arr[i])) 28 { 29 printf("\n%d is a Niven number.", arr[i]); 30 } 31 else 32 { 33 printf("\n%d is a NOT Niven number.", arr[i]); 34 } 35 } 36 free(arr); 37 return 0; 38 } 39 40 void inputarr(int arr[], int n) 41 { 42 int i; 43 for (i = 0; i < n; i++) 44 { 45 printf("Enter Element %d: ", i + 1); 46 scanf("%d", &arr[i]); 47 } 48 } 49 50 int isNiven(int num) 51 { 52 int i, sum = 0, temp = num; 53 while (temp > 0) 54 { 55 sum += temp % 10; 56 temp /= 10; 57 } 58 return num % sum == 0; 59 }