Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

Number based Java Programs

Uploaded by

Harsha p
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Number based Java Programs

Uploaded by

Harsha p
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 107

1.

Program to check if a number is prime:


import java.util.Scanner;
public class PrimeCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
boolean isPrime = true;
if (num <= 1) {
isPrime = false;
} else {1
for (int i = 2; i <= Math.sqrt(num); i++) {:
if (num % i == 0) {
isPrime = false;l
break;
}
}ll
}
if (isPrime) {
System.out.println(num + " is a prime number.");
} else {
System.out.println(num + " is not a prime number.");
}
}
}

2.Program to find the factorial of a number:


import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
long factorial = 1;
for (int i = 1; i <= num; i++) {
factorial *= i;
}
System.out.println("Factorial of " + num + " is " + factorial);
}
}

3.Program to print Fibonacci series:


import java.util.Scanner;
public class FibonacciSeries {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of terms: ");
int n = scanner.nextInt();
int first = 0, second = 1;
System.out.print("Fibonacci Series: ");
for (int i = 0; i < n; i++) {
System.out.print(first + " ");
int next = first + second;
first = second;
second = next;
}
}
}

4.Program to find the sum of digits of a number:


import java.util.Scanner;
public class SumOfDigits {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
int sum = 0;
while (num != 0) {
sum += num % 10;
num /= 10;
}
System.out.println("Sum of digits: " + sum);
}
}

5.Program to reverse a number:


import java.util.Scanner;
public class ReverseNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
int reversed = 0;
while (num != 0) {
reversed = reversed * 10 + num % 10;
num /= 10;
}
System.out.println("Reversed number: " + reversed);
}
}

6.Program to check if a number is Armstrong:


import java.util.Scanner;
public class ArmstrongNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
int originalNum = num;
int digits = String.valueOf(num).length();
int sum = 0;
while (num != 0) {
int digit = num % 10;
sum += Math.pow(digit, digits);
num /= 10;
}
if (sum == originalNum) {
System.out.println(originalNum + " is an Armstrong number.");
} else {
System.out.println(originalNum + " is not an Armstrong number.");
}
}
}

7.Program to find the GCD (Greatest Common Divisor) of two numbers:


import java.util.Scanner;

public class GCD {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter first number: ");
int num1 = scanner.nextInt();
System.out.print("Enter second number: ");
int num2 = scanner.nextInt();
int gcd = findGCD(num1, num2);
System.out.println("GCD of " + num1 + " and " + num2 + " is: " + gcd);
}

private static int findGCD(int a, int b) {


while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
}

8.Program to find the LCM (Least Common Multiple) of two numbers:


import java.util.Scanner;

public class LCM {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter first number: ");
int num1 = scanner.nextInt();
System.out.print("Enter second number: ");
int num2 = scanner.nextInt();
int lcm = (num1 * num2) / findGCD(num1, num2);
System.out.println("LCM of " + num1 + " and " + num2 + " is: " + lcm);
}

private static int findGCD(int a, int b) {


while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
}

9.Program to check if a number is a perfect number:


import java.util.Scanner;

public class PerfectNumber {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
boolean isPerfect = checkPerfect(num);
if (isPerfect) {
System.out.println(num + " is a perfect number.");
} else {
System.out.println(num + " is not a perfect number.");
}
}

private static boolean checkPerfect(int num) {


int sum = 0;
for (int i = 1; i < num; i++) {
if (num % i == 0) {
sum += i;
}
}
return sum == num;
}
}

10.Program to find the factorial using recursion:


import java.util.Scanner;

public class RecursiveFactorial {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
long factorial = calculateFactorial(num);
System.out.println("Factorial of " + num + " is: " + factorial);
}

private static long calculateFactorial(int n) {


if (n == 0 || n == 1) {
return 1;
}
return n * calculateFactorial(n - 1);
}
}

11.Program to find the power of a number using recursion:


import java.util.Scanner;

public class Power {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the base: ");
int base = scanner.nextInt();
System.out.print("Enter the exponent: ");
int exponent = scanner.nextInt();
long result = power(base, exponent);
System.out.println(base + " raised to the power " + exponent + " is: " + result);
}

private static long power(int base, int exponent) {


if (exponent == 0) {
return 1;
}
return base * power(base, exponent - 1);
}
}

12.Program to generate the first n prime numbers:


import java.util.Scanner;

public class GeneratePrimes {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the value of n: ");
int n = scanner.nextInt();
int count = 0;
int num = 2;
while (count < n) {
if (isPrime(num)) {
System.out.print(num + " ");
count++;
}
num++;
}
}

private static boolean isPrime(int num) {


if (num <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
}

13.Program to find the sum of all even numbers between 1 to N:


import java.util.Scanner;

public class SumOfEvenNumbers {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the value of N: ");
int n = scanner.nextInt();
int sum = 0;
for (int i = 2; i <= n; i += 2) {
sum += i;
}
System.out.println("Sum of even numbers between 1 and " + n + " is: " + sum);
}
}

14.Program to find the number of digits in a given number:


import java.util.Scanner;

public class CountDigits {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
long num = scanner.nextLong();
int count = 0;
while (num != 0) {
num /= 10;
count++;
}
System.out.println("Number of digits: " + count);
}
}

15.Program to check if a number is a palindrome:


import java.util.Scanner;

public class PalindromeCheck {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
int originalNum = num;
int reversed = 0;
while (num != 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}
if (originalNum == reversed) {
System.out.println(originalNum + " is a palindrome.");
} else {
System.out.println(originalNum + " is not a palindrome.");
}
}
}

16.Program to find the sum of all natural numbers up to N:


import java.util.Scanner;

public class SumOfNaturalNumbers {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the value of N: ");
int n = scanner.nextInt();
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += i;
}
System.out.println("Sum of natural numbers up to " + n + " is: " + sum);
}
}

17.Program to find the factorial of a number using iteration and recursion:


import java.util.Scanner;

public class Factorial {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = scanner.nextInt();
System.out.println("Factorial using iteration: " + factorialIterative(n));
System.out.println("Factorial using recursion: " + factorialRecursive(n));
}

private static int factorialIterative(int n) {


int factorial = 1;
for (int i = 1; i <= n; i++) {
factorial *= i;
}
return factorial;
}

private static int factorialRecursive(int n) {


if (n == 0 || n == 1) {
return 1;
}
return n * factorialRecursive(n - 1);
}
}

18.Program to find the roots of a quadratic equation:


import java.util.Scanner;

public class QuadraticRoots {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter coefficients a, b, and c of the quadratic equation (ax^2 + bx + c =
0):");
double a = scanner.nextDouble();
double b = scanner.nextDouble();
double c = scanner.nextDouble();
double discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
double root1 = (-b + Math.sqrt(discriminant)) / (2 * a);
double root2 = (-b - Math.sqrt(discriminant)) / (2 * a);
System.out.println("Roots are real and distinct. They are: " + root1 + " and " + root2);
} else if (discriminant == 0) {
double root = -b / (2 * a);
System.out.println("Roots are real and equal. They are: " + root + " and " + root);
} else {
double realPart = -b / (2 * a);
double imaginaryPart = Math.sqrt(-discriminant) / (2 * a);
System.out.println("Roots are complex. They are: " + realPart + " + " + imaginaryPart + "i
and "
+ realPart + " - " + imaginaryPart + "i");
}
}
}

19.Program to find the sum of digits of a factorial:


import java.util.Scanner;

public class SumOfDigitsInFactorial {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
long factorial = calculateFactorial(num);
int sum = calculateSumOfDigits(factorial);
System.out.println("Sum of digits in " + num + "! is: " + sum);
}

private static long calculateFactorial(int n) {


if (n == 0 || n == 1) {
return 1;
}
return n * calculateFactorial(n - 1);
}

private static int calculateSumOfDigits(long num) {


int sum = 0;
while (num != 0) {
sum += num % 10;
num /= 10;
}
return sum;
}
}

20.Program to find the nth Fibonacci number using memoization:


import java.util.Scanner;

public class FibonacciWithMemoization {


static long[] memo;

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.print("Enter the value of n: ");
int n = scanner.nextInt();
memo = new long[n + 1];
long result = fibonacci(n);
System.out.println("The " + n + "th Fibonacci number is: " + result);
}

private static long fibonacci(int n) {


if (n <= 1) {
return n;
}
if (memo[n] != 0) {
return memo[n];
}
memo[n] = fibonacci(n - 1) + fibonacci(n - 2);
return memo[n];
}
}

21.Program to find the number of trailing zeroes in factorial of a number:


import java.util.Scanner;

public class TrailingZeroesInFactorial {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
int count = countTrailingZeroes(num);
System.out.println("Number of trailing zeroes in the factorial of " + num + " is: " + count);
}

private static int countTrailingZeroes(int num) {


int count = 0;
for (int i = 5; num / i >= 1; i *= 5) {
count += num / i;
}
return count;
}
}

22.Program to find the sum of all prime numbers up to N:


import java.util.Scanner;
public class SumOfPrimes {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the value of N: ");
int n = scanner.nextInt();
int sum = 0;
for (int i = 2; i <= n; i++) {
if (isPrime(i)) {
sum += i;
}
}
System.out.println("Sum of prime numbers up to " + n + " is: " + sum);
}

private static boolean isPrime(int num) {


if (num <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
}

23.Program to check if a number is a perfect square:


import java.util.Scanner;

public class PerfectSquareCheck {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
if (isPerfectSquare(num)) {
System.out.println(num + " is a perfect square.");
} else {
System.out.println(num + " is not a perfect square.");
}
}

private static boolean isPerfectSquare(int num) {


int sqrt = (int) Math.sqrt(num);
return sqrt * sqrt == num;
}
}

24.Program to find the nth term of the arithmetic progression:


import java.util.Scanner;

public class ArithmeticProgression {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first term of the arithmetic progression: ");
int firstTerm = scanner.nextInt();
System.out.print("Enter the common difference: ");
int commonDifference = scanner.nextInt();
System.out.print("Enter the value of n (term to find): ");
int n = scanner.nextInt();
int nthTerm = firstTerm + (n - 1) * commonDifference;
System.out.println("The " + n + "th term of the arithmetic progression is: " + nthTerm);
}
}

25.Program to find the sum of first N natural numbers using the formula: Sum=(N×(N+1))/2
import java.util.Scanner;

public class SumOfNaturalNumbersFormula {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the value of N: ");
int n = scanner.nextInt();
int sum = n * (n + 1) / 2;
System.out.println("Sum of first " + n + " natural numbers is: " + sum);
}
}

26.Program to find the number of digits in the factorial of a number without calculating the
factorial:
import java.util.Scanner;

public class NumberOfDigitsInFactorial {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
double digits = 0;
for (int i = 2; i <= num; i++) {
digits += Math.log10(i);
}
int numberOfDigits = (int) Math.floor(digits) + 1;
System.out.println("Number of digits in " + num + "! is: " + numberOfDigits);
}
}

27.Program to find the number of ways to express a number as the sum of 1, 3, or 4:


import java.util.Scanner;

public class ExpressAsSum {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
int[] dp = new int[num + 1];
dp[0] = dp[1] = dp[2] = 1;
dp[3] = 2;
for (int i = 4; i <= num; i++) {
dp[i] = dp[i - 1] + dp[i - 3] + dp[i - 4];
}
System.out.println("Number of ways to express " + num + " as the sum of 1, 3, or 4: " +
dp[num]);
}
}

28.Program to find the sum of digits of a given number until it becomes a single-digit number:
import java.util.Scanner;

public class SumOfDigitsSingle {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
int result = sumOfDigitsSingle(num);
System.out.println("Sum of digits until single-digit: " + result);
}

private static int sumOfDigitsSingle(int num) {


while (num >= 10) {
int sum = 0;
while (num != 0) {
sum += num % 10;
num /= 10;
}
num = sum;
}
return num;
}
}

29.Program to find the sum of proper divisors of a number:


import java.util.Scanner;

public class SumOfDivisors {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
int sum = sumOfProperDivisors(num);
System.out.println("Sum of proper divisors of " + num + " is: " + sum);
}

private static int sumOfProperDivisors(int num) {


int sum = 0;
for (int i = 1; i <= num / 2; i++) {
if (num % i == 0) {
sum += i;
}
}
return sum;
}
}

30.Program to find the next palindrome number:


