# Find out all Harshad numbers present within a given range

Write a C program to find out all Harshad numbers present within a given range. or Write a program to find out all Harshad numbers present within a given range in C.

Program in C

Code:

/*Write a C program to find out all Harshad numbers present within a given range. or Write a program to find out all Harshad numbers present within a given range Using C*/

#include<stdio.h>
int main()
{
int range1,range2,i;
printf("Enter a range:");
scanf("%d %d",&range1,&range2);
printf("Harshad numbers between %d and %d are: ",range1,range2);
for(i=range1;i<=range2;i++)
{
int num2=i;
int num1=i;
int sum=0;
while(num1!=0)
{
int rem=num1%10;
num1=num1/10;
sum=sum+rem;
}
if(num2%sum==0)
printf("%d ",i);
}
}

Input/Output:
Enter a range:
10
50
Harshad numbers between 10 and 50 are: 10 12 18 20 21 24 27 30 36 40 42 45 48 50

Write a C++ program to find out all Harshad numbers present within a given range. or Write a program to find out all Harshad numbers present within a given range in C++.

Program in C++

Code:

/*Write a C++ program to find out all Harshad numbers present within a given range. or Write a program to find out all Harshad numbers present within a given range using C++*/

#include<iostream>
using namespace std;
int main()
{
int range1,range2;
cout<<"Enter a range:";
cin>>range1>>range2;
cout<<"Harshad numbers between "<<range1<<" and "<<range2<<" are: ";
for(int i=range1;i<=range2;i++)
{
int num2=i;
int num1=i;
int sum=0;
while(num1!=0)
{
int rem=num1%10;
num1=num1/10;
sum=sum+rem;
}
if(num2%sum==0)
cout<<i<<" ";
}
}

Input/Output:
Enter a range:
51
100
Harshad numbers between 51 and 100 are: 54 60 63 70 72 80 81 84 90 100

Write a JAVA program to find out all Harshad numbers present within a given range. or Write a program to find out all Harshad numbers present within a given range in Java.

Program in Java

Code:

/*Write a JAVA program to find out all Harshad numbers present within a given range. or Write a program to find out all Harshad numbers present within a given range using Java*/

import java.util.Scanner;

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int range1,range2;
System.out.println("Enter a range:");
range1=cs.nextInt();
range2=cs.nextInt();
System.out.println("Harshad numbers between "+range1+" and "+range2+" are: ");
for(int i=range1;i<=range2;i++)
{
int num2=i;
int num1=i;
int sum=0;
while(num1!=0)
{
int rem=num1%10;
num1=num1/10;
sum=sum+rem;
}
if(num2%sum==0)
System.out.print(i+" ");
}
cs.close();
}
}

Input/Output:
Enter a range:
200
250
Harshad numbers between 200 and 250 are:
200 201 204 207 209 210 216 220 222 224 225 228 230 234 240 243 247

Write a PYTHON to find out all Harshad numbers present within a given range. or Write a program to find out all Harshad numbers present within a given range in Python.

Program in Python

Code:

'''Write a Python program to find out all Harshad numbers present within a given range. or Write a program to find out all Harshad numbers present within a given range using Python '''

print("Enter a range:")
range1=int(input())
range2=int(input())
print("Harshad numbers between ",range1," and ",range2," are: ")
for i in range(range1,range2+1):
num2=i
num=i
sum=0
while num!=0:
rem=num%10
num=int(num/10)
sum=sum+rem
if(num2%sum==0):
print(i,end=" ")

Input/Output:
Enter a range:
500
550
Harshad numbers between  500  and  550  are:
500 504 506 510 511 512 513 516 518 522 531 540 550