C++ Program To Check If A Number Is Harshad Number - C++ Programming Tutorial
C++ Course / C++ Programs / C++ Program To Check If A Number Is Harshad Number

C++ Program To Check If A Number Is Harshad Number

BLUF: Mastering C++ Program To Check If A Number Is Harshad Number is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: C++ Program To Check If A Number Is Harshad Number

C++ is renowned for its efficiency. Learn how C++ Program To Check If A Number Is Harshad Number enables low-level control and high-performance computing in the tutorial below.

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.
  • Key concepts:

  • 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.
  • Problem description:

A given number is checked to determine if it is a Harshad or Niven number, returning true if it meets the criteria and false if it does not.

Example 1:

Example

Input: 207
Output:
Yes

Explanation:

The sum of digits: 2 + 0 + 7 = 9

Now, we divide: 207 ÷ 9 = 23

It can be divided evenly without any leftover remainder.

So, this is a Harshad number.

Example 2:

Example

Input: 35
Output:
No

Explanation:

The sum of digits: 3 + 5 = 8

Now, we divide: 35 ÷ 9 = X

It is not entirely divisible and results in a remainder.

So, this is not a Harshad number.

Example Code 1:

Let's consider an example to verify whether a given number qualifies as a Harshad Number in C++.

Example

#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:

Output

Enter a number: 78
78 is not a Harshad Number.
Enter a number: 45
45 is a Harshad Number.

Example Code 2:

Let's consider another scenario to verify whether a given number qualifies as a Harshad Number in the C++ programming language.

Example

#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:

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 essence, a C++ script verifying if a provided number is a Harshad Number showcases fundamental programming concepts like if statements, loops, and mathematical calculations. This script provides a simple yet effective approach to solving a mathematical problem computationally by breaking it down into two separate stages: calculating the digit sum and verifying divisibility. Demonstrated in the subsequent instance, employing modular coding enhances code readability and reusability. By delving into number theory and reinforcing their understanding of core programming concepts, these exercises aid beginners in enhancing their logical thinking and coding abilities.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience