# Category: Sample Java Programs

## Java program to count number of digits in integer

Number of digits in 12345  is 5. Example using while Loop Output Execution of program: count initalize to 0, num to 12345 while loop body will execute if num != 0 num = num /10; Here, 12345 /10 result in 1234.5 since num is integer decimal number is ignored. the value of num becomes 1234

## Java Program to find Prime Number

Java Program to find Prime Number : Prime Number is a positive integer number that is divisible by only two numbers, that is 1 and the number itself. Note: 1 is not a prime number as it isn’t divisible by two number.  The prime numbers are 2, 3, 5, 7, 11……. Java Program to find

## Java Program that will encode a number n as an array that contains the prime numbers

The fundamental theorem of arithmetic states that every natural number greater than 1 can be written as a unique product of prime numbers. So, for instance, 6936=2*2*2*3*17*17. A method named encodeNumber will encode a number n as an array that contains the prime numbers that, when multipled together, will equal n. So encodeNumber(6936) would return

## Java Program to check a cube powerful number

A number n>0 is called cube-powerful if it is equal to the sum of the cubes of its digits. Examples: if n is return because 153 1 because 153 = 13 + 53 + 33 370 1 because 370 = 33 + 73 + 03 371 1 because 371 = 33 + 73 + 13

## Java Program to find largest prime factor

A factor that is a prime number is prime factor.  A function named largestPrimeFactor that will return the largest prime factor of a number. If the number is <=1 it should return 0.  Recall that a prime number is a number > 1 that is divisible only by 1 and itself, e.g., 13 is prime

## Java Program to calculate largest adjacent sum of a given array

A function named largestAdjacentSum that iterates through an array computing the sum of adjacent elements and returning the largest such sum. Examples: if a is return {1, 2, 3, 4} 7 because 3+4 is larger than either 1+2 or 2+3 {18, -12, 9, -10} 6 because 18-12 is larger than -12+9 or 9-10 {1,1,1,1,1,1,1,1,1} 2

## Java Program to calculate the largest exponent of a number

A method named getExponent(n, p) that returns the largest exponent x such that px evenly divides n. If p is <= 1 the method should return -1. For example, getExponent(162, 3) returns 4 because 162 = 21 * 34, therefore the value of x here is 4. Examples: if n is and p is return

## Java Program to find fullness quotient of a number

The fullness quotient of an integer n > 0 to be the number of representations of n in bases 2 through 9 that have no zeroes anywhere after the most significant digit. For example, to see why the fullness quotient of 94 is 6 examine the following table which shows the representations of 94 in

## Java Program to generate cluster compression array

A cluster in an integer array to be a maximum sequence of elements that are all the same value. For example, in the array {3, 3, 3, 4, 4, 3, 2, 2, 2, 2, 4} there are 5 clusters, {3, 3, 3}, {4, 4}, {3}, {2, 2, 2, 2} and {4}. A cluster-compression of an array

## Java Program to check trivalent array

An array to be trivalent if all its elements are one of three different values. For example, {22, 19, 10, 10, 19, 22, 22, 10} is trivalent because all elements are either 10, 22, or 19. However, the array {1, 2, 2, 2, 2, 2, 2} is not trivalent because it contains only two different