Skip to main content

Posts

Showing posts with the label Java program

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)   ...

Emirp number in java, program to check emirp number

Emirp is simply the reverse prononciation of word "prime". An emirp number is the number which is prime and whose reverse is also a prime number. EXAMPLE-1 17  Reverse of the number = 71 Both the numbers are prime so 17 is an emirp number. EXAMPLE-2 37 Reverse of the number = 73 Both the numbers are prime so 37 is an emirp number. EXAMPLE-3 13 Reverse of the number = 31 Both the numbers are prime so 13 is an emirp number. Emrip number can be easily made in java with the help of methods/functions. Firstly, take an input of number, then check it is prime or not, then reverse the number and again check it is prime or not, if both are true, print the suitable output. ALGORITHM- STEP 1 : start STEP 2 : print "enter a number" STEP 3 : take input of number in e STEP 4 : int n=num, int rev =0 STEP 5 : repeat STEP to STEP until n>0 STEP 6 : int d=n%10 STEP 7 : rev=(rev*10)+d STEP 8 : n=n/10 STEP 9 :  int p= input value from STEP 13 STEP 10 : int d=0, x=num STEP 11 : int i...

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...

What is magic number in java ?

A number is said to be magic number if the repetitive sum of digits of number is equal to 1 or a single digit number . If the repetitive sum ends up  as 1 then the number is a magic number, else not. For example- n= 289;.   2+8+9 = 19      19;.      1+9 = 10      10;.      1+0= 0       1 Since the number ends in 1, hence 289 is a magic number. Magic number in java is the java program to check whether a number is magic number or not . For this we do repetitive addition of the digits of the number till the number is a two or more digit number. If after repetitive adding digits of the number ends up to 1 then the loop stops and a message is displayed. For this program we need to extract digits of the number for this we use while loop and add this digit . Code snippet to get digits of the number - while(n!=0) {        int d = n%10;         s=s+d;   ...

Palindrome number in java

A number is said to be palindrome if it reads both forward and backwards same. Example- 175571 454 A program for palindrome number can be made by simply extracting digits of the number in reverse order and adding them with their place value and then checking is it same as the original number or not. To extract a digit in revers order and adding with place value we simply follow the below code. while(n>0) { int d=n%10; rev=(rev*10)+d; n=n/10; } To explain this let us take an example 657 Then  For first iteration =  n=657                                     d=657%10=7                                     rev=(0*10)+7=7 For second iteration = n=65                                         ...

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...

Perfect number in java, program for perfect number in java

A perfect number is one where the sum of factors of a number (excluding the number itself) is equal to the number itself. EXAMPLE- We take 6. Factors of 6 are: 1,2,3 6 = 1 + 2 + 3 6 is a perfect number. Now we take 28 .Factors of 28 are: 1,2,4,7,14 28 = 1 + 2 + 4 + 7 + 14 28 is a perfect number. Perfect number in java language can be made by first applying a for loop starting from 1 to 1 less than that number and then with the help of if statement, checking if the number is divisible by that particular number which is currently stored in loop control variable.  Then after the loop terminates we will check whether the sum of factors of the number is equal to the number itself or not. If the statement returns true then appropriate statement prints. Here is the code for perfect number in two different formats firstly with the help of methods and secondly a very simple program using main method.

Java program for prime numbers

A prime number is the number which is is  divisible by itself and not by other numbers. A program to check prime number can be made by using for and while loop combination . This article contains java program for finding prime number ,write a java program for prime number between 1- 100 ,simple program to find prime factorial of a number in java. EXAMPLE 1 - program to check whether a number is prime number  or not. import java.util.*; public class pr { public static void main() { Scanner sc=new Scanner(System.in); System.out.println("enter number"); int num=sc.nextInt(); int d=0; for(int i=2;i<=num;i++) { if(num%i==0) d++; } if(d==0) System.out.println("it is a prime number"); else System.out.println("it is not a prime number"); } } Output enter number 23 it is a p...