Simple Encryption Program in Java

A simple encryption system uses a shifting process to hide a message. The value of the shift can be in the range 1 to 26. For example, a shift of 7 means that A = U, B = V, C = W, etc. A B C D E F G H I J K L M N O P Q R S T U V W X Y Z U V W X Y Z A B C D E F G H I J K L M N O P Q R S T Firstly an extra space is added to the end of the string. To make things little more difficult, spaces within the original text are replaced with QQ before the text is encrypted. Double Q (QQ) was selected because no English word ends in Q or contains QQ. Additionally, the coded message is printed in blocks of six characters separated by spaces. The last block might not contain six characters. Write a program that takes the coded text (less than 100 characters), the shift value and prints the decoded original text. Your program must reject any invalid value for shift and also display a suitable message “INVALID SHIFT VALUE” for the same. Assume all characters are in uppercase. Test your program for the following data as well as your own data: Example 1: INPUT: CODED TEXT: UHINBY LQQQCH HYLQQ SHIFT: 7 OUTPUT: DECODED TEXT: ANOTHER WINNER Example 2: INPUT: CODED TEXT: RUIJQQ EVQQBK SAQQ SHIFT: 11 OUTPUT: DECODED TEXT: BEST OF LUCK Example 3: INPUT: CODED TEXT: DKSQQW NAQQUK QQQ SHIFT: 29 OUTPUT: INVALID SHIFT VALUE
 import java.io.*;
 class Decoder
{
 public static void main(String args[])throws IOException
 {
 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 System.out.print("CODED TEXT: ");
 String c = br.readLine();
 System.out.print("SHIFT: ");
 int s = Integer.parseInt(br.readLine());
 if(s > 26)
{
 System.out.println("INVALID SHIFT VALUE");
 return;
 }
 c = c.toUpperCase();
 c = c.replace(" ", "");
 c = c.replace("QQ", " ");
 String d = "";
 for(int i = 0; i < c.length(); i++)
{
 char ch = c.charAt(i);
 if(Character.isLetter(ch))
{
 if(ch + s - 1 <= 90)
 d += (char)(ch + s - 1);
 else
{
 char letter = (char)((ch + s - 1) % 90 + 64);
 d += letter;
 }
 }
 else d += ch;
 }
 System.out.println("DECODED TEXT: " + d);
 }
 }

Middle Digit Number in Java

Write a program in Java to accept a positive integer from the user. If the number has odd number of digits, then display the square of the middle digit. But if the number has even number of digits, then display the square root of the sum of the square of the two digits in the middle. Example 1: INPUT: N = 12345 OUTPUT: Middle number = 3. Square of the middle number = 9. Example 2: INPUT: N = 123456 OUTPUT: Middle number = 34 Sum of the squares of digits = 32 + 42 = 25. Square root = 5.0
 import java.io.*;
 class Middle
{
 public static void main(String args[])throws IOException
{
 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 System.out.print("N = ");
 int num = Math.abs(Integer.parseInt(br.readLine()));
 int count = 0; 
 for(int i = num; i != 0; i /= 10)
 count++; 
 if(count % 2 == 1)
{
 for(int i = 1; i <= count / 2; i++)
{
 num /= 10; 
 }
 int mid = num % 10;
 System.out.println("Middle number = " + mid);
 System.out.println("Square of the middle number = " + mid * mid);
 }
 else
{
 for(int i = 1; i < count / 2; i++)
{
 num /= 10;
 }
 int mid = num % 100; 
 System.out.println("Middle number = " + mid);
 int first = mid / 10;
 int second = mid % 10;
 int sum = first * first + second * second; 
 System.out.println("Sum of the square of digits = " + sum);
 double s = Math.sqrt(sum); 
 System.out.println("Square root = " + s); 
 }
 }
 }

Jumping Number in Java

