Check whether a given number is a Harshad number or not

Write a C program to check whether a given number is a Harshad number or not. or Write a program to check whether a given number is a Harshad number or not in C.

Program in C

Code:

/*Write a C program to check whether a given number is a Harshad number or not. or Write a program to check whether a given number is a Harshad number or not Using C*/

#include<stdio.h>
int main()
{
    int num;
    printf("Enter a number:");
    scanf("%d",&num);
    int num2=num;
    int sum=0;
   while(num!=0)
   {
       int rem=num%10;
       num=num/10;
       sum=sum+rem;
   }
   if(num2%sum==0)
    printf("It is a Harshad Number");
   else
    printf("It is not a Harshad Number");
}

Input/Output:
Enter a number:21
It is a Harshad Number



Write a C++ program to check whether a given number is a Harshad number or not. or Write a program to check whether a given number is a Harshad number or not in C++.

Program in C++

Code:

/*Write a C++ program to check whether a given number is a Harshad number or not. or Write a program to check whether a given number is a Harshad number or not using C++*/

#include<iostream>
using namespace std;
int main()
{
    int num;
    cout<<"Enter a number:";
    cin>>num;
    int num2=num;
    int sum=0;
   while(num!=0)
   {
       int rem=num%10;
       num=num/10;
       sum=sum+rem;
   }
   if(num2%sum==0)
    cout<<"It is a Harshad Number";
   else
    cout<<"It is not a Harshad Number";
}

Input/Output:
Enter a number:7
It is a Harshad Number

Write a JAVA program to check whether a given number is a Harshad number or not. or Write a program to check whether a given number is a Harshad number or not in Java.

Program in Java

Code:

/*Write a JAVA program to check whether a given number is a Harshad number or not. or Write a program to check whether a given number is a Harshad number or not using Java*/

import java.util.Scanner;
public class HarshadNumberOrNot {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int num;
    System.out.println("Enter a number:");
    num=cs.nextInt();
    int num2=num;
    int sum=0;
   while(num!=0)
   {
       int rem=num%10;
       num=num/10;
       sum=sum+rem;
   }
   if(num2%sum==0)
    System.out.println("It is a Harshad Number");
   else
    System.out.println("It is not a Harshad Number");
cs.close();
}
}

Input/Output:
Enter a number:
55
It is not a Harshad Number

Write a PYTHON to check whether a given number is a Harshad number or not. or Write a program to check whether a given number is a Harshad number or not in Python.


Program in Python

Code:

'''Write a Python program to check whether a given number is a Harshad number or not. or Write a program to check whether a given number is a Harshad number or not using Python '''

num=int(input("Enter a number:"))
num2=num
sum=0
while num!=0:
  rem=num%10
  num=int(num/10)
  sum=sum+rem
if(num2%sum==0):
   print("It is a Harshad Number")
else:
print("It is not a Harshad Number") 

Input/Output:
Enter a number:156
It is a Harshad Number







Post a Comment

0 Comments