Java Program To Find The Frequency Of Each Digit In A Given Number. | JAVA POINT
Write a Java program to find the frequency of each digit in a given number.
PROGRAM :
Output :
How to Convert a Decimal Number to Binary Equivalent in Java
Write a Java program to convert the decimal number into the binary equivalent.
Decimal Number System -
The decimal number system consists of digits from 0 to 9.
Binary Number System -
The binary number system consists of only two digits which are 0 and 1.
PROGRAM :
import java.util.Scanner;
public class DecimalBinary
{
public static void main(String args[])
{
int dec, q, i=1, j;
int bin[] = new int[100];
Scanner scan = new Scanner(System.in);
System.out.print("Enter a Decimal Number : ");
dec = scan.nextInt();
q = dec;
while(q != 0)
{
bin[i++] = q%2;
q = q/2;
}
System.out.print("Equivalent Binary number is: ");
for(j=i-1; j>0; j--)
{
System.out.print(bin[j]);
}
System.out.print("\n");
}
}
Output:
Enter a Decimal Number : 45
Equivalent Binary number is: 101101
Also read:
Java program to check the Neon Number | JAVA POINT
Write a Java program to check that a given number is Neon number or not.
![]() |
Neon number
A number is said to be a Neon Number if the sum of digits of the square of the number is equal to the number itself.
PROGRAM :
import java.util.*;
public class neon
{
public static void main(String args[])
{
Scanner ob=new Scanner(System.in);
System.out.print("\nEnter the number to be checked : ");
int num=ob.nextInt();
int square=num*num;
int sum=0;
while(square!=0)//Loop to find the sum of digits.
{
int a=square%10;
sum=sum+a;
square=square/10;
}
if(sum==num)
{
System.out.println(num+" is a Neon Number.");
}
else
{
System.out.println(num+" is not a Neon Number.");
}
}
}
Output :
Java program to check the DISARIUM number | JAVA POINT
Write a Java program to check that a given number is DISARIUM number or not.
DISARIUM number
A number will be called DISARIUM if sum of its digits powered with their respective position is equal to the original number.
PROGRAM :
import java.util.Scanner;
class disarium
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.print("\nEnter the number to be checked : ");
int n=sc.nextInt();
int copy = n, d = 0, sum = 0;
String s = Integer.toString(n); //converting the number into a String
int len = s.length(); //finding the length of the number i.e. no.of digits
while(copy>0)
{
d = copy % 10; //extracting the last digit
sum = sum + (int)Math.pow(d,len);
len--;
copy = copy / 10;
}
if(sum == n)
System.out.println(n+" is a Disarium Number.");
else
System.out.println(n+" is not a Disarium Number.");
}
}
Output :
Java program to insert an element into the array | JAVA POINT
Write a Java program to insert an element into the array.
PROGRAM :
import java.util.*;
class arrinsert
{
public static void main(String[] args)
{
int n,pos,x;
Scanner sc=new Scanner(System.in);
System.out.print("\nEnter size of array : ");
n=sc.nextInt();
int a[]=new int[n+1];
System.out.print("\nEnter all the elements of array : \n");
System.out.println();
for(int i=0; i<n; i++)
{
a[i]=sc.nextInt();
}
System.out.println("\nBefore insertion the array is :");
for(int i=0; i<n; i++)
{
System.out.print(a[i]+" ");
}
System.out.println();
System.out.print("\nEnter the position where you want to insert the element: ");
pos=sc.nextInt();
System.out.print("\nEnter the element you want to insert : ");
x=sc.nextInt();
for(int i=(n-1); i>=(pos-1); i--)
{
a[i+1]=a[i];
}
a[pos-1]=x;
System.out.println("\nAfter insertion");
for(int i=0; i<n; i++)
{
System.out.print(a[i]+",");
}
System.out.print(a[n]);
}
}
Output :
Java Program to display all the Prime numbers between 1 and n
Write a Java Program to display all the prime numbers between 1 to n (the number n is entered by user).
Prime Numbers
Prime numbers are the numbers that have only two factors, that are, 1 and the number itself.
PROGRAM :
import java.util.Scanner;
class PrimeNumbers
{
public static void main (String[] args)
{
Scanner scanner = new Scanner(System.in);
int i =0;
int num =0;
String primeNumbers = " ";
System.out.println("Enter the value of n:");
int n = scanner.nextInt();
for (i = 1; i <= n; i++)
{
int counter = 0;
for(num =i; num>=1; num--)
{
if (i%num == 0)
{
counter = counter + 1;
}
}
if (counter == 2)
{
primeNumbers = primeNumbers + i + " ";
//Appended the Prime number to the String
}
}
System.out.println("Prime numbers from 1 to n are :");
System.out.println(primeNumbers);
}
}
/*
Output:
Enter the value of n:
100
Prime numbers from 1 to n are :
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
*/
Java program for the pattern of 'E' | Patterns in JAVA | JAVA POINT
Ques : Write a java program for following Pattern of 'E' :
* * * *
*
*
* * * *
*
*
* * * *
PROGRAM :
import java.util.Scanner;
public class PatternE
{
public static void main(String[] args)
{
System.out.print("Enter the number of
rows : ");. // 7
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
System.out.println();
for (int i=0;i<n ;i++ )
{
for (int j=0;j<=n/2 ;j++ )
{
if (i==0 || i==n-1 || i==n/2 || j==0)
{
System.out.print("* ");
}
else
{
System.out.print(" ");
}
}
System.out.println();
}
}
}
Java program for the pattern of 'A' | Pattern Designs | JAVA POINT