import java.util.Scanner;

public class NextPalindrome {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
int nextPalindrome = findNextPalindrome(num);
System.out.println("Next palindrome number after " + num + " is: " + nextPalindrome);
}

private static int findNextPalindrome(int num) {


num++;
while (!isPalindrome(num)) {
num++;
}
return num;
}

private static boolean isPalindrome(int num) {


int originalNum = num;
int reversed = 0;
while (num != 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}
return originalNum == reversed;
}
}

31.Program to find the sum of first N terms of the Fibonacci series:


import java.util.Scanner;

public class SumOfFibonacci {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of terms: ");
int n = scanner.nextInt();
long sum = sumOfFibonacci(n);
System.out.println("Sum of first " + n + " terms of Fibonacci series: " + sum);
}

private static long sumOfFibonacci(int n) {


if (n <= 0) return 0;
if (n == 1) return 1;
long a = 0, b = 1, sum = 1;
for (int i = 2; i <= n; i++) {
long temp = a + b;
sum += temp;
a = b;
b = temp;
}
return sum;
}
}

32.Program to check if a number is a perfect cube:


import java.util.Scanner;

public class PerfectCubeCheck {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
if (isPerfectCube(num)) {
System.out.println(num + " is a perfect cube.");
} else {
System.out.println(num + " is not a perfect cube.");
}
}

private static boolean isPerfectCube(int num) {


double cubeRoot = Math.cbrt(num);
int roundedCubeRoot = (int) cubeRoot;
return Math.pow(roundedCubeRoot, 3) == num;
}
}

33.Program to find the digital root of a number (repeatedly sum digits until single digit):
import java.util.Scanner;

public class DigitalRoot {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
int digitalRoot = findDigitalRoot(num);
System.out.println("Digital root of " + num + " is: " + digitalRoot);
}

private static int findDigitalRoot(int num) {


if (num < 10) return num;
int sum = 0;
while (num != 0) {
sum += num % 10;
num /= 10;
}
return findDigitalRoot(sum);
}
}

34.Program to find the smallest and largest digit in a number:


import java.util.Scanner;

public class SmallestLargestDigit {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
int smallest = findSmallestDigit(num);
int largest = findLargestDigit(num);
System.out.println("Smallest digit: " + smallest);
System.out.println("Largest digit: " + largest);
}

private static int findSmallestDigit(int num) {


int smallest = 9;
while (num != 0) {
int digit = num % 10;
smallest = Math.min(smallest, digit);
num /= 10;
}
return smallest;
}

private static int findLargestDigit(int num) {


int largest = 0;
while (num != 0) {
int digit = num % 10;
largest = Math.max(largest, digit);
num /= 10;
}
return largest;
}
}
35.Program to find the sum of squares of digits of a number:
import java.util.Scanner;

public class SumOfSquaresOfDigits {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
int sum = sumOfSquaresOfDigits(num);
System.out.println("Sum of squares of digits: " + sum);
}

private static int sumOfSquaresOfDigits(int num) {


int sum = 0;
while (num != 0) {
int digit = num % 10;
sum += digit * digit;
num /= 10;
}
return sum;
}
}

36.Program to check if a number is a Harshad (Niven) number:


import java.util.Scanner;

public class HarshadNumber {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
boolean isHarshad = isHarshadNumber(num);
if (isHarshad) {
System.out.println(num + " is a Harshad number.");
} else {
System.out.println(num + " is not a Harshad number.");
}
}

private static boolean isHarshadNumber(int num) {


int sumOfDigits = 0;
int temp = num;
while (temp != 0) {
sumOfDigits += temp % 10;
temp /= 10;
}
return num % sumOfDigits == 0;
}
}

37.Program to check if a number is a Kaprekar number:


import java.util.Scanner;

public class KaprekarNumber {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
if (isKaprekarNumber(num)) {
System.out.println(num + " is a Kaprekar number.");
} else {
System.out.println(num + " is not a Kaprekar number.");
}
}

private static boolean isKaprekarNumber(int num) {


long square = (long) num * num;
String squareStr = String.valueOf(square);
for (int i = 1; i < squareStr.length(); i++) {
String part1 = squareStr.substring(0, i);
String part2 = squareStr.substring(i);
int part1Num = part1.isEmpty() ? 0 : Integer.parseInt(part1);
int part2Num = part2.isEmpty() ? 0 : Integer.parseInt(part2);
if (part1Num + part2Num == num && part1Num > 0 && part2Num > 0) {
return true;
}
}
return false;
}
}

38.Program to find the largest palindrome made from the product of two n-digit numbers:
import java.util.Scanner;

public class LargestPalindromeProduct {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter number of digits: ");
int n = scanner.nextInt();
int largestPalindrome = findLargestPalindromeProduct(n);
System.out.println("Largest palindrome made from the product of two " + n + "-digit
numbers is: " + largestPalindrome);
}

private static int findLargestPalindromeProduct(int n) {


int maxNum = (int) Math.pow(10, n) - 1;
int minNum = (int) Math.pow(10, n - 1);
int largestPalindrome = 0;
for (int i = maxNum; i >= minNum; i--) {
for (int j = i; j >= minNum; j--) {
int product = i * j;
if (product < largestPalindrome) {
break;
}
if (isPalindrome(product)) {
largestPalindrome = product;
}
}
}
return largestPalindrome;
}

private static boolean isPalindrome(int num) {


String numStr = String.valueOf(num);
int left = 0, right = numStr.length() - 1;
while (left < right) {
if (numStr.charAt(left++) != numStr.charAt(right--)) {
return false;
}
}
return true;
}
}

39.Program to find the number of steps required to reach 1 in the Collatz sequence:
import java.util.Scanner;

public class CollatzSequence {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a positive integer: ");
int num = scanner.nextInt();
int steps = collatzSteps(num);
System.out.println("Number of steps required to reach 1 in the Collatz sequence: " +
steps);
}

private static int collatzSteps(int num) {


if (num <= 0) {
throw new IllegalArgumentException("Input must be a positive integer.");
}
int steps = 0;
while (num != 1) {
if (num % 2 == 0) {
num /= 2;
} else {
num = num * 3 + 1;
}
steps++;
}
return steps;
}
}

40.Program to find the sum of all amicable numbers below N:


import java.util.Scanner;

public class SumOfAmicableNumbers {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the value of N: ");
int n = scanner.nextInt();
int sum = sumOfAmicableNumbers(n);
System.out.println("Sum of amicable numbers below " + n + " is: " + sum);
}

private static int sumOfAmicableNumbers(int n) {


int sum = 0;
for (int i = 2; i < n; i++) {
int divisorSum = divisorSum(i);
if (divisorSum > i && divisorSum < n && divisorSum(i) == i && divisorSum(divisorSum)
== i) {
sum += i + divisorSum;
}
}
return sum;
}

private static int divisorSum(int num) {


int sum = 1;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
sum += i;
if (i != num / i) {
sum += num / i;
}
}
}
return sum;
}
}

41.Program to find the number of Hamming numbers up to N:


import java.util.Scanner;

public class HammingNumbers {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the value of N: ");
int n = scanner.nextInt();
int count = countHammingNumbers(n);
System.out.println("Number of Hamming numbers up to " + n + " is: " + count);
}

private static int countHammingNumbers(int n) {


int[] hamming = new int[n];
hamming[0] = 1;
int i2 = 0, i3 = 0, i5 = 0;
int nextMultipleOf2 = 2;
int nextMultipleOf3 = 3;
int nextMultipleOf5 = 5;
int nextHammingNumber = 1;
for (int i = 1; i < n; i++) {
nextHammingNumber = Math.min(nextMultipleOf2, Math.min(nextMultipleOf3,
nextMultipleOf5));
hamming[i] = nextHammingNumber;
if (nextHammingNumber == nextMultipleOf2) {
i2++;
nextMultipleOf2 = hamming[i2] * 2;
}
if (nextHammingNumber == nextMultipleOf3) {
i3++;
nextMultipleOf3 = hamming[i3] * 3;
}
if (nextHammingNumber == nextMultipleOf5) {
i5++;
nextMultipleOf5 = hamming[i5] * 5;
}
}
return nextHammingNumber;
}
}

42.Program to check if a number is a happy number:


import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class HappyNumber {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
if (isHappyNumber(num)) {
System.out.println(num + " is a happy number.");
} else {
System.out.println(num + " is not a happy number.");
}
}

private static boolean isHappyNumber(int num) {


Set<Integer> seen = new HashSet<>();
while (num != 1 && !seen.contains(num)) {
seen.add(num);
num = nextNumber(num);
}
return num == 1;
}
private static int nextNumber(int num) {
int sum = 0;
while (num != 0) {
int digit = num % 10;
sum += digit * digit;
num /= 10;
}
return sum;
}
}

43.Program to find the sum of all odd numbers between 1 and N:


import java.util.Scanner;

public class SumOfOddNumbers {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the value of N: ");
int n = scanner.nextInt();
int sum = 0;
for (int i = 1; i <= n; i += 2) {
sum += i;
}
System.out.println("Sum of odd numbers between 1 and " + n + ": " + sum);
}
}

44.Program to find the HCF (Highest Common Factor) of two numbers:


import java.util.Scanner;

public class HCF {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first number: ");
int num1 = scanner.nextInt();
System.out.print("Enter the second number: ");
int num2 = scanner.nextInt();
int hcf = findHCF(num1, num2);
System.out.println("HCF of " + num1 + " and " + num2 + " is: " + hcf);
}

private static int findHCF(int num1, int num2) {


while (num2 != 0) {
int temp = num2;
num2 = num1 % num2;
num1 = temp;
}
return num1;
}
}

45.Program to check if a number is a Smith number:


import java.util.Scanner;

public class SmithNumberCheck {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
if (isSmithNumber(num)) {
System.out.println(num + " is a Smith number.");
} else {
System.out.println(num + " is not a Smith number.");
}
}

private static boolean isSmithNumber(int num) {


int originalNum = num;
int sumOfDigits = 0;
for (int i = 2; i <= num / 2; i++) {
while (num % i == 0) {
sumOfDigits += sumOfDigits(i);
num /= i;
}
}
if (num > 1) {
sumOfDigits += sumOfDigits(num);
}
return sumOfDigits(originalNum) == sumOfDigits;
}

private static int sumOfDigits(int num) {


int sum = 0;
while (num > 0) {
sum += num % 10;
num /= 10;
}
return sum;
}
}

46.Program to check if a number is a Duck number:


import java.util.Scanner;

public class DuckNumberCheck {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
String numStr = scanner.nextLine();
if (isDuckNumber(numStr)) {
System.out.println(numStr + " is a Duck number.");
} else {
System.out.println(numStr + " is not a Duck number.");
}
}

private static boolean isDuckNumber(String numStr) {


if (numStr.startsWith("0")) {
return false;
}
for (int i = 1; i < numStr.length(); i++) {
if (numStr.charAt(i) == '0') {
return true;
}
}
return false;
}
}

47.Program to find the largest and smallest prime factors of a number:


import java.util.Scanner;

public class PrimeFactors {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
int largestPrimeFactor = findLargestPrimeFactor(num);
int smallestPrimeFactor = findSmallestPrimeFactor(num);
System.out.println("Largest prime factor of " + num + " is: " + largestPrimeFactor);
System.out.println("Smallest prime factor of " + num + " is: " + smallestPrimeFactor);
}

private static int findLargestPrimeFactor(int num) {


int largest = 0;
for (int i = 2; i <= num; i++) {
if (num % i == 0 && isPrime(i)) {
largest = i;
}
}
return largest;
}

private static int findSmallestPrimeFactor(int num) {


for (int i = 2; i <= num; i++) {
if (num % i == 0 && isPrime(i)) {
return i;
}
}
return num;
}

private static boolean isPrime(int num) {


if (num <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
}

48.Program to find the sum of all perfect numbers up to N:


import java.util.Scanner;

public class SumOfPerfectNumbers {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the value of N: ");
int n = scanner.nextInt();
int sum = 0;
for (int i = 2; i <= n; i++) {
if (isPerfectNumber(i)) {
sum += i;
}
}
System.out.println("Sum of perfect numbers up to " + n + " is: " + sum);
}

private static boolean isPerfectNumber(int num) {


int sum = 1; // Start with 1 because every number is divisible by 1
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
sum += i;
if (i * i != num) {
sum += num / i;
}
}
}
return sum == num;
}
}

49.Program to find the sum of all abundant numbers up to N:


import java.util.Scanner;

public class SumOfAbundantNumbers {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the value of N: ");
int n = scanner.nextInt();
int sum = 0;
for (int i = 1; i <= n; i++) {
if (isAbundantNumber(i)) {
sum += i;
}
}
System.out.println("Sum of abundant numbers up to " + n + " is: " + sum);
}

private static boolean isAbundantNumber(int num) {


int sum = 1; // Include 1 as divisor
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
sum += i;
if (i * i != num) {
sum += num / i;
}
}
}
return sum > num;
}
}
50.Program to find the sum of all composite numbers up to N:
import java.util.Scanner;

public class SumOfCompositeNumbers {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the value of N: ");
int n = scanner.nextInt();
int sum = 0;
for (int i = 4; i <= n; i++) {
if (isComposite(i)) {
sum += i;
}
}
System.out.println("Sum of composite numbers up to " + n + " is: " + sum);
}

private static boolean isComposite(int num) {


if (num <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return true;
}
}
return false;
}
}

51.Program to find the product of digits of a number:


import java.util.Scanner;

public class ProductOfDigits {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
int product = productOfDigits(num);
System.out.println("Product of digits of " + num + " is: " + product);
}

private static int productOfDigits(int num) {


int product = 1;
while (num != 0) {
int digit = num % 10;
product *= digit;
num /= 10;
}
return product;
}
}

52.Program to find the sum of all even Fibonacci numbers up to N:


import java.util.Scanner;

public class SumOfEvenFibonacci {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the value of N: ");
int n = scanner.nextInt();
int sum = 0;
int a = 1, b = 2;
while (b <= n) {
if (b % 2 == 0) {
sum += b;
}
int temp = a + b;
a = b;
b = temp;
}
System.out.println("Sum of even Fibonacci numbers up to " + n + " is: " + sum);
}
}
53.Program to find the sum of proper divisors of a number:
import java.util.Scanner;

public class SumOfDivisors {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
int sum = sumOfDivisors(num);
System.out.println("Sum of proper divisors of " + num + " is: " + sum);
}

private static int sumOfDivisors(int num) {


int sum = 0;
for (int i = 1; i < num; i++) {
if (num % i == 0) {
sum += i;
}
}
return sum;
}
}

54.Program to find the sum of proper divisors of a number:


import java.util.Scanner;

public class SumOfDivisors {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
int sum = sumOfDivisors(num);
System.out.println("Sum of proper divisors of " + num + " is: " + sum);
}

private static int sumOfDivisors(int num) {


int sum = 0;
for (int i = 1; i < num; i++) {
if (num % i == 0) {
sum += i;
}
}
return sum;
}
}
55.Program to find the sum of all narcissistic numbers up to N:
import java.util.Scanner;

public class SumOfNarcissisticNumbers {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the value of N: ");
int n = scanner.nextInt();
int sum = 0;
for (int i = 1; i <= n; i++) {
if (isNarcissistic(i)) {
sum += i;
}
}
System.out.println("Sum of narcissistic numbers up to " + n + " is: " + sum);
}

private static boolean isNarcissistic(int num) {


int sum = 0;
int originalNum = num;
int numDigits = (int) Math.floor(Math.log10(num)) + 1;
while (num > 0) {
int digit = num % 10;
sum += Math.pow(digit, numDigits);
num /= 10;
}
return sum == originalNum;
}
}

56.Program to find the sum of prime factors of a number:


import java.util.Scanner;

public class SumOfPrimeFactors {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
int sum = sumOfPrimeFactors(num);
System.out.println("Sum of prime factors of " + num + " is: " + sum);
}

private static int sumOfPrimeFactors(int num) {


int sum = 0;
for (int i = 2; i <= num; i++) {
while (num % i == 0 && isPrime(i)) {
sum += i;
num /= i;
}
}
return sum;
}

private static boolean isPrime(int num) {


if (num <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
}

57.Program to check if a number is a Pronic number:


import java.util.Scanner;

public class PronicNumberCheck {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
if (isPronicNumber(num)) {
System.out.println(num + " is a Pronic number.");
} else {
System.out.println(num + " is not a Pronic number.");
}
}

private static boolean isPronicNumber(int num) {


for (int i = 0; i <= Math.sqrt(num); i++) {
if (i * (i + 1) == num) {
return true;
}
}
return false;
}
}

58. Program to find the sum of first N prime numbers:


import java.util.Scanner;

public class SumOfFirstNPrimes {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the value of N: ");
int n = scanner.nextInt();
int sum = 0;
int count = 0;
int num = 2;
while (count < n) {
if (isPrime(num)) {
sum += num;
count++;
}
num++;
}
System.out.println("Sum of first " + n + " prime numbers is: " + sum);
}

private static boolean isPrime(int num) {


if (num <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
}

59.Program to find the sum of the series: 1 + 1/2 + 1/3 + ... + 1/N:
import java.util.Scanner;

public class HarmonicSeriesSum {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the value of N: ");
int n = scanner.nextInt();
double sum = 0;
for (int i = 1; i <= n; i++) {
sum += 1.0 / i;
}
System.out.println("Sum of harmonic series up to " + n + " terms is: " + sum);
}
}

60.Program to find the sum of the series: 1 - 1/2 + 1/3 - ... + (-1)^(N+1) * 1/N:
import java.util.Scanner;

public class AlternatingSeriesSum {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the value of N: ");
int n = scanner.nextInt();
double sum = 0;
for (int i = 1; i <= n; i++) {
sum += Math.pow(-1, i + 1) * 1.0 / i;
}
System.out.println("Sum of alternating series up to " + n + " terms is: " + sum);
}
}

61.Program to find the sum of the series: 1^2 + 2^2 + 3^2 + ... + N^2:
import java.util.Scanner;

public class SquareSeriesSum {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the value of N: ");
int n = scanner.nextInt();
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += i * i;
}
System.out.println("Sum of square series up to " + n + " terms is: " + sum);
}
}

62. Program to find the sum of digits of a number raised to the power of itself:
import java.util.Scanner;

public class SumOfDigitsPower {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
int powerSum = sumOfDigitsPower(num);
System.out.println("Sum of digits of " + num + " raised to the power of itself is: " +
powerSum);
}

private static int sumOfDigitsPower(int num) {


int sum = 0;
int originalNum = num;
while (num != 0) {
int digit = num % 10;
sum += Math.pow(digit, digit);
num /= 10;
}
return sum;
}
}

63.Program to find the sum of even digits and odd digits of a number separately:
import java.util.Scanner;

public class SumOfEvenOddDigits {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
int sumEven = 0, sumOdd = 0;
while (num != 0) {
int digit = num % 10;
if (digit % 2 == 0) {
sumEven += digit;
} else {
sumOdd += digit;
}
num /= 10;
}
System.out.println("Sum of even digits: " + sumEven);
System.out.println("Sum of odd digits: " + sumOdd);
}
}

64.Program to check if a number is a Disarium number:


import java.util.Scanner;

public class DisariumNumberCheck {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
if (isDisarium(num)) {
System.out.println(num + " is a Disarium number.");
} else {
System.out.println(num + " is not a Disarium number.");
}
}

private static boolean isDisarium(int num) {


int sum = 0;
int temp = num;
int count = countDigits(num);
while (temp != 0) {
int digit = temp % 10;
sum += Math.pow(digit, count);
count--;
temp /= 10;
}
return sum == num;
}

private static int countDigits(int num) {


int count = 0;
while (num != 0) {
count++;
num /= 10;
}
return count;
}
}

65.Program to find the sum of Armstrong numbers in a range:


import java.util.Scanner;

public class SumOfArmstrongNumbers {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the starting number: ");
int start = scanner.nextInt();
System.out.print("Enter the ending number: ");
int end = scanner.nextInt();
int sum = 0;
for (int i = start; i <= end; i++) {
if (isArmstrong(i)) {
sum += i;
}
}
System.out.println("Sum of Armstrong numbers between " + start + " and " + end + " is: " +
sum);
}

private static boolean isArmstrong(int num) {


int originalNum = num;
int sum = 0;
int digits = countDigits(num);
while (num != 0) {
int digit = num % 10;
sum += Math.pow(digit, digits);
num /= 10;
}
return sum == originalNum;
}

private static int countDigits(int num) {


int count = 0;
while (num != 0) {
count++;
num /= 10;
}
return count;
}
}

66.Program to check if a number is a Spy number:


import java.util.Scanner;

public class SpyNumberCheck {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
if (isSpyNumber(num)) {
System.out.println(num + " is a Spy number.");
} else {
System.out.println(num + " is not a Spy number.");
}
}

private static boolean isSpyNumber(int num) {


int sumOfDigits = 0;
int productOfDigits = 1;
while (num != 0) {
int digit = num % 10;
sumOfDigits += digit;
productOfDigits *= digit;
num /= 10;
}
return sumOfDigits == productOfDigits;
}
}

67.Program to check if a number is a Neon number:


import java.util.Scanner;

public class NeonNumberCheck {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
if (isNeonNumber(num)) {
System.out.println(num + " is a Neon number.");
} else {
System.out.println(num + " is not a Neon number.");
}
}

private static boolean isNeonNumber(int num) {


int square = num * num;
int sum = 0;
while (square != 0) {
sum += square % 10;
square /= 10;
}
return sum == num;
}
}

68.Program to find the sum of Fibonacci numbers up to N terms:


import java.util.Scanner;

public class SumOfFibonacci {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the value of N: ");
int n = scanner.nextInt();
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += fibonacci(i);
}
System.out.println("Sum of Fibonacci numbers up to " + n + " terms is: " + sum);
}

private static int fibonacci(int n) {


if (n <= 1) {
return n;
}
int fib = 1, prevFib = 1;
for (int i = 2; i < n; i++) {
int temp = fib;
fib += prevFib;
prevFib = temp;
}
return fib;
}
}
69.Program to check if a number is a Keith number:
import java.util.Scanner;

public class KeithNumberCheck {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
if (isKeithNumber(num)) {
System.out.println(num + " is a Keith number.");
} else {
System.out.println(num + " is not a Keith number.");
}
}

private static boolean isKeithNumber(int num) {


String numStr = String.valueOf(num);
int[] sequence = new int[numStr.length() * 2];
for (int i = 0; i < numStr.length(); i++) {
sequence[i] = Character.getNumericValue(numStr.charAt(i));
}
int i = numStr.length(), sum = 0;
while (sum < num) {
sum = 0;
for (int j = 0; j < numStr.length(); j++) {
sum += sequence[i - numStr.length() + j];
sequence[i + j] = sum;
}
i++;
}
return sum == num;
}
}

70.Program to find the nth term of the Fibonacci series using recursion:
import java.util.Scanner;

public class FibonacciRecursive {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the value of n: ");
int n = scanner.nextInt();
System.out.println("The " + n + "th term of the Fibonacci series is: " + fibonacci(n));
}

private static int fibonacci(int n) {


if (n <= 1) {
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}
}

71.Program to find the sum of the series: 1 + 1/3 + 1/5 + ... + 1/(2n-1):
import java.util.Scanner;

public class SeriesSum {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the value of n: ");
int n = scanner.nextInt();
double sum = seriesSum(n);
System.out.println("Sum of the series is: " + sum);
}

private static double seriesSum(int n) {


double sum = 0;
for (int i = 1; i <= n; i++) {
sum += 1.0 / (2 * i - 1);
}
return sum;
}
}

72.Program to find the sum of natural numbers using recursion:


import java.util.Scanner;

public class SumOfNaturalNumbersRecursive {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a positive integer: ");
int n = scanner.nextInt();
if (n < 0) {
System.out.println("Please enter a positive integer.");
} else {
int sum = sumOfNaturalNumbers(n);
System.out.println("Sum of first " + n + " natural numbers is: " + sum);
}
}

private static int sumOfNaturalNumbers(int n) {


if (n == 1) {
return 1;
}
return n + sumOfNaturalNumbers(n - 1);
}
}

73.Program to check if a number is a Smith number:


import java.util.Scanner;

public class SmithNumberCheck {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
if (isSmithNumber(num)) {
System.out.println(num + " is a Smith number.");
} else {
System.out.println(num + " is not a Smith number.");
}
}

private static boolean isSmithNumber(int num) {


int sumOfDigits = sumOfDigits(num);
int sumOfPrimeFactors = sumOfPrimeFactors(num);
return sumOfDigits == sumOfPrimeFactors;
}

private static int sumOfDigits(int num) {


int sum = 0;
while (num != 0) {
sum += num % 10;
num /= 10;
}
return sum;
}

private static int sumOfPrimeFactors(int num) {


int sum = 0;
int temp = num;
for (int i = 2; i <= num / 2; i++) {
while (temp % i == 0) {
sum += sumOfDigits(i);
temp /= i;
}
}
if (temp != 1) {
sum += sumOfDigits(temp);
}
return sum;
}
}

74.Program to check if a number is a Krishnamurthy number:


import java.util.Scanner;

public class KrishnamurthyNumberCheck {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
if (isKrishnamurthy(num)) {
System.out.println(num + " is a Krishnamurthy number.");
} else {
System.out.println(num + " is not a Krishnamurthy number.");
}
}

private static boolean isKrishnamurthy(int num) {


int originalNum = num;
int sum = 0;
while (num != 0) {
int digit = num % 10;
sum += factorial(digit);
num /= 10;
}
return sum == originalNum;
}

private static int factorial(int num) {


if (num <= 1) {
return 1;
}
int fact = 1;
for (int i = 2; i <= num; i++) {
fact *= i;
}
return fact;
}
}

75.Program to find the sum of first N natural numbers divisible by 5:


import java.util.Scanner;

public class SumOfNaturalNumbersDivisibleBy5 {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the value of N: ");
int n = scanner.nextInt();
int sum = sumOfNaturalNumbersDivisibleBy5(n);
System.out.println("Sum of first " + n + " natural numbers divisible by 5 is: " + sum);
}

private static int sumOfNaturalNumbersDivisibleBy5(int n) {


return 5 * n * (n + 1) / 2;
}
}

76.Program to find the sum of even Fibonacci numbers up to N terms:


import java.util.Scanner;

public class SumOfEvenFibonacci {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the value of N: ");
int n = scanner.nextInt();
long sum = sumOfEvenFibonacci(n);
System.out.println("Sum of even Fibonacci numbers up to " + n + " terms is: " + sum);
}

private static long sumOfEvenFibonacci(int n) {


long sum = 0;
long fib1 = 1, fib2 = 1;
while (fib1 <= n) {
if (fib1 % 2 == 0) {
sum += fib1;
}
long nextFib = fib1 + fib2;
fib1 = fib2;
fib2 = nextFib;
}
return sum;
}
}

77.Program to find the sum of cubes of individual digits of a number:


import java.util.Scanner;

public class SumOfDigitCubes {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
int sum = sumOfDigitCubes(num);
System.out.println("Sum of cubes of digits of " + num + " is: " + sum);
}

private static int sumOfDigitCubes(int num) {


int sum = 0;
while (num != 0) {
int digit = num % 10;
sum += digit * digit * digit;
num /= 10;
}
return sum;
}
}

78.Program to find the sum of first N natural numbers divisible by 3 or 5:


import java.util.Scanner;

public class SumOfNaturalNumbersDivisibleBy3Or5 {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the value of N: ");
int n = scanner.nextInt();
int sum = sumOfNaturalNumbersDivisibleBy3Or5(n);
System.out.println("Sum of first " + n + " natural numbers divisible by 3 or 5 is: " + sum);
}

private static int sumOfNaturalNumbersDivisibleBy3Or5(int n) {


int sum = 0;
for (int i = 1; i <= n; i++) {
if (i % 3 == 0 || i % 5 == 0) {
sum += i;
}
}
return sum;
}
}

79.Program to find the sum of odd Fibonacci numbers up to N terms:


import java.util.Scanner;

public class SumOfOddFibonacci {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the value of N: ");
int n = scanner.nextInt();
long sum = sumOfOddFibonacci(n);
System.out.println("Sum of odd Fibonacci numbers up to " + n + " terms is: " + sum);
}

private static long sumOfOddFibonacci(int n) {


long sum = 0;
long fib1 = 1, fib2 = 1;
while (fib1 <= n) {
if (fib1 % 2 != 0) {
sum += fib1;
}
long nextFib = fib1 + fib2;
fib1 = fib2;
fib2 = nextFib;
}
return sum;
}
}

80.Program to find the sum of the series: 1/1! + 2/2! + 3/3! + ... + N/N!:
import java.util.Scanner;
public class SeriesSumFactorial {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the value of N: ");
int n = scanner.nextInt();
double sum = seriesSumFactorial(n);
System.out.println("Sum of the series is: " + sum);
}

private static double seriesSumFactorial(int n) {


double sum = 0;
for (int i = 1; i <= n; i++) {
sum += (double) i / factorial(i);
}
return sum;
}

private static int factorial(int n) {


if (n == 0 || n == 1) {
return 1;
}
return n * factorial(n - 1);
}
}

81.Program to find the sum of the digits of a number in a given base:


import java.util.Scanner;

public class SumOfDigitsInBase {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
String num = scanner.next();
System.out.print("Enter the base: ");
int base = scanner.nextInt();
int sum = sumOfDigitsInBase(num, base);
System.out.println("Sum of digits of " + num + " in base " + base + " is: " + sum);
}

private static int sumOfDigitsInBase(String num, int base) {


int sum = 0;
for (int i = 0; i < num.length(); i++) {
char digit = num.charAt(i);
if (Character.isDigit(digit)) {
sum += Character.getNumericValue(digit);
} else {
sum += digit - 'A' + 10;
}
}
return sum;
}
}

82.Program to find the sum of the series: 1 + 11 + 111 + ... up to N terms:


import java.util.Scanner;

public class SeriesSumOnes {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the value of N: ");
int n = scanner.nextInt();
int sum = seriesSumOnes(n);
System.out.println("Sum of the series is: " + sum);
}

private static int seriesSumOnes(int n) {


int sum = 0;
int term = 0;
for (int i = 1; i <= n; i++) {
term = term * 10 + 1;
sum += term;
}
return sum;
}
}

83.Program to check if a number is a Triangular number:


import java.util.Scanner;

public class TriangularNumberCheck {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
if (isTriangularNumber(num)) {
System.out.println(num + " is a Triangular number.");
} else {
System.out.println(num + " is not a Triangular number.");
}
}

private static boolean isTriangularNumber(int num) {


int sum = 0;
int n = 1;
while (sum < num) {
sum += n;
if (sum == num) {
return true;
}
n++;
}
return false;
}
}

84.Program to check if a number is a Vampire number:


import java.util.Arrays;
import java.util.Scanner;

public class VampireNumberCheck {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
if (isVampireNumber(num)) {
System.out.println(num + " is a Vampire number.");
} else {
System.out.println(num + " is not a Vampire number.");
}
}

private static boolean isVampireNumber(int num) {


String numStr = String.valueOf(num);
int length = numStr.length();
if (length % 2 != 0) {
return false;
}
for (int i = 10; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
String factor1 = String.valueOf(i);
String factor2 = String.valueOf(num / i);
if (isPermutation(numStr, factor1 + factor2) || isPermutation(numStr, factor2 + factor1))
{
return true;
}
}
}
return false;
}

private static boolean isPermutation(String str1, String str2) {


char[] charArray1 = str1.toCharArray();
char[] charArray2 = str2.toCharArray();
Arrays.sort(charArray1);
Arrays.sort(charArray2);
return Arrays.equals(charArray1, charArray2);
}
}

85.Program to check if a number is a Happy prime number:


import java.util.Scanner;

public class HappyPrimeCheck {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
if (isHappyPrime(num)) {
System.out.println(num + " is a Happy prime number.");
} else {
System.out.println(num + " is not a Happy prime number.");
}
}

private static boolean isHappyPrime(int num) {


return isHappy(num) && isPrime(num);
}

private static boolean isHappy(int num) {


while (num != 1 && num != 4) {
int sum = 0;
while (num != 0) {
int digit = num % 10;
sum += digit * digit;
num /= 10;
}
num = sum;
}
return num == 1;
}

private static boolean isPrime(int num) {


if (num <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
}

86.Program to find the Armstrong numbers between two given numbers:


import java.util.Scanner;

public class ArmstrongNumbersInRange {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the lower bound of the range: ");
int lowerBound = scanner.nextInt();
System.out.print("Enter the upper bound of the range: ");
int upperBound = scanner.nextInt();
System.out.println("Armstrong numbers between " + lowerBound + " and " + upperBound +
" are:");
for (int i = lowerBound; i <= upperBound; i++) {
if (isArmstrong(i)) {
System.out.print(i + " ");
}
}
}

private static boolean isArmstrong(int num) {


int originalNum = num;
int digits = numberOfDigits(num);
int sum = 0;
while (num != 0) {
int digit = num % 10;
sum += Math.pow(digit, digits);
num /= 10;
}
return sum == originalNum;
}

private static int numberOfDigits(int num) {


int count = 0;
while (num != 0) {
count++;
num /= 10;
}
return count;
}
}

87.Program to check if a number is a Disarium prime number:


import java.util.Scanner;

public class DisariumPrimeCheck {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
if (isDisariumPrime(num)) {
System.out.println(num + " is a Disarium prime number.");
} else {
System.out.println(num + " is not a Disarium prime number.");
}
}

private static boolean isDisariumPrime(int num) {


return isDisarium(num) && isPrime(num);
}

private static boolean isDisarium(int num) {


int sum = 0;
int temp = num;
int length = String.valueOf(num).length();
while (temp > 0) {
int digit = temp % 10;
sum += Math.pow(digit, length);
temp /= 10;
length--;
}
return sum == num;
}

private static boolean isPrime(int num) {


if (num <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
}

88.Program to find the sum of the squares of first N natural numbers:


import java.util.Scanner;

public class SumOfSquares {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the value of N: ");
int n = scanner.nextInt();
int sum = sumOfSquares(n);
System.out.println("Sum of squares of first " + n + " natural numbers is: " + sum);
}

private static int sumOfSquares(int n) {


int sum = 0;
for (int i = 1; i <= n; i++) {
sum += i * i;
}
return sum;
}
}
89.Program to find the Automorphic Number Program in Java:
import java.util.Scanner;

public class AutomorphicNumber


{
public static void main(String[] args)
{
int n, sqrNum, temp,sqrNumRemainder,c = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
n = sc.nextInt();
temp=n;
while (temp > 0)
{
temp=temp/10;
c++;
}
sqrNum = n * n;
sqrNumRemainder= sqrNum%(int)Math.pow(10, c);
if(sqrNumRemainder==n)
{
System.out.println("Automorphic Number");
}
else
{
System.out.println("Not Automorphic Number");
}

}
}

90.Buzz Number Program in Java


import java.util.Scanner;
public class BuzzNumber
{
public static void main(String[] args)
{
int n;
Scanner sc = new Scanner(System.in);
System.out.print("Enter n=");
n = sc.nextInt();
if (n % 10 == 7 || n % 7 == 0)
{
System.out.println("Buzz number");
}
else
{
System.out.println("Not Buzz number");
}
}
}

91.Circular Prime Program in Java.


import java.util.Scanner;

public class CircularPrime


{
public static void main(String[] args)
{
boolean flag = true;
int n, num, r, c = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
n = sc.nextInt();
num = n;
while (num > 0)
{
r = num % 10;
c++;
num = num / 10;
}
num = n;
for (int i = 1; i <= c; i++)
{
r = num % 10;
num = num / 10;
num = (r * (int) Math.pow(10, c - 1)) + num;
if (!prime(num))
{
flag = false;
break;
}
}
if(flag)
{
System.out.println("Circular Prime");
}
else
{
System.out.println("Not Circular Prime");
}

}
static boolean prime(int n)
{
// TODO code application logic here
int i = 2;
boolean flag = true;
while (n > i)
{
if (n % 2 == 0)
{
flag = false;
break;
}
i++;
}
return flag;
}
}

92.CoPrime Numbers Program in Java


import java.util.Scanner;
public class CoPrimeNumbers
{
public static void main(String[] args)
{
int a, b, gcd = 1;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a=");
a = sc.nextInt();
System.out.print("Enter b=");
b = sc.nextInt();
int min, max;
min = a;
if (min > b)
{
min = b;
max = a;
}
else
{
min = a;
max = b;
}
while (max > min)
{
int r = max % min;
if (r == 0)
{
gcd = min;
break;
}
else
{
max = min;
min = r;
}
}
if (gcd == 1)
{
System.out.println("Co Prime Numbers");
}
else
{
System.out.println("Not Co Prime Numbers");
}
}
}

