bsc

Comprehensive codebase and cou...
Log | Files | Refs | Activity | README | LICENSE

root / semester_1 / tuition-c / P063.c

P063.c (1137B)


      1 /* Write a C program to find all Niven in an array. Define a user-defined function int isNiven(int num)
      2 that returns 1 if the numbers is a Niven number, otherwise returns 0. A Niven number (also known as a
      3 Harshad Number) is an integer that is divisible by the sum of its digits. */
      4 
      5 #include <stdio.h>
      6 #include <stdlib.h>
      7 
      8 void inputarr(int[], int);
      9 int isNiven(int);
     10 
     11 int main()
     12 {
     13     int n, *arr, i;
     14     printf("How many element do you want to enter: ");
     15     scanf("%d", &n);
     16     arr = (int *)malloc(n * sizeof(int));
     17     inputarr(arr, n);
     18     for (i = 0; i < n; i++)
     19     {
     20         if (isNiven(arr[i]))
     21         {
     22             printf("\n%d is a Niven number.", arr[i]);
     23         }
     24         else
     25         {
     26             printf("\n%d is a NOT Niven number.", arr[i]);
     27         }
     28     }
     29     free(arr);
     30     return 0;
     31 }
     32 
     33 void inputarr(int arr[], int n)
     34 {
     35     int i;
     36     for (i = 0; i < n; i++)
     37     {
     38         printf("Enter Element %d: ", i + 1);
     39         scanf("%d", &arr[i]);
     40     }
     41 }
     42 
     43 int isNiven(int num)
     44 {
     45     int i, sum = 0, temp = num;
     46     while (temp > 0)
     47     {
     48         sum += temp % 10;
     49         temp /= 10;
     50     }
     51     return num % sum == 0;
     52 }
© notamitgamer • Site Built: 2026-09-05 01:53:16 UTC • git-mirror commit: c170d72 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror