Write a program in C++ to check if a number is Harshad Number or not

  • برمجة سي بلس بلس
  • برمجة

Write a program in C++ to check if a number is Harshad Number or not

Sample Output:

Check whether a number is Harshad Number or not:                                                    
 ---------------------------------------------------                                                 
 Input a number: 18                                                                                  
 The given number is a Harshad Number.

الأجوبة

#include<bits/stdc++.h>
using namespace std;
bool chkHarshad(int n)
{
    int s = 0;
	int tmp;
    for (tmp=n; tmp>0; tmp /= 10)
        s += tmp % 10;
    return (n%s == 0);
}
 

int main()
{
    int hdno;
 cout << "\n\n Check whether a number is Harshad Number or not: \n";
 cout << " ---------------------------------------------------\n";
 cout << " Input a number: ";
 cin >> hdno;
 
     if( chkHarshad(hdno))
        cout << " The given number is a Harshad Number."<<endl;
    else
        cout << " The given number is not a Harshad Number."<<endl;
    return 0;

}
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...