93.Evil Number Program in Java


import java.util.Scanner;
public class EvilNumber {
public static void main(String[] args)
{
int n,num;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
n = sc.nextInt();
int binaryDigits=0;
String binary = "";
num=n;
while (num > 0)
{
binary= num % 2 + binary ;
if(num%2==1)
{
binaryDigits++;
}
num = num / 2;
}
if(binaryDigits%2==0)
{
System.out.println("Binary of "+n+"="+binary);
System.out.println("Evil Number");
}
else
{
System.out.println("Binary of "+n+"="+binary);
System.out.println("Not Evil Number");
}

}
}

94.Factors Program in Java


import java.util.Scanner;

public class Factors


{
public static void main(String[] args)
{
int n;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
n = sc.nextInt();
for (int i = 1; i <= n; i++)
{
if (n % i == 0)
{
System.out.println(i);
}
}
}
}

95.Fascinating Number Program in Java


import java.util.Scanner;
public class FascinatingNumber
{

public static void main(String args[])


{
int num, num2, num3;
Scanner sc = new Scanner(System.in);
System.out.print("Enter any Number: ");
num = sc.nextInt();
num2 = num * 2;
num3 = num * 3;
String concatNumbers = num + "" + num2 +""+ num3;
System.out.println("Concated Number="+concatNumbers);
boolean flag = true;
for (char digit = '1'; digit <= '9'; digit++)
{
int count = 0;
for (int i = 0; i < concatNumbers.length(); i++)
{
char ch = concatNumbers.charAt(i);
if (ch == digit)
{
count++;
}
}
if (count > 1 || count == 0)
{
flag = false;
break;
}
}
if (flag)
{
System.out.println("Fascinating Number.");
} else
{
System.out.println("Not a Fascinating number.");
}
}
}

96.Magic Number Program in Java


import java.util.Scanner;

public class MagicNumber


{
public static void main(String[] args)
{
int n, r = 1, num, sum = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
n = sc.nextInt();
num = n;
while (num > 9)
{
while (num > 0)
{
r = num % 10;
sum = sum + r;
num = num / 10;
}
num = sum;
sum = 0;
}
if (num == 1)
{
System.out.println("Magic Number");
}
else
{
System.out.println("Not Magic Number");
}
}
}

97.Multiply Of Digit Program in Java


import java.util.Scanner;

public class MultiplyOfDigit


{
public static void main(String[] args)
{
// TODO code application logic here
int r, n, num,
mul = 1;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
n = sc.nextInt();
num = n;
while (num > 0)
{
r = num % 10;
mul = mul * r;
num = num / 10;
}
System.out.println("Multiply of digit=" + mul);
}
}

98.Tech Number Program in Java


import java.util.Scanner;

public class TechNumber


{
public static void main(String[] args)
{
// TODO code application logic here
int n, num, leftNumber, rightNumber, digits = 0,
sumSquare = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
n = sc.nextInt();
num = n;
while (num > 0)
{
digits++;
num = num / 10;
}
if (digits % 2 == 0)
{
num = n;
leftNumber = num % (int) Math.pow(10, digits / 2);
rightNumber = num / (int) Math.pow(10, digits / 2);

sumSquare = (leftNumber + rightNumber) * (leftNumber + rightNumber);


if (n == sumSquare)
{
System.out.println("Tech Number");
}
else
{
System.out.println("Not Tech Number");
}
}
else
{
System.out.println("Not Tech Number");
}
}
}

99.Twin Prime Program in Java


import java.util.Scanner;

public class TwinPrime


{
public static void main(String[] args)
{
int a, b;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a=");
a = sc.nextInt();
System.out.print("Enter b=");
b = sc.nextInt();
if (prime(a) && prime(b) && (Math.abs(a - b) == 2))
{
System.out.println("Twin Prime");
}
else
{
System.out.println("Not Twin Prime");
}
}
static boolean prime(int n)
{
// TODO code application logic here
int i = 2;
boolean flag = true;
while (n > i)
{
if (n % 2 == 0)
{
flag = false;
break;
}
i++;
}
return flag;
}
}

100.Twisted Prime Program in Java


import java.util.Scanner;

public class TwistedPrime


{
public static void main(String[] args)
{
// TODO code application logic here
int n, num, r,
rev = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
n = sc.nextInt();
num = n;
while (num > 0)
{
r = num % 10;
rev = (rev * 10) + r;
num = num / 10;
}
if (prime(n) && prime(rev))
{
System.out.println("Twisted Prime");
}
else
{
System.out.println("Not Twisted Prime");
}
}
static boolean prime(int n)
{
// TODO code application logic here
int i = 2;
boolean flag = true;
while (n > i)
{
if (n % 2 == 0)
{
flag = false;
break;
}
i++;
}
return flag;
}
}

101.Ugly Number Program in Java


import java.util.Scanner;

public class UglyNumber


{
public static void main(String[] args)
{
int n;
boolean flag=true;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
n = sc.nextInt();
while (n != 1)
{
if (n % 5 == 0)
{
n /= 5;
}
else if (n % 3 == 0)
{
n /= 3;
}
else if (n % 2 == 0)
{
n /= 2;
}
else
{
flag=false;
break;
}
}
if (flag)
{
System.out.println("Ugly number.");
}
else
{
System.out.println("Not Ugly number.");
}
}
}

102.Unique Number Program in Java


import java.util.Scanner;

public class UniqueNumber


{
public static void main(String[] args)
{
// TODO code application logic here
int r1, r2, n, num1, num2, c = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
n = sc.nextInt();
num1 = n;
num2 = n;
while (num1 > 0)
{
r1 = num1 % 10;
while (num2 > 0)
{
r2 = num2 % 10;
if (r1 == r2)
{
c++;
}
num2 = num2 / 10;
}
num1 = num1 / 10;
}
if (c == 1)
{
System.out.println("Unique Number");
}
else
{
System.out.println("Not Unique Number");
}
}
}

103.Java Program Number to Word


class NumberToWordExample1
{
//user-defined static method that converts a number into words
static void numberToWords(char num[])
{
//determines the number of digits in the given number
int len = num.length;
//checks the given number has number or not
if (len == 0)
{
//if the given number is empty prints the following statement
System.out.println("The string is empty.");
return;
}
//here, we have specified the length of the number to 4
//it means that the number (that you want to convert) should be four or less than four digits
if (len > 4)
{
//if the given number is more than four-digit number, it prints the following statement
System.out.println("\n The given number has more than 4 digits.");
return;
}
//string type array for one-digit numbers
String[] onedigit = new String[] {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven",
"Eight", "Nine"};
//string type array for two digits numbers
//the first index is empty because it makes indexing easy
String[] twodigits = new String[] {"", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen",
"Sixteen", "Seventeen", "Eighteen", "Nineteen"};
//string type array of tens multiples
//the first two indexes are empty because it makes indexing easy
String[] multipleoftens = new String[] {"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty",
"Seventy", "Eighty", "Ninety"};
//string type array of power of tens
String[] poweroftens = new String[] {"Hundred", "Thousand"};
//Used for debugging purpose only
//the valueOf() method returns the string representation of the character array argument
System.out.print(String.valueOf(num) + ": ");
//checks whether the length of the given string is one or not
if (len == 1)
{
//if the above condition returns true, it accesses the corresponding index and prints the value
of that index
//[num[0]-'0']: getting the number equal the decimal value of the character (assuming the char
is the digit)
System.out.println(onedigit[num[0]-'0']);
return;
}
int x = 0;
//executes until num does not become not '\0'
while (x < num.length)
{
//executes if the length of the string is greater than equal to three
if (len >= 3)
{
if (num[x] - '0' != 0)
{
System.out.print(onedigit[num[x] - '0'] + " ");
//here length can be 3 or 4
System.out.print(poweroftens[len - 3]+ " ");
}
//decrements the length of the string by 1
--len;
}
//executes if the given number has two digits
else
{
//the if-statement handles the numbers from 10 to 19 only
if (num[x] - '0' == 1)
{
//adding the digits of the given number
//the logic behind sum up the digits is that we will use the sum for accessing the index of the
array
//for example: 17, sum of digits = 8
//we will access the 8th index in twodigits[] array i.e. Seventeen
int sum = num[x] - '0' + num[x + 1] - '0';
System.out.println(twodigits[sum]);
return;
}
//the else-if statement handles the number 20 only
//compares the tens and unit place with 2 and 0 respectively
else if (num[x] - '0' == 2 && num[x + 1] - '0' == 0)
{
//executes if the above else-if condition returns true
System.out.println("Twenty");
return;
}
//the else block handles the numbers from 21 to 100
else
{
int i = (num[x] - '0');
if (i > 0)
//prints the ith index element of the array multipleoftens[]
System.out.print(multipleoftens[i]+ " ");
else
//prints space
System.out.print("");
//increments the variable i by 1
++x;
//checks whether the number is not equal to zero, it means the number has only a digit
if (num[x] - '0' != 0)
//prints the ith index element of the array onedigit[]
System.out.println(onedigit[num[x] - '0']);
}
}
//increments the variable i by 1
++x;
}
}
//main() method
public static void main(String args[])
{
//calling the user-defined method and that invokes another predefined method toCharArray()
//the method toCharArray() converts the given number into character array
numberToWords("1111".toCharArray());
numberToWords("673".toCharArray());
numberToWords("85".toCharArray());
numberToWords("5".toCharArray());
numberToWords("0".toCharArray());
numberToWords("20".toCharArray());
numberToWords("1000".toCharArray());
numberToWords("12345".toCharArray());
//passing empty string
numberToWords("".toCharArray());
}
}

104.Converting Large Numbers into Words