A Jumping Number is a number in which each adjacent digit differs by only 1. For example, 76789 is a Jumping Number. Also note that all the one-digit numbers are considered to be Jumping Numbers. Write a program in Java to input a positive integer and check whether it is a Jumping Number. Display a suitable message accordingly. 
 import java.io.*;
 class Jumping
{
 public static void main(String args[])throws IOException
{
 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
 System.out.print("Enter a positive integer: ");
 int num = Math.abs(Integer.parseInt(br.readLine()));
 int copy = num;
 boolean status = true;
 int diff = 0; 
 while(num != 0)
 int d1 = num % 10; 
 num /= 10;
 if(num != 0)
{
 int d2 = num % 10; 
 if(Math.abs(d1 - d2) != 1)
{
 status = false;
 break;
 }
 }
 }
 if(status)
 System.out.println(copy + " is a jumping number."); 
 else 
 System.out.println(copy + " is NOT a jumping number.");
 }
 }

Autobiographical Number in Java

An autobiographical number is a number such that the first digit of it counts how many zeroes are there in it, the second digit counts how many ones are there and so on. An autobiographical number is like a self-descriptive number. For example, 1210 has 1 zero, 2 ones, 1 two and 0 threes. 3211000 is another example of an autobiographical number. Even the year 2020 is an autobiographical number! Write a program in Java to input a positive number from the user and check whether that number is an autobiographical number or not. 
 import java.io.*;
 class Autobiographical
{
 public static void main(String args[])throws IOException
{
 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 System.out.print("Enter the number: ");
 int num = Integer.parseInt(br.readLine());
 num = Math.abs(num); 
 int n = num; 
 String s = String.valueOf(num);
 int digit[] = new int[s.length()];
 for(int i = digit.length - 1; i >= 0; i--)
{
 digit[i] = n % 10; 
 n /= 10;
 }
 boolean status = true;
 for(int i = 0; i < digit.length; i++)
{
 int count = 0;
 for(int j = 0; j < digit.length; j++)
{
 if(i == digit[j]) count++; 
 }
 if(count != digit[i])
{
 status = false;
 break;
 }
 }
 if(status)
 System.out.println(num + " is an Autobiographical Number."); 
 else 
 System.out.println(num + " is NOT an Autobiographical Number."); 
 }
 }

Full Prime Number in Java

Write a program to input an integer from the user, and check if that number is a full prime. A number is said to be a full prime if it is a prime number and all its digits are also prime numbers. For example, 23 is a full prime number because 23 itself is prime, and all its digits 2, 3 are also prime.
 import java.io.*;
 class FullPrime
{
 public static void main(String args[])throws IOException
{
 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 System.out.print("Enter the number: ");
 int num = Integer.parseInt(br.readLine());
 if(isPrime(num))
{
 int n = num; 
 loop:
 while(n != 0)
{
 int d = n % 10;
 switch(d)
{
 case 2:
 case 3:
 case 5:
 case 7:
 n /= 10;
 break;
 default:
 break loop;
 }
 }
 if(n == 0)
 System.out.println(num + " is full prime.");
 else
 System.out.println(num + " is not full prime.");
 }
 else 
 System.out.println(num + " is not full prime.");
 }
 public static boolean isPrime(int n)
{
 int f = 0;
 for(int i = 1; i <= n; i++)
 if(n % i == 0) 
 f++; 
 }
 return f == 2;
 }
 }

Xylem And Phloem Number in Java

A xylem number is a number such that the sum of extreme digits equals the sum of the mean digits. Otherwise the number is considered as the phloem number. Write a program in Java to accept a positive integer from the user. Check whether it is a xylem number or a phloem number. Example: INPUT: N = 12348 OUTPUT: Sum of extreme digits = 1 + 8 = 9. Sum of mean digits = 2 + 3 + 4 = 9. Thus, 12348 is a xylem number.
 import java.io.*;
 class Xylem
{
 public static void main(String args[])throws IOException
{
 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 System.out.print("N = ");
 int num = Math.abs(Integer.parseInt(br.readLine())); 
 int extreme = 0;
 int mean = 0; 
 int copy = num;
 while(copy != 0)
{
 if(copy == num || copy < 10)
 extreme += copy % 10;
 else mean += copy % 10; 
 copy /= 10;
 }
 System.out.println("Sum of extreme digits: " + extreme); 
 System.out.println("Sum of mean digits: " + mean); 
 if(extreme == mean) 
 System.out.println(num + " is a xylem number.");
 else 
 System.out.println(num + " is a phloem number."); 
 }
 }

Sphenic Number in Java

