C Program to check whether a number is Harshad number or not
Write a C Program to check if number is Harshad number or not
In mathematics, a Harshad number is a number that is divisible by the sum of its digits.
Ex- Number is 21 it is divisible by its own sum (1+2) of its digit(2,1) So it is Harshad's Number Some other Harshad's Number are 156,54,120 etc
Working:-
For user input num
- Extract individual digits of num
- Calculate the sum of these digits
- Check if the num is divisible by sum
- If yes, then its a harshad’s number
- Else its not
C Program:-
Run
#include <stdio.h> int checkHarshad(int num){ int sum = 0; int temp = num; while(temp != 0){ sum = sum + temp % 10; temp /= 10; } // will return 1 if num is divisible by sum, else 0 return num % sum == 0; } int main () { int num = 153; if(checkHarshad(num)) printf("%d is Harshad's Number", num); else printf("%d is not Harshad's Number", num); return 0; } // Time complexity: O(N) // Space complexity: O(1)
Output:-
153 is Harshad's Number
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
- Positive or Negative number: C | C++ | Java | Python
- Even or Odd number: C | C++ | Java | Python
- Sum of First N Natural numbers: C | C++ | Java | Python
- Sum of N natural numbers: C | C++ | Java | Python
- Sum of numbers in a given range: C | C++ | Java | Python
- Greatest of two numbers: C | C++ | Java | Python
- Greatest of the Three numbers: C | C++ | Java | Python
- Leap year or not: C | C++ | Java | Python
- Prime number: C | C++ | Java | Python
- Prime number within a given range: C | C++ | Java | Python
- Sum of digits of a number: C | C++ | Java | Python
- Reverse of a number : C | C++ | Java | Python
- Palindrome number: C | C++ | Java | Python
- Armstrong number : C | C++ | Java | Python
- Armstrong number in a given range : C | C++ | Java | Python
- Fibonacci Series upto nth term : C | C++ | Java | Python
- Find the Nth Term of the Fibonacci Series : C | C++ | Java | Python
- Factorial of a number : C | C++ | Java | Python
- Power of a number : C | C++ | Java | Python
- Factor of a number : C | C++ | Java | Python
- Strong number : C | C++ | Java | Python
- Perfect number : C | C++ | Java | Python
- Automorphic number : C | C++ | Java | Python
- Harshad number : C | C++ | Java | Python
- Abundant number : C| C++ | Java | Python
- Friendly pair : C | C++ | Java | Python
Login/Signup to comment
# easy python code
num = int(input(“Enter a number “))
num1 = [int(d) for d in str(num)]
sum = 0
for i in num1:
sum=sum + i
if num%sum==0 :
print(“harshad number “)
else:
print(‘Not a harshad Number ‘)
Harshad Number in Python :
n = int(input(“Enter a number : “))
temp = n
n = [int(d) for d in str(n)]
sum = 0
x = list(n)
for i in x:
sum = sum + i
if temp % sum == 0:
print(“It is a Harshad Number”)
else:
print(“It is not a Harshad number”)