import java.text.DecimalFormat;
public class NumberToWordExample2
{
//string type array for one digit numbers
private static final String[] twodigits = {"", " Ten", " Twenty", " Thirty", " Forty", " Fifty", " Sixty", "
Seventy", " Eighty", " Ninety"};
//string type array for two digits numbers
private static final String[] onedigit = {"", " One", " Two", " Three", " Four", " Five", " Six", "
Seven", " Eight", " Nine", " Ten", " Eleven", " Twelve", " Thirteen", " Fourteen", " Fifteen", "
Sixteen", " Seventeen", " Eighteen", " Nineteen"};
//defining constructor of the class
private NumberToWordExample2()
{
}
//user-defined method that converts a number to words (up to 1000)
private static String convertUptoThousand(int number)
{
String soFar;
if (number % 100 < 20)
{
soFar = onedigit[number % 100];
number = number/ 100;
}
else
{
soFar = onedigit[number % 10];
number = number/ 10;
soFar = twodigits[number % 10] + soFar;
number = number/ 10;
}
if (number == 0)
return soFar;
return onedigit[number] + " Hundred " + soFar;
}
//user-defined method that converts a long number (0 to 999999999) to string
public static String convertNumberToWord(long number)
{
//checks whether the number is zero or not
if (number == 0)
{
//if the given number is zero it returns zero
return "zero";
}
//the toString() method returns a String object that represents the specified long
String num = Long.toString(number);
//for creating a mask padding with "0"
String pattern = "000000000000";
//creates a DecimalFormat using the specified pattern and also provides the symbols for the
default locale
DecimalFormat decimalFormat = new DecimalFormat(pattern);
//format a number of the DecimalFormat instance
num = decimalFormat.format(number);
//format: XXXnnnnnnnnn
//the subString() method returns a new string that is a substring of this string
//the substring begins at the specified beginIndex and extends to the character at index
endIndex - 1
//the parseInt() method converts the string into integer
int billions = Integer.parseInt(num.substring(0,3));
//format: nnnXXXnnnnnn
int millions = Integer.parseInt(num.substring(3,6));
//format: nnnnnnXXXnnn
int hundredThousands = Integer.parseInt(num.substring(6,9));
//format: nnnnnnnnnXXX
int thousands = Integer.parseInt(num.substring(9,12));
String tradBillions;
switch (billions)
{
case 0:
tradBillions = "";
break;
case 1 :
tradBillions = convertUptoThousand(billions)+ " Billion ";
break;
default :
tradBillions = convertUptoThousand(billions)+ " Billion ";
}
String result = tradBillions;
String tradMillions;
switch (millions)
{
case 0:
tradMillions = "";
break;
case 1 :
tradMillions = convertUptoThousand(millions)+ " Million ";
break;
default :
tradMillions = convertUptoThousand(millions)+ " Million ";
}
result = result + tradMillions;
String tradHundredThousands;
switch (hundredThousands)
{
case 0:
tradHundredThousands = "";
break;
case 1 :
tradHundredThousands = "One Thousand ";
break;
default :
tradHundredThousands = convertUptoThousand(hundredThousands)+ " Thousand ";
}
result = result + tradHundredThousands;
String tradThousand;
tradThousand = convertUptoThousand(thousands);
result = result + tradThousand;
//removing extra space if any
return result.replaceAll("^\\s+", "").replaceAll("\\b\\s{2,}\\b", " ");
}
//main() method
public static void main(String args[])
{
//calling the user-defined method that converts the parsed number into words
System.out.println(convertNumberToWord(2));
System.out.println(convertNumberToWord(99));
System.out.println(convertNumberToWord(456));
System.out.println(convertNumberToWord(1101));
System.out.println(convertNumberToWord(19812));
System.out.println(convertNumberToWord(674319));
System.out.println(convertNumberToWord(909087531));
System.out.println(convertNumberToWord(1000000000));
System.out.println(convertNumberToWord(359999999));
System.out.println(convertNumberToWord(1213000000L));
System.out.println(convertNumberToWord(1000000));
System.out.println(convertNumberToWord(1111111111));
System.out.println(convertNumberToWord(3000200));
System.out.println(convertNumberToWord(700000));
System.out.println(convertNumberToWord(9000000));
}
}

105.Converting Very Long Numbers into Words


import java.util.*;
public class NumberToWordExample3
{
static public class ScaleUnit
{
private int exponent;
private String[] names;
private ScaleUnit(int exponent, String...names)
{
this.exponent = exponent;
this.names = names;
}
public int getExponent()
{
return exponent;
}
public String getName(int index)
{
return names[index];
}
}
static private ScaleUnit[] SCALE_UNITS = new ScaleUnit[]
{
new ScaleUnit(63, "vigintillion", "decilliard"),
new ScaleUnit(60, "novemdecillion", "decillion"),
new ScaleUnit(57, "octodecillion", "nonilliard"),
new ScaleUnit(54, "septendecillion", "nonillion"),
new ScaleUnit(51, "sexdecillion", "octilliard"),
new ScaleUnit(48, "quindecillion", "octillion"),
new ScaleUnit(45, "quattuordecillion", "septilliard"),
new ScaleUnit(42, "tredecillion", "septillion"),
new ScaleUnit(39, "duodecillion", "sextilliard"),
new ScaleUnit(36, "undecillion", "sextillion"),
new ScaleUnit(33, "decillion", "quintilliard"),
new ScaleUnit(30, "nonillion", "quintillion"),
new ScaleUnit(27, "octillion", "quadrilliard"),
new ScaleUnit(24, "septillion", "quadrillion"),
new ScaleUnit(21, "sextillion", "trilliard"),
new ScaleUnit(18, "quintillion", "trillion"),
new ScaleUnit(15, "quadrillion", "billiard"),
new ScaleUnit(12, "trillion", "billion"),
new ScaleUnit(9, "billion", "milliard"),
new ScaleUnit(6, "million", "million"),
new ScaleUnit(3, "thousand", "thousand"),
new ScaleUnit(2, "hundred", "hundred"),
//new ScaleUnit(1, "ten", "ten"),
//new ScaleUnit(0, "one", "one"),
new ScaleUnit(-1, "tenth", "tenth"),
new ScaleUnit(-2, "hundredth", "hundredth"),
new ScaleUnit(-3, "thousandth", "thousandth"),
new ScaleUnit(-4, "ten-thousandth", "ten-thousandth"),
new ScaleUnit(-5, "hundred-thousandth", "hundred-thousandth"),
new ScaleUnit(-6, "millionth", "millionth"),
new ScaleUnit(-7, "ten-millionth", "ten-millionth"),
new ScaleUnit(-8, "hundred-millionth", "hundred-millionth"),
new ScaleUnit(-9, "billionth", "milliardth"),
new ScaleUnit(-10, "ten-billionth", "ten-milliardth"),
new ScaleUnit(-11, "hundred-billionth", "hundred-milliardth"),
new ScaleUnit(-12, "trillionth", "billionth"),
new ScaleUnit(-13, "ten-trillionth", "ten-billionth"),
new ScaleUnit(-14, "hundred-trillionth", "hundred-billionth"),
new ScaleUnit(-15, "quadrillionth", "billiardth"),
new ScaleUnit(-16, "ten-quadrillionth", "ten-billiardth"),
new ScaleUnit(-17, "hundred-quadrillionth", "hundred-billiardth"),
new ScaleUnit(-18, "quintillionth", "trillionth"),
new ScaleUnit(-19, "ten-quintillionth", "ten-trillionth"),
new ScaleUnit(-20, "hundred-quintillionth", "hundred-trillionth"),
new ScaleUnit(-21, "sextillionth", "trilliardth"),
new ScaleUnit(-22, "ten-sextillionth", "ten-trilliardth"),
new ScaleUnit(-23, "hundred-sextillionth", "hundred-trilliardth"),
new ScaleUnit(-24, "septillionth","quadrillionth"),
new ScaleUnit(-25, "ten-septillionth","ten-quadrillionth"),
new ScaleUnit(-26, "hundred-septillionth","hundred-quadrillionth"),
};
static public enum Scale
{
SHORT,
LONG;
public String getName(int exponent)
{
for (ScaleUnit unit : SCALE_UNITS)
{
if (unit.getExponent() == exponent)
{
return unit.getName(this.ordinal());
}
}
return "";
}
}
/**
* Change this scale to support American and modern British value (short scale)
* or Traditional British value (long scale)
*/
static public Scale SCALE = Scale.SHORT;
static abstract public class AbstractProcessor
{
static protected final String SEPARATOR = " ";
static protected final int NO_VALUE = -1;
protected List<Integer> getDigits(long value)
{
ArrayList<Integer> digits = new ArrayList<Integer>();
if (value == 0)
{
digits.add(0);
}
else
{
while (value > 0)
{
digits.add(0, (int) value % 10);
value /= 10;
}
}
return digits;
}
public String getName(long value)
{
return getName(Long.toString(value));
}
public String getName(double value)
{
return getName(Double.toString(value));
}
abstract public String getName(String value);
}
static public class UnitProcessor extends AbstractProcessor
{
static private final String[] TOKENS = new String[]
{
"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve",
"thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"
};
@Override
public String getName(String value)
{
StringBuilder buffer = new StringBuilder();
int offset = NO_VALUE;
int number;
if (value.length() > 3)
{
number = Integer.valueOf(value.substring(value.length() - 3), 10);
}
else
{
number = Integer.valueOf(value, 10);
}
number %= 100;
if (number < 10)
{
offset = (number % 10) - 1;
//number /= 10;
}
else if (number < 20)
{
offset = (number % 20) - 1;
//number /= 100;
}
if (offset != NO_VALUE && offset < TOKENS.length)
{
buffer.append(TOKENS[offset]);
}
return buffer.toString();
}
}
static public class TensProcessor extends AbstractProcessor
{
static private final String[] TOKENS = new String[]
{
"twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"
};
static private final String UNION_SEPARATOR = "-";
private UnitProcessor unitProcessor = new UnitProcessor();
@Override
public String getName(String value)
{
StringBuilder buffer = new StringBuilder();
boolean tensFound = false;
int number;
if (value.length() > 3)
{
number = Integer.valueOf(value.substring(value.length() - 3), 10);
}
else
{
number = Integer.valueOf(value, 10);
}
number %= 100; // keep only two digits
if (number >= 20)
{
buffer.append(TOKENS[(number / 10) - 2]);
number %= 10;
tensFound = true;
}
else
{
number %= 20;
}
if (number != 0)
{
if (tensFound)
{
buffer.append(UNION_SEPARATOR);
}
buffer.append(unitProcessor.getName(number));
}
return buffer.toString();
}
}
static public class HundredProcessor extends AbstractProcessor
{
private int EXPONENT = 2;
private UnitProcessor unitProcessor = new UnitProcessor();
private TensProcessor tensProcessor = new TensProcessor();
@Override
public String getName(String value)
{
StringBuilder buffer = new StringBuilder();
int number;
if (value.isEmpty())
{
number = 0;
}
else if (value.length() > 4)
{
number = Integer.valueOf(value.substring(value.length() - 4), 10);
}
else
{
number = Integer.valueOf(value, 10);
}
number %= 1000; // keep at least three digits
if (number >= 100)
{
buffer.append(unitProcessor.getName(number / 100));
buffer.append(SEPARATOR);
buffer.append(SCALE.getName(EXPONENT));
}
String tensName = tensProcessor.getName(number % 100);
if (!tensName.isEmpty() && (number >= 100))
{
buffer.append(SEPARATOR);
}
buffer.append(tensName);
return buffer.toString();
}
}
static public class CompositeBigProcessor extends AbstractProcessor
{
private HundredProcessor hundredProcessor = new HundredProcessor();
private AbstractProcessor lowProcessor;
private int exponent;
public CompositeBigProcessor(int exponent)
{
if (exponent <= 3)
{
lowProcessor = hundredProcessor;
}
else
{
lowProcessor = new CompositeBigProcessor(exponent - 3);
}
this.exponent = exponent;
}
public String getToken()
{
return SCALE.getName(getPartDivider());
}
protected AbstractProcessor getHighProcessor()
{
return hundredProcessor;
}
protected AbstractProcessor getLowProcessor()
{
return lowProcessor;
}
public int getPartDivider()
{
return exponent;
}
@Override
public String getName(String value)
{
StringBuilder buffer = new StringBuilder();
String high, low;
if (value.length() < getPartDivider())
{
high = "";
low = value;
}
else
{
int index = value.length() - getPartDivider();
high = value.substring(0, index);
low = value.substring(index);
}
String highName = getHighProcessor().getName(high);
String lowName = getLowProcessor().getName(low);
if (!highName.isEmpty())
{
buffer.append(highName);
buffer.append(SEPARATOR);
buffer.append(getToken());
if (!lowName.isEmpty())
{
buffer.append(SEPARATOR);
}
}
if (!lowName.isEmpty())
{
buffer.append(lowName);
}
return buffer.toString();
}
}
static public class DefaultProcessor extends AbstractProcessor
{
static private String MINUS = "minus";
static private String UNION_AND = "and";
static private String ZERO_TOKEN = "zero";
private AbstractProcessor processor = new CompositeBigProcessor(63);
@Override
public String getName(String value)
{
boolean negative = false;
if (value.startsWith("-"))
{
negative = true;
value = value.substring(1);
}
int decimals = value.indexOf(".");
String decimalValue = null;
if (0 <= decimals)
{
decimalValue = value.substring(decimals + 1);
value = value.substring(0, decimals);
}
String name = processor.getName(value);
if (name.isEmpty())
{
name = ZERO_TOKEN;
}
else if (negative)
{
name = MINUS.concat(SEPARATOR).concat(name);
}
if (!(null == decimalValue || decimalValue.isEmpty()))
{
name = name.concat(SEPARATOR).concat(UNION_AND).concat(SEPARATOR)

.concat(processor.getName(decimalValue)).concat(SEPARATOR).concat(SCALE.getName(-dec
imalValue.length()));
}
return name;
}
}
static public AbstractProcessor processor;
public static void main(String...args)
{
processor = new DefaultProcessor();
long[] values = new long[]
{
0,4,10,12,100,108,299,1000,1003,2040,45213,100000,100005,100010,202020,202022,999999,
1000000,1000001,10000000,10000007,99999999,
Long.MAX_VALUE,
Long.MIN_VALUE
};
String[] strValues = new String[]
{
"0001.2","3.141592"
};
for (long val : values)
{
System.out.println(val + " = " + processor.getName(val) );
}
for (String strVal : strValues)
{
System.out.println(strVal + " = " + processor.getName(strVal) );
}
//generates a very large number
StringBuilder bigNumber = new StringBuilder();
for (int d=0; d<66; d++)
{
bigNumber.append( (char) ((Math.random() * 10) + '0'));
}
bigNumber.append(".");
for (int d=0; d<26; d++)
{
bigNumber.append( (char) ((Math.random() * 10) + '0'));
}
System.out.println(bigNumber.toString() + " = " + processor.getName(bigNumber.toString()));
}
}

106.Automorphic Number Program in Java


public class AutomorphicNumberExample1
{
//user-defined static method that checks whether the number is automorphic or not
static boolean isAutomorphic(int num)
{
//determines the square of the specified number
int square = num * num;
//comparing the digits until the number becomes 0
while (num > 0)
{
//find the remainder (last digit) of the variable num and square and comparing them
if (num % 10 != square % 10)
//returns false if digits are not equal
return false;
//reduce num and square by dividing them by 10
num = num/10;
square = square/10;
}
return true;
}
//Driver code
public static void main(String args[])
{
//number to be check
//calling the method and prints the result accordingly
System.out.println(isAutomorphic(76) ? "Automorphic" : "Not Automorphic");
System.out.println(isAutomorphic(13) ? "Automorphic" : "Not Automorphic");
}
}

107.Peterson Number in Java


import java.io.*;
import java.util.*;
public class PetersonNumberExample1
{
//an array is defined for the quickly find the factorial
static long[] factorial = new int[] { 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800,
39916800, 479001600};
//driver code
public static void main(String args[])
{
//constructor of the Scanner class
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number to check: ");
//reading a number from the user
int n=sc.nextInt();
//calling the user-defined function to check Peterson number
if (isPeterson(n))
System.out.println("The given number is a Peterson number.");
else
System.out.println("The given number is not a Peterson number.");
}
//function to check the given number is Peterson or not
static boolean isPeterson(int n)
{
int num = n;
int sum = 0;
//loop executes until the condition becomes false
while (n > 0)
{
//determines the last digit of the given number
int digit = n % 10;
//determines the factorial of the digit and add it to the variable sum
sum += factorial[digit];
//removes the last digit of the given number
n = n / 10;
}
//compares sum with num if they are equal returns the number itself
return (sum == num);
}
}

108.Sunny Number in Java


import java.util.*;
public class SunnyNumberExample1
{
//driver code
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number to check: ");
//reading a number from the user
int N=sc.nextInt();
//calling user-defined function
isSunnyNumber(N);
}
//function checks whether the given is a perfect square or not
static boolean findPerfectSquare(double num)
{
//finds the square root of the ggiven number
double square_root = Math.sqrt(num);
//if square root is an integer
return((square_root - Math.floor(square_root)) == 0);
}
//function that checks whether the given number is Sunny or not
static void isSunnyNumber(int N)
{
//checks N+1 is perfect square or not
if (findPerfectSquare(N + 1))
{
System.out.println("The given number is a sunny number.");
}
//executes if N+1 is not a perfect square
else
{
System.out.println("The given number is not a sunny number.");
}
}
}

109.Tech Number in Java


import java.util.Scanner;
public class TechNumberExample2
{
public static void main(String args[])
{
int n, num, firstHalf, secondHalf, digits = 0, squareOfSum = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number to check: ");
//reads an integer from the user
n = sc.nextInt();
//assign the value of n into num
num = n;
//the loop executes until the condition returns false
while (num > 0)
{
//incerements the variable digits by 1
digits++;
//removes the last digit of the given number
num = num / 10;
}
//check if the given number has an even number of digits or not
if (digits % 2 == 0)
{
num = n;
//determines the first half of the given number
firstHalf = num % (int) Math.pow(10, digits / 2);
//determines the second half of the given number
secondHalf = num / (int) Math.pow(10, digits / 2);
//calculate the square of both the numbers
squareOfSum = (firstHalf + secondHalf) * (firstHalf + secondHalf);
//compares the square of the sum with the given number
if (n == squareOfSum)
{
System.out.println(n+" is a tech number.");
}
else
{
System.out.println(n+" is not a tech number.");
}
}
else
{
System.out.println(n+ " is not a tech number.");
}
}
}

110.Keith Number in Java


import java.util.*;
class KeithNumberExample1
{
//user-defined function that checks if the given number is Keith or not
static boolean isKeith(int x)
{
//List stores all the digits of the X
ArrayList<Integer> terms=new ArrayList<Integer>();
//n denotes the number of digits
int temp = x, n = 0;
//executes until the condition becomes false
while (temp > 0)
{
//determines the last digit of the number and add it to the List
terms.add(temp%10);
//removes the last digit
temp = temp/10;
//increments the number of digits (n) by 1
n++;
}
//reverse the List
Collections.reverse(terms);
int next_term = 0, i = n;
//finds next term for the series
//loop executes until the condition returns true
while (next_term < x)
{
next_term = 0;
//next term is the sum of previous n terms (it depends on number of digits the number has)
for (int j=1; j<=n; j++)
next_term = next_term + terms.get(i-j);
terms.add(next_term);
i++;
}
//when the control comes out of the while loop, there will be two conditions:
//either next_term will be equal to x or greater than x
//if equal, the given number is Keith, else not
return (next_term == x);
}
//driver code
public static void main(String[] args)
{
//calling the user-defined method inside the if statement and print the result accordingly
if (isKeith(19))
System.out.println("Yes, the given number is a Keith number.");
else
System.out.println("No, the given number is not a Keith number.");
if(isKeith(742))
System.out.println("Yes, the given number is a Keith number.");
else
System.out.println("No, the given number is not a Keith number.");
if(isKeith(4578))
System.out.println("Yes, the given number is a Keith number.");
else
System.out.println("No, the given number is not a Keith number.");
}
}

111.ATM program Java


//import required classes and packages
import java.util.Scanner;

//create ATMExample class to implement the ATM functionality


public class ATMExample
{
//main method starts
public static void main(String args[] )
{
//declare and initialize balance, withdraw, and deposit
int balance = 100000, withdraw, deposit;

//create scanner class object to get choice of user


Scanner sc = new Scanner(System.in);

while(true)
{
System.out.println("Automated Teller Machine");
System.out.println("Choose 1 for Withdraw");
System.out.println("Choose 2 for Deposit");
System.out.println("Choose 3 for Check Balance");
System.out.println("Choose 4 for EXIT");
System.out.print("Choose the operation you want to perform:");

//get choice from user


int choice = sc.nextInt();
switch(choice)
{
case 1:
System.out.print("Enter money to be withdrawn:");

//get the withdrawl money from user


withdraw = sc.nextInt();

//check whether the balance is greater than or equal to the withdrawal amount
if(balance >= withdraw)
{
//remove the withdrawl amount from the total balance
balance = balance - withdraw;
System.out.println("Please collect your money");
}
else
{
//show custom error message
System.out.println("Insufficient Balance");
}
System.out.println("");
break;

case 2:

System.out.print("Enter money to be deposited:");

//get deposite amount from te user


deposit = sc.nextInt();
//add the deposit amount to the total balanace
balance = balance + deposit;
System.out.println("Your Money has been successfully depsited");
System.out.println("");
break;

case 3:
//displaying the total balance of the user
System.out.println("Balance : "+balance);
System.out.println("");
break;

case 4:
//exit from the menu
System.exit(0);
}
}
}
}

112.Autobiographical Number in Java


import java.util.*;
public class AutobiographicalNumberExample
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number you want to check: ");
//reading an integer from the user to check
int num = sc.nextInt();
//determines the absolute value of the given number
num = Math.abs(num);
//assigning the value of num into variable n
int n = num;
//the valueOf() method returns the string representation of int argument
String str = String.valueOf(num);
//creates an array of digits
int digitarray[] = new int[str.length()];
for(int i = digitarray.length - 1; i >= 0; i--)
{
//determines the last digit of the given number
digitarray[i] = n % 10;
//removes the last digit
n = n/10;
}
boolean flag = true;
//an inner loop compares the iterator of the outer loop with each digit of the inner loop //if they
are equal then increment the occurrence count of the digit
for(int i = 0; i < digitarray.length; i++)
{
int count = 0;
for(int j = 0; j < digitarray.length; j++)
{
if(i == digitarray[j])
//increments the count by 1 if the above condition returns true
count++;
}
if(count != digitarray[i])
{
flag = false;
//breaks the execution if the condition becomes true
break;
}
}
if(flag)
//prints if the status returns true
System.out.println(num + " is an autobiographical number.");
else
//prints if status returns false
System.out.println(num + " is not an autobiographical number.");
}
}

