Numbers that can be divided by the sum of their digits are called "Harshad" or "Niven" numbers. For instance, 18 is a Harshad number because it is divisible by 9, and 1 + 8 = 9. This C++ program checks to see if an integer is a Harshad number. In order to determine whether an integer is divisible, it first accepts it as input and then utilizes a loop to compute the sum of its digits. If the input is a Harshad Number, the software outputs that information. This exercise aids in practicing C++ ideas, such as loops, arithmetic operations, and conditional logic.
- Request a number from the user.
- Find the sum of its digits.
- Check to see if you can divide the integer by the sum of its digits.
- Indicate whether the input number is a Harshad number by displaying the result.
- Arithmetic operations: Dividing an integer into its digits using modulus and division operations.
- Looping constructs: The sum of digits is computed using loops.
- Statements with conditions: The integer's divisibility by the sum of its digits is confirmed.
- Input-output operations include: It displays the outcomes of human interaction.
Key concepts:
Problem description:
A number is provided to us, and we must return true if it is a Harshad or Niven number and false otherwise.
Example 1:
Input: 207
Output:
Yes
Explanation:
The sum of digits: 2 + 0 + 7 = 9
Now, we divide: 207 ÷ 9 = 23
It is completely divisible and leaves no remainder.
So, this is a Harshad number.
Example 2:
Input: 35
Output:
No
Explanation:
The sum of digits: 3 + 5 = 8
Now, we divide: 35 ÷ 9 = X
It is not completely divisible and leaves a remainder.
So, this is not a Harshad number.
Example Code 1:
Let us take an example to check if a number is Harshad Number in C++.
#include <iostream>
using namespace std;
int main() {
int number, temp, digitSum = 0;
// Input a number from the user
cout << "Enter a number: ";
cin >> number;
// Store the original number in a temporary variable
temp = number;
// Calculate the sum of the digits
while (temp > 0) {
digitSum += temp % 10; // Extract the last digit and add to digitSum
temp /= 10; // Remove the last digit
}
// Check if the number is divisible by the sum of its digits
if (number % digitSum == 0) {
cout << number << " is a Harshad Number." << endl;
} else {
cout << number << " is not a Harshad Number." << endl;
}
return 0;
}
Output:
Enter a number: 78
78 is not a Harshad Number.
Enter a number: 45
45 is a Harshad Number.
Example Code 2:
Let us take another example to check if a number is Harshad Number in C++.
#include <iostream>
using namespace std;
// Function to calculate the sum of the digits of a number
int sumOfDigits(int num) {
int sum = 0;
while (num > 0) {
sum += num % 10; // Extract the last digit and add to sum
num /= 10; // Remove the last digit
}
return sum;
}
// Function to check if a number is a Harshad Number
bool isHarshad(int num) {
int digitSum = sumOfDigits(num); // Get the sum of digits
return (num % digitSum == 0); // Check divisibility
}
int main() {
int number;
// Input a number from the user
cout << "Enter a number: ";
cin >> number;
// Check and display whether the number is a Harshad Number
if (isHarshad(number)) {
cout << number << " is a Harshad Number." << endl;
} else {
cout << number << " is not a Harshad Number." << endl;
}
return 0;
}
Output:
Enter a number: 207
207 is a Harshad Number.
Enter a number: 21
21 is not a Harshad Number
Explanation:
- It calculates the sum of the digits for the specified number using the sumOfDigits function.
- The number's ability to be divided by the sum of its digits is checked.
- The main program makes the code more readable and reusable by using the functions to decide and display the outcome.
Conclusion:
In summary, a C++ program that determines whether a given number is a Harshad Number illustrates basic programming ideas, such as conditional statements, loops, and arithmetic operations. This program offers a straightforward yet efficient method for computationally solving a mathematical issue by decomposing it into two distinct steps: computing the sum of the digits and confirming divisibility. As the second example illustrates, modular coding makes the program easier to read and reuse. By examining number theory and strengthening their grasp of fundamental programming principles, these tasks assist novices in developing their logical reasoning and coding skills.