A sphenic number is a positive integer which has exactly three distinct prime factors. For example, 30 is a sphenic number because 30 = 2 × 3 × 5. 66, 70, 78, 102, 114 are some more examples of sphenic numbers. Write a program in Java to input a positive integer from the user and check whether it is a sphenic number or not.
 import java.io.*; 
class Sphenic
 public static void main(String args[])throws IOException
{
 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 System.out.print("N = ");
 int n = Math.abs(Integer.parseInt(br.readLine()));
 int a[] = {1, 1, 1};
 int prime = 2;
 int num = n;
 int index = 0;
 outer:
 for(index = 0; index < 3;)
{
 while(num % prime == 0)
{
 a[index++] = prime;
 num /= prime;
 if(index == 3)
 break;
 if(num == 1)
 break outer;
 }
 prime++; 
 if(prime > n)
 break;
 }
 System.out.println("Factors: " + a[0] + ", " + a[1] + ", " + a[2]);
 int product = a[0] * a[1] * a[2];
 if(product == n)
{
 if(a[0] != a[1] && a[1] != a[2] && a[2] != a[0])
{
 if(a[0] > 1 && a[1] > 1 && a[2] > 1) 
 System.out.println(n + " is a sphenic number.");
 else
 System.out.println(n + " is not a sphenic number.");
 }
 else System.out.println(n + " is not a sphenic number."); 
 }
 else System.out.println(n + " is not a sphenic number.");
 }
 }

Ore Number in Java

Ore number is a positive integer whose divisors have a harmonic value as an integer. Ore number is also known as harmonic divisor number. For example, 6 has four factors: 1, 2, 3 and 6. Harmonic mean of the factors is: 4 / (1 + 1 / 2 + 1 / 3 + 1 / 6) = 2 The harmonic mean of the factors of 6 is 2, which is an integer. So, 6 is an Ore number or harmonic divisor number. Write a program in Java to input an integer and check whether it is an Ore Number or not. 
import java.io.*;
 class Ore
{
 public static void main(String args[])throws IOException
{
 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 System.out.print("Enter the number: "); 
 int num = Integer.parseInt(br.readLine()); 
 int f = 0;
 double mean = 0.0;
 for(int i = 1; i <= num; i++)
{
 if(num % i == 0)
{
 f++; mean += 1.0 / i;
 }
 }
 mean = f / mean; 
 if(mean == (int)mean) 
 System.out.println(num + " is an Ore Number.");
 else
 System.out.println(num + " is NOT an Ore Number.");
 }
 }

Achilles Number in Java

An Achilles Number is a number that is powerful but not a perfect power. A Powerful Number is a positive integer N, such that for every prime factor p of N, p2 is also a factor. A Perfect Power is a positive integer N such that it can be expressed as ab, where a and b are natural numbers > 1. 72, 108, 200, 288 are some of the first few Achilles Numbers. The prime factors of 72 = 2 × 2 × 2 × 3 × 3. Both 2 and 22 = 4 are factors of 72. Both 3 and 32 = 9 are factors of 72. Also, 72 can’t be represented as ab. Therefore, 72 is an Achilles Number. Write a program in Java to input an integer from the user and check whether it is an Achilles Number or not. 
 import java.io.*;
 class Power
 public static boolean isPerfectPower(int num)
 for(int i = 2; i <= num; i++)
{
 for(int j = 2; j <= num; j++)
 if(Math.pow(i, j) == num)
 return true; 
 }
 else if(Math.pow(i, j) > num)
 break; 
 }
 } 
 return false; 
 } 
 public static void main(String args[])throws IOException
{
 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 System.out.print("Enter the number: "); 
 int num = Integer.parseInt(br.readLine()); 
 boolean status = true; 
 if(!isPerfectPower(num))
 int n = num; 
 int pf = 2; 
 while(n != 1)
 if(n % pf == 0)
{
 int s = pf * pf;
 if(num % s != 0)
 status = false; 
 break;
 }
 n /= pf; 
 } 
 else
 pf++; 
 }
 } 
 else 
 status = false; 
 if(status)
 System.out.println(num + " is an Achilles Number."); 
 else 
 System.out.println(num + " is not an Achilles Number.");
 } 
}

Second largest number in java using ternary operator

 //Largest second number using ternary operator public class Main { public static void main(String[] args) { int a=5,b=6,c=7; int ...