113.Emirp Number in Java


import java.io.*;
import java.util.*;
public class EmirpNumberExample1
{
//function that checks the given number is prime or not
public static boolean isPrime(int n)
{
//base case
if (n <= 1)
return false;
//loop executes from 2 to n-1
for (int i = 2; i < n; i++)
if (n % i == 0)
//returns false if the condition returns true
return false;
//returns true if the condition returns false
return true;
}
//function that checks if the given number is emirp or not
public static boolean isEmirp(int n)
{
//checks if the given number is prime or not
if (isPrime(n) == false)
return false;
//variable that stores the reverse of the number
int reverse = 0;
//the while loop executes until the specified condition becomes false
while (n != 0)
{
//finds the last digit of the number (n)
int digit = n % 10;
//finds the reverse of the given number
reverse = reverse * 10 + digit;
//removes the last digit
n = n/10;
}
//calling the user-defined function that checks the reverse number is prime or not
return isPrime(reverse);
}
//driver code
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter a number to check: ");
//reading an integer from the user
int n=sc.nextInt();
if (isEmirp(n) == true)
System.out.println("Yes, the given number is an emirp number.");
else
System.out.println("No, the given number is not an emirp number.");
}
}

114.Sphenic Number in Java


import java.util.*;
public class SphenicNumberExample1
{
//create a global array of size 100000
static boolean arr[] = new boolean[10000];
//finds all the primes smaller than the limit
static void findPrime()
{
//marks all entries as true
//A value in mark[p] will finally be false if 'p' is Not a prime, else true.
Arrays.fill(arr, true);
//iterate over all the numbers so that their multiples can be marked as composite
for(int p = 2; p * p < 10000; p++)
{
//if p is not changed, then it is a prime
if(arr[p])
{
//update all the multiples of p
for(int i = p * 2; i < 10000; i = i + p)
arr[i] = false;
}
}
}
//user-defined function that checks if the given number is sphenic or not
static int isSphenic(int N)
{
//creating an array that stores the 8 divisors
int []arr1 = new int[8];
//counts the divisors
int count = 0;
int j = 0;
for(int i = 1; i <= N; i++)
{
if(N % i == 0 && count < 8)
{
//increments the count by 1
count++;
arr1[j++] = i;
}
}
//checks that there is exactly 8 divisors or not and all the numbers are distincit prime or not
//if yes returns 1, else returns 0
if(count == 8 && (arr[arr1[1]] && arr[arr1[2]] && arr[arr1[3]]))
return 1;
return 0;
}
//driver code
public static void main(String args[])
{
//calling user-defined function that find the priime numbers
findPrime();
Scanner sc=new Scanner(System.in);
System.out.print("Enter a number to check: ");
//reading an iteger from the user
int n=sc.nextInt();
int result = isSphenic(n);
if(result == 1)
//prints yes if the above condition returns true
System.out.print("Yes, the given number is sphenic.");
else
//prints no if the above condition returns false
System.out.print("No, the given number is not a sphenic.");
}
}

115.ISBN Number Java


//import required classes and packages
import java.util.*;
import java.io.*;
import java.util.Scanner;

