The eight primitive data types in Java are byte, short , int , long , float , double , char and boolean.
data_type variable_name = initial_value;
The purpose of initializing a variable in Java is to assign it an initial value so that it can be safely used in program . By initializing a variable, it prevents runtime exceptions , avoid compilation errors and enhance safety & security.
data_type variable_name;
data_type variable_name;
In Java primitive data type store the actual value directly , while reference data type store a memory address that points to where the actual data is located .The size of the primitive variable is fixed whereas the size of reference variable is dynamic
Variables in Java are used to allocate specific amount of memory for the data and to inform the compiler about the type of data that will be stored.
The two main categories of data types in java are primitive data type and reference data type.
‘int’ is used for storing integer values. float is a single -precision floating -point data type, and double is a double-precision floating-point data type .The size of the ‘int’ data type is 4 bytes(32-bit), the size of the ‘double’ data type is 8 bytes( 64 -bit) and the size of the ‘float’ data type is 4 bytes(32-bit).
The default value of boolean variable in Java is ‘false’.
Data type in java defines the type and size of data a variable can store.
Given number is z , determine whether this number is Armstrong Number or not. A positive integer number of n digits is called an Armstrong Number of order n if
abcd…=pow(a,n) + pow(b, n) +pow(c,n)+pow(d,n)+………, where a, b, c, d are digits of input number abcd….., n is the number of digits present in the number.
Approach
- First count the number of digits present in the given number z. Let the number of digits be n.
- For every digits present in the number z ,let digits are d1 , d2 , d3,…..;compute d1^n , d2^n , d3^n……..
- If the sum of all computed digits are equal to z i.e. d1^n+d2^n+d3^n+……..=z; it is an armstrong number, then print that this number is an armstrong number, else print this number is not an armstrong number.
Example:
153 is an armstrong number because
1^3+5^3+3^3=153
1634 is a 4 digit armstrong number because
1^4+6^4+3^4+4^4=1634
Java Program to check a number is Armstrong Number Or Not
public class ArmstrongNumber {
public static void main (String args[]) {
// code to enter a number you want to check armstrong number or not
Scanner sc = new Scanner(System.in);
System.out.println(“Enter the number you want to check whether armstrong number or not”);
int n = sc.nextInt();
int temp=n,digits=0,rev,sum=0;
//To find the number of digits:
while(temp>0) {
temp=temp/10;
digits++;
}
temp=n;
while (temp > 0) {
//To find the last digit
rem=temp%10;
//Sum up of the each digit to the power total number of digits present in the number
sum + = Math.pow(rem,digits);
//To remove the last digit
temp=temp/10;
}
if (sum==n) {
System.out.println(“The given number is an armstrong number”);
}
else
System.out.println(“The given number is not an armstrong number”);
}
}
}
If you don’t understand how to find the number of digits java program and how to sum up each digits of a number,
Check the following :
Java Program to count the number of digits in a given number
Let, Given number be n , count the number of digits present in this number
Example :
Input n= 9876
Output: 4
There are 4 digits in 9876 , which are 9 , 8 , 7 & 6.
Approach :
In this approach count the digits by removing the digits from the given number starting from right to left till the number is reduced to 0 . Here remove digits from right because rightmost digit can be removed by performing integer division by 10 . For eg : n=9876, then 9876/10 = 987.6=987(Integer Division ) .
import java.util.*;
public class CountDigits {
public static void main (String args[] ) {
Scanner sc = new Scanner ( System.in) ;
System.out.println(“Enter the number to count the digits in it”);
int num=sc.nextInt();
int temp=num,count=0;
// if the number is exactly 0 , it has 1 digit
if(temp==0) {
count=1 ;
} else {
//Loop runs until the number become 0
while (temp >0 ) {
temp=temp/10; //Removes the last digit
count++;
}
}
System.out.println(“Number of digits : ” + count);
}
}
