





Please login
Prime

Prepinsta Prime
Video courses for company/skill based Preparation
(Check all courses)
Get Prime Video
Prime

Prepinsta Prime
Purchase mock tests for company/skill building
(Check all mocks)
Get Prime mock
C++ program to check whether a number is a Harshad Number or not
Program to check whether a number is a Harshad Number or not
A Harshad number is a positive integer which is divisible by the sum of the digits of the integer. It is also called the Niven number.
To find the Harshad number find and add the digits of the number. If the number is divisible by the sum then the number is a Harshad number else not a Harshad number.
For Example : 153
Sum of digits = 1 + 5 + 3= 9
153 is divisible by 9 so 153 is a Harshad Number.


Algorithm:-
- Take input from User
- Input is stored in an int type variable say num.
- Input is copied in another variable say save
- A loop is started
- Digits of the number is found
- each digit is added to sum( initial value 0).
- num is divided by 10 i.e value of num is changed in every cycle.
- loop runs until num is greater than 0.
- save is divided by sum
- if there is no remainder then the number is a Harshad number
- otherwise number is not a Harshad number
C++ Code:-
//C++ Program
//Harshad number or not
#include <iostream>
using namespace std;
//main program
int main()
{
int num,sum = 0;
cout<<“Enter number: “;
//user input
cin>>num;
int n = num;
//loop to calculate the sum of digits
while(num > 0)
{
int rem = num%10;
sum = sum + rem;
num = num/10;
}
//checking for harshad number
if(n % sum == 0)
cout<<n<<” is a harshad number”;
else
cout<<n<<” is not a harshad number”;
return 0;
}
Output
Enter number: 81
81 is a harshad number
Note
Note that harshard number is an integer that is divisible by the sum of its digits.
- Positive or Negative number: C | C++ | Java
- Even or Odd number: C | C++ | Java
- Sum of First N Natural numbers: C | C++ | Java
- Sum of N natural numbers: C | C++ | Java
- Sum of numbers in a given range: C | C++ | Java
- Greatest of two numbers: C | C++ | Java
- Greatest of the Three numbers: C | C++ | Java
- Leap year or not: C | C++ | Java
- Prime number: C | C++ | Java
- Prime number within a given range: C | C++ | Java
- Factorial of a number: C | C++ | Java
- Sum of digits of a number: C | C++ | Java
- Reverse of a number : C | C++ | Java
- Palindrome number: C | C++ | Java
- Armstrong number : C | C++ | Java
- Armstrong number in a given range : C | C++ | Java
- Fibonacci Series upto nth term : C | C++ | Java
- Factorial of a number : C | C++ | Java
- Power of a number : C | C++ | Java
- Factor of a number : C | C++ | Java
- Strong number : C | C++ | Java
- Perfect number : C | C++ | Java
- Automorphic number : C | C++ | Java
- Harshad number : C | C++ | Java
- Abundant number : C| C++ | Java
- Friendly pair : C | C++ | Java

Login/Signup to comment