//create ISBNNumberExample class to check whether the given number is a valid ISBN or not
class ISBNNumberExample {

static boolean checkISBNNumber(long number)


{
int sum = 0;
int i, t, intNumber, dNumber;
String strNumber;

strNumber = ""+number;

if (strNumber.length() != 10) {
return false;
}

for (i = 0; i < strNumber.length(); i++) {


intNumber = Integer.parseInt(strNumber.substring(i, i+1));
dNumber = i + 1;
t = dNumber * intNumber;
sum = sum + t;
}

// check whether the sum is divisible by 11 or not

if ((sum % 11) == 0) {
return true;
}

return false;

// main() method start


public static void main(String args[])
{
long n1, n2;

try {

//create BufferedReader class object to get input from user


InputStreamReader in = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(in);

//show custom message


System.out.println("Enter first 10 digit ISBN number");

//store user entered value into variable n1


n1 = Long.parseLong(br.readLine());

//show custom message


System.out.println("Enter second 10 digit ISBN number");

//store user entered value into variable n2


n2 = Long.parseLong(br.readLine());

if (checkISBNNumber(n1))
System.out.println(n1 + " is a valid ISBN number");
else
System.out.println(n1 + " is not a valid ISBN number");
if (checkISBNNumber(n2))
System.out.println(n2 + " is a a valid ISBN number");
else
System.out.println(n2 + " is not a valid ISBN number");

}catch(java.lang.Exception e) {
System.out.println("Error while reading the data.");
}
}
}

116.Krishnamurthy Number Java


//import required classes and packages
import Java.util.*;
import java.io.*;
import java.util.Scanner;

//create KrishnamurthyNumber class to check whether the given number is a Krishnamurthy


number or not
class KrishnamurthyNumber {

// create fact() method to calculate the factorial of the number


static int fact(int number)
{
int f = 1;
while (number != 0) {
f = f * number;
number--;
}
return f;
}

// create checkNumber() method that returns true when it founds number krishnamurthy
static boolean checkNumber(int number)
{
int sum = 0; //initialize sum to 0

int tempNumber = number; //create a copy of the original number

//perform operation until tempNumber will not equal to 0


while (tempNumber != 0) {
// calculate the factorial of the last digit of the tempNumber and then add it to the sum
sum = sum + fact(tempNumber % 10);
// replace the value of tempNumber by tempNumber/10
tempNumber = tempNumber / 10;
}

// Check whether the number is equal to the sum or not. If both are equal, number is
krishnamurthy number
if(sum == number)
return true;
else
return false;
}

// main() method start


public static void main(String[] args)
{
int n; //initialize variable n

//create scanner class object to read data from user


Scanner sc = new Scanner(System.in);

//custom message
System.out.println("Enter any number:");

//store user entered value into variable n


n = sc.nextInt();

if (checkNumber(n))
System.out.println(n + " is a krishnamurthy number");
else
System.out.println(n + "is not a krishnamurthy number");
}
}

117.Bouncy Number in Java


import java.util.*;
public class BouncyNumberExample1
{
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter any number you want to check: ");
//reading an integer from the user
int inputNumber = scan.nextInt();
//if any of the following condition returns true, the number id not bouncy
if (isIncreasing(inputNumber) || isDecreasing(inputNumber) || inputNumber < 101)
//prints if the number is not bouncy
System.out.println(inputNumber+" not a bouncy number.");
else
//prints if the number is bouncy
System.out.println(inputNumber+" is a bouncy number.");
}
//function that checks if the number is an increasing number or not
public static boolean isIncreasing(int inputNumber)
{
//converts the number into string
String str = Integer.toString(inputNumber);
char digit;
//flag set to true
boolean flag = true;
//iterates over the string up to length-1
for(int i=0;i < str.length()-1;i++)
{
digit = str.charAt(i);
//if any digit is greater than check next digit, it will not check further
if(digit > str.charAt(i+1))
{
//flag set to false if the condition returns true
flag = false;
break;
}
}
return flag;
}
//function that checks if the number is a decreasing number or not
public static boolean isDecreasing(int inputNumber)
{
//converts the number into string
String str = Integer.toString(inputNumber);
char digit;
//flag set to true
boolean flag = true;
//iterates over the string up to length-1
for(int i=0;i < str.length()-1;i++)
{
digit = str.charAt(i);
//if any digit is less than the next digit, it will not check further
if(digit < str.charAt(i+1))
{
//flag set to false if the condition returns true
flag = false;
break;
}
}
return flag;
}
}

118.Mystery Number in Java


import java.util.Scanner;
public class MysteryNumberExample1
{
//function that finds reverse of the given number
static int reverse(int x)
{
//converts the given number into string
String str = Integer.toString(x);
//stores string
String string="";
for(int i=str.length()-1;i>=0;i--)
{
//stores the reverse of the string
string=string+str.charAt(i);
}
//converts the string into integer
int rev=Integer.parseInt(str);
//returns the reverse number
return rev;
}
//function that checks the number is mystery or not
static boolean isMysteryNo(int num)
{
for (int i=1; i <= num/2; i++)
{
//calling the function that reverse a number and assign it to j
int j = reverse(i);
//compares the sum of two numbers is equal to given number or not
if (i + j == num)
{
//prints a pair of numbers whose sum is the given number
System.out.println( i + " " + j);
System.out.println(num+ " is a mystery number.");
//returns a boolean value if pair is found
return true;
}
}
System.out.println("The given number is not a mystery number.");
//returns false if pair is not found
return false;
}
//driver code
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter a number: ");
//reading an integer from the user
int num = sc.nextInt();
//calling the user-defined function to check the number is a mystery or not
isMysteryNo(num);
}
}

119.Strontio Number in Java


import java.util.*;
public class StrontioNumberExample1
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number: ");
//reading an integer from the user
int num=sc.nextInt();
int n=num;
//first, we have multiplied a number by 2
//the resultant is divided by 1000 that gives the remainder and removes the first digit
//at last, the resultant is divided by 10 that removes the last digit
//therefore, we get a two-digit number that are mean digits of the given number
num=(num*2%1000)/10;
//divide the two-digit number (that we get from the above) by 10 and find the remainder
//compares the remainder and quotient
if(num%10==num/10)
//if equal, prints strontio number
System.out.println(n+ " is a strontio number.");
else
//prints if not a strontio number
System.out.println(n+ " is not a strontio number.");
}
}

120.Xylem and Phloem Number in Java


import java.util.*;
import java.io.*;
public class XylemPhloemExample
{
public static void main(String args[])
{
//the variable extreme_sum stores the sum of extreme digits
//the variable mean_sum stores the sum of mean digits
int num, extreme_sum = 0, mean_sum = 0, n;
Scanner sc= new Scanner (System.in);
System.out.print("Enter a number: ");
//reading an integer from the user
num = sc.nextInt();
//finds the absolute value of the given number
num = Math.abs(num);
//copying the given number into n
n = num;
//the while loop executes until the specified condition becomes false
while(n != 0)
{
//returns true if one of the conditions is true
if(n == num || n < 10)
//finds the last digit and add it to the variable extreme_sum
extreme_sum = extreme_sum + n % 10;
else
//finds the mean digits and add it to the variable mean_sum
mean_sum = mean_sum + n % 10;
//removes the last digit from the number
n = n / 10;
}
System.out.println("The sum of extreme digits: " + extreme_sum );
System.out.println("The sum of mean digits: " + mean_sum);
//comparing the sum of extreme digits and with the sum of mean digits
if(extreme_sum == mean_sum)
//prints if sums are equal
System.out.println(num + " is a xylem number.");
else
//prints if sums are not equal
System.out.println(num + " is a phloem number.");
}
}

121.nth Prime Number Java


import java.util.Scanner;
public class NthPrimeNumberExample
{
public static void main(String[] args)
{
//constructor of the Scanner class
Scanner sc = new Scanner(System.in);
System.out.print("Enter the value of n to compute the nth prime number: ");
//reading an integer from the user
int n = sc.nextInt();
int num=1, count=0, i;
while (count < n)
{
num=num+1;
for (i = 2; i <= num; i++)
{
//determines the modulo and compare it with 0
if (num % i == 0)
{
//breaks the loop if the above condition returns true
break;
}
}
if (i == num)
{
//increments the count variable by 1 if the number is prime
count = count+1;
}
}
//prints the nth prime number
System.out.println("The " +n +"th prime number is: " + num);
}
}

122.Java Program to Find Square Root of a Number Without sqrt Method


import java.util.Scanner;
public class FindSquareRootExample1
{
public static void main(String[] args)
{
System.out.print("Enter a number: ");
//creating object of the Scanner class
Scanner sc = new Scanner(System.in);
//reading a number form the user
int n = sc.nextInt();
//calling the method and prints the result
System.out.println("The square root of "+ n+ " is: "+squareRoot(n));
}
//user-defined method that contains the logic to find the square root
public static double squareRoot(int num)
{
//temporary variable
double t;
double sqrtroot=num/2;
do
{
t=sqrtroot;
sqrtroot=(t+(num/t))/2;
}
while((t-sqrtroot)!= 0);
return sqrtroot;
}
}

123.Java Program to Swap Two Numbers Using Bitwise Operator


import java.util.Scanner;
public class SwapTwoNumbersExample1
{
public static void main(String args[])
{
int a, b;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first number: ");
a = scanner.nextInt();
System.out.print("Enter the second number: ");
b = scanner.nextInt();
System.out.println("Before swapping:");
System.out.println("a = " +a +", b = " +b);
a = a ^ b;
b = a ^ b;
a = a ^ b;
System.out.println("After swapping:");
System.out.print("a = " +a +", b = " +b);
}
}

124.Java Program to Find Smallest of Three Numbers Using Ternary Operator


import java.util.Scanner;
public class SmallestNumberExample1
{
public static void main(String[] args)
{
int a, b, c, smallest, temp;
//object of the Scanner class
Scanner sc = new Scanner(System.in);
//reading input from the user
System.out.println("Enter the first number:");
a = sc.nextInt();
System.out.println("Enter the second number:");
b = sc.nextInt();
System.out.println("Enter the third number:");
c = sc.nextInt();
//comparing a and b and storing the smallest number in a temp variable
temp=a<b?a:b;
//comparing the temp variable with c and storing the result in the variable names smallest
smallest=c<temp?c:temp;
//prints the smallest number
System.out.println("The smallest number is: "+smallest);
}
}

125.Generate and show the first 15 narcissistic decimal numbers


// https://rosettacode.org/
public class Example6 {
public static void main(String[] args){
for(long n = 0, ctr = 0; ctr < 15; n++){
if(is_narc_dec_num(n)){
System.out.print(n + " ");
ctr++;
}
}
System.out.println();
}
public static boolean is_narc_dec_num(long n){
if(n < 0) return false;

String str1 = Long.toString(n);


int x = str1.length();
long sum_num = 0;

for(char c : str1.toCharArray()){
sum_num += Math.pow(Character.digit(c, 10), x);
}
return sum_num == n;
}

126.Write a Java program to print out the first 10 Catalan numbers by extracting them from
Pascal's triangle.
import java.util.Scanner;
public class Example8 {
public static void main(String[] args) {
int num = 10;
int[] t = new int[num + 2];
t[1] = 1;
System.out.printf("\nList 10 Catalan numbers:-\n");
for (int i = 1; i <= num; i++) {

for (int j = i; j > 1; j--)


t[j] = t[j] + t[j - 1];

t[i + 1] = t[i];

for (int j = i + 1; j > 1; j--)


t[j] = t[j] + t[j - 1];
System.out.printf("\n%d ", t[i + 1] - t[i]);
}
System.out.printf("\n");
}
}

127.Write a Java program to check if a number is a cube or not.


import java.util.Scanner;
public class Example18 {

public static void main( String args[] ){


Scanner sc = new Scanner( System.in );
System.out.print("Input a number: ");
int num = sc.nextInt();
int n = (int) Math.round(Math.pow(num, 1.0/3.0));
if((num == n * n * n))
{
System.out.print("Number is a cube.");
}
else
{
System.out.print("Number is not a cube.");
}
System.out.println("\n");
}
}

128.Write a Java program to check if a number is cyclic or not.


import java.util.Scanner;
import java.math.BigInteger;
public class Example19 {

public static void main( String args[] ){


Scanner sc = new Scanner( System.in );
System.out.print("Input a number: ");
String strnum = sc.nextLine().trim();
BigInteger n = new BigInteger(strnum);
int len = strnum.length()+1;
String str = String.valueOf(len);
BigInteger n1 = new BigInteger(str);
StringBuilder buf = new StringBuilder();
for(int i = 0 ; i < (len-1); i++) {
buf.append('9');
}
BigInteger total = new BigInteger(buf.toString());
if(n.multiply(n1).equals(total)) {
System.out.println("It is a cyclic number.");
}
else {
System.out.println("Not a cyclic number.");
}
}
}

129.Write a Java program to display the first 10 Fermat numbers.


In mathematics, a Fermat number is a positive integer of the form
import java.util.Scanner;
public class Example20 {

public static void main( String args[] ){


int n = 0;
double result;

while (n <= 10) {


result= Math.pow (2, Math.pow(2, n)) + 1;
n++;
System.out.println (result);
}
}
}

You might also like