Skip to main content

Niven number in java


A number is said to be niven or harshad if the number is divisible by the sum of its digits.
EXAMPLE-135
The sum of digits are: 1 + 3 + 5                                                                  135/9=15
A simple java program to check whether a number is harshad / niven or not can be made with the help of while loop.
For this, firstly we extract the digits of the number and then we sum up all the digits.After all the digits are summed then we check whether rhe number when divided by the sum of its digits leaves any remainder or not.
If the remainder is zero then number is niven / harshad number else not.

ALGORITHM-
STEP 1: Start
STEP 2 : print "enter a number"
STEP 3 : taking input of integer in n
STEP 4 : int s = 0
STEP 5 : int no = n
STEP 6 : repeat STEP 7 to STEP 9 until no!=0
STEP 7 : int d=no%10
STEP 8 : s=s+d
STEP 9 : no=no/10
STEP 10: if (n%s==0) then goto STEP 11 else goto STEP 12
STEP 11 : print n+"is a harshad number"
STEP 12 : print n+"is not a harshad number"
STEP 13 : end

JAVA CODE-
import java.util.*;
public class harshad
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("enter a number");
int n=sc.nextInt();
int s=0;
int no=n;
while(no!=0)
{
int d=no%10;
s=s+d;
no=no/10;
}
if(n%s==0)
{
System.out.println(n+" is a harshad number");
}
else
{
System.out.println(n+" is not a harshad number");
    }
  }
}

Output-
enter a number
135
135 is a harshad number

SIMILAR JAVA CODES-
If you find this helpful please share

Comments

Popular posts from this blog

How to Learn whole syllabus of subject in 1 day? + some cool last minute tips

Hi everyone,  In  this article I am going to tell you how to complete the whole syllabus for exam in one day. I will tell you a full one day plan to complete all your syllabus without having any doubt and also prepare for that subject. To follow this, firstly you have to divide the syllabus of your subjects into 3 parts-  The first part is for easy chapters second part is for moderate chapters and third parties for difficult chapters in easy chapters you have to put it at topics for which you can answer all the question.  In moderate you have to put that chapters in which you have very less doubt. In the difficult you have to put that topic whose questions you are not able to answer. HERE IS THE TEMPLATE FOR YOUR PLANNING- PLAN starting in the morning  Prepare a chart of the whole day time table. Begin your day at 8:00 a.m.  In the first three hours revise or learn all the chapters which are easy for you remember after every 50 minutes you have to take 10 m...

disarium number in java

A number is said to be disarium if the sum of its digit powered with their positions is equal to the number itself. EXAMPLE- 135 = (1^1) + (3^2) + (5^3)                             = 1 + 9 + 125                             = 135 A simple java program to check disarium number can be made by firstly calculating the length of the number and then adding digits of the number raised to the power of its position in the number. ALGORITHM - STEP 1 : Start STEP 2 : print "enter a number" STEP 3 : taking input of integer in n STEP 4 : int l= Integer.toString().length() STEP 5 : int no=n STEP 6 : int s=0 STEP 7 : repeat STEP 8 to STEP 11 until (no!=0) STEP 8 : int d=no%10 STEP 9 : s=s+(int)(Math.pow(d,l) STEP 10 :no=no/10 STEP 11 : l-- STEP 12 : if  (s==n) then goto STEP 13 else goto STEP 14 STEP 13 : print "the number is disarium" STEP 14 : print "the num...

Happy number in java, program to check happy number

Happy number is similar to magic number, but instead of obtaining sum of the digits we obtain sum of square of the digits. EXAMPLE-1 19 =1²+9²=82 82=8²+2²=68 68=6²+8²=100 100=1²+0²+0²=1 EXAMPLE-2 7=7²=49 49=4²+9²=97 97=9²+7²=130 130=1²+3²+0²=10 10=1²+0²=1 EXAMPLE-3 13=1²+3²=10 10=1²+0²=1 A java program on happy number can be made by extracting digits of the number then adding the square of the digits. If the sum result to 1 then it is a happy number and if it not results to 1 then if the sum is greater than 10 then it digits are re-extracted and then again added it's square of digits. JAVA CODE- import java.util.*; public class happy { int n; happy() {     n=0; } void getNum(int nx) {   n=nx;   } int sum_sq_digits(int d) {     int sum=0;     while(d>0)     {         sum=sum+((d%10)*(d%10));         d=d/10;     }     int s;int c=0;     while(sum>9)   ...