Computer
Computer
Code:
import java.util.Scanner;
class Vampire{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("m = ");
int m = Integer.parseInt(in.nextLine());
System.out.print("n = ");
int n = Integer.parseInt(in.nextLine());
if(m < 1000 || n < 1000 || m > 9999 || n > 9999){
System.out.println("INVALID INPUT");
return;
}
int count = 0;
System.out.println("THE VAMPIRE NUMBERS ARE:");
for(int i = m; i <= n; i++){
if(isVampire(i)){
fi
fi
count++;
System.out.print(i + " ");
}
}
if(count == 0)
System.out.print("NIL");
System.out.println("\nFREQUENCY OF VAMPIRE NUMBER IS:
" + count);
}
public static boolean isVampire(int num){
String number = String.valueOf(num);
int len = number.length();
if(len % 2 != 0)
return false;
for(int i = (int)Math.pow(10, len / 2 - 1); i <
Math.pow(10, len / 2); i++){
int factor1 = i;
int factor2 = num / factor1;
if(factor1 * factor2 == num){
String factors = String.valueOf(factor1) +
String.valueOf(factor2);
char originalChars[] = new
char[number.length()];
for(int j = 0; j < number.length(); j++)
originalChars[j] = number.charAt(j);
char factorsChars[] = new
char[factors.length()];
for(int j = 0; j < factors.length(); j++)
factorsChars[j] = factors.charAt(j);
sort(originalChars);
sort(factorsChars);
if(equals(originalChars, factorsChars))
return true;
}
}
return false;
}
public static void sort(char a[]){
for(int i = 0; i < a.length; i++){
for(int j = 0; j < a.length - 1 - i; j++){
if(a[j] > a[j + 1]){
char temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}
public static boolean equals(char a[], char b[]){
if(a.length != b.length)
return false;
for(int i = 0; i < a.length; i++){
if(a[i] != b[i])
return false;
}
return true;
}
}
Examples:
Example 1
INPUT:
m = 1002
n = 1640
OUTPUT:
THE VAMPIRE NUMBERS ARE: 1260 1395 1435 1530
FREQUENCY OF VAMPIRE NUMBER IS: 4
Example 2
INPUT:
m = 1810
n = 7800
OUTPUT:
THE VAMPIRE NUMBERS ARE: 1827 2187 6880
FREQUENCY OF VAMPIRE NUMBER IS: 3
Example 3
INPUT:
m = 8105
N = 9999
OUTPUT:
THE VAMPIRE NUMBERS ARE: NIL
FREQUENCY OF VAMPIRE NUMBER IS: 0
Example 4
INPUT:
m = 174
n = 4500
Code:
import java.util.Scanner;
class Snowball{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String s = in.nextLine();
char last = s.charAt(s.length() - 1);
if(last != '.' && last != '?'){
System.out.println("INCORRECT TERMINATING
CHARACTER. INVALID INPUT");
return;
}
int len = 0;
String word = "";
boolean status = true;
for(int i = 0; i < s.length(); i++){
char ch = s.charAt(i);
if(Character.isLetterOrDigit(ch))
word += ch;
else{
if(len == 0)
len = word.length();
else if(len + 1 != word.length()){
status = false;
break;
}
else
len = word.length();
word = "";
}
}
if(status)
System.out.println("IT IS A SNOWBALL STRING");
else
System.out.println("IT IS NOT A SNOWBALL
STRING");
}
}
Examples:
Example 1
INPUT: He may give bonus.
OUTOUT: IT IS A SNOWBALL STRING
Example 2
INPUT: Is the cold water frozen?
OUTPUT: IT IS A SNOWBALL STRING
Example 3
INPUT: Look before you leap.
OUTPUT: IT IS NOT A SNOWBALL STRING
Example 4
INPUT: The child is father of the man!
OUTPUT: INCORRECT TERMINATING CHARACTER. INVALID INPUT
Doubly Markov
Matrix
A matrix which satis es the following conditions is Doubly Markov
Matrix:
(i) All elements are >= 0
(ii) Sum of each row = 1
(iii) Sum of each column = 1
Code:
import java.util.Scanner;
class DoublyMarkov{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("N = ");
int n = Integer.parseInt(in.nextLine());
if(n < 3 || n > 9){
System.out.println("SIZE IS OUT OF RANGE. INVALID
ENTRY");
return;
}
double m[][] = new double[n][n];
boolean isNegative = false;
System.out.println("Enter elements in the matrix:");
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
m[i][j] = Double.parseDouble(in.nextLine());
if(m[i][j] < 0)
isNegative = true;
}
}
if(isNegative){
System.out.println("NEGATIVE NUMBERS ENTERED.
INVALID ENTRY");
return;
}
System.out.println("FORMED MATRIX");
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++)
System.out.print(m[i][j] + "\t");
System.out.println();
fi
}
boolean isMarkov = true;
for(int i = 0; i < n; i++){
double rowSum = 0.0;
double colSum = 0.0;
for(int j = 0; j < n; j++){
rowSum += m[i][j];
colSum += m[j][i];
}
if(rowSum != 1)
isMarkov = false;
if(colSum != 1)
isMarkov = false;
}
if(isMarkov)
System.out.println("IT IS A DOUBLY MARKOV
MATRIX");
else
System.out.println("IT IS NOT A DOUBLY MARKOV
MATRIX");
}
}
Examples:
Example 1
INPUT: N = 3
Enter elements in the matrix: 0.5, 0.25, 0.25, 0.25, 0.75, 0.0, 0.25, 0.0,
0.75
OUTPUT: FORMED MATRIX
Example 2
INPUT: N = 3
Enter elements in the matrix: 1.5, 3, 0.15, 0.25, 4, 1.0, 0.25, 1.0, 3
OUTPUT: FORMED MATRIX
1.5 3 0.15
0.25 4 1.0
0.25 1.0 3
Example 3
INPUT: N = 3
Enter elements in the matrix: 0.8, -4.0, 0.9, 3.5, 0.25, 0.25, 0.5, 0.0, 0.5
OUTPUT: NEGATIVE NUMBERS ENTERED. INVALID ENTRY
Example 4
INPUT: N = 12
OUTPUT: SIZE IS OUT OF RANGE. INVALID ENTRY
Hamming Number
Hamming numbers are positive integers whose prime factors include 2,
3 and 5 only.
Example:
n = 6 is a hamming number as 6 = 2 × 3. So, its prime factors are limited
to 2, 3.
n = 8 is a hamming number as 8 = 2 × 2 × 2 and it has only 2 as its prime
factors.
n = 90 is a hamming number as 90 = 2 × 3 × 3 × 5 which has only 2, 3, 5
as prime factors.
n = 14 is not a hamming number as 14 = 2 × 7. It has 7 as one of its
prime factors.
n = 44 is not a hamming number as 44 = 2 × 2 × 11. It has 11 as one of its
prime factors.
Code:
import java.util.Scanner;
class Hamming{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = Integer.parseInt(in.nextLine());
if(n < 0){
System.out.println("NEGATIVE NUMBER ENTERED.
INVALID INPUT");
return;
}
boolean status = true;
if(n < 2)
status = false;
int pf = 2;
int num = n;
int count = 0;
while(num > 1){
if(num % pf == 0){
count++;
if(count == 1)
System.out.print(pf);
else
System.out.print(" x " + pf);
num /= pf;
if(pf != 2 && pf != 3 && pf != 5)
status = false;
}
else
pf++;
}
if(status)
System.out.println("\n" + n + " IS A HAMMING
NUMBER");
else
System.out.println("\n" + n + " IS NOT A HAMMING
NUMBER");
}
}
Examples:
Example 1
INPUT: Enter any number: 3600
OUTPUT: 3600 = 2 × 2 × 2 × 2 × 3 × 3 × 5 × 5
3600 IS A HAMMING NUMBER
Example 2
INPUT: Enter any number: 5832
OUTPUT: 5832 = 2 × 2 × 2 × 3 × 3 × 3 × 3 × 3 × 3
5832 IS A HAMMING NUMBER
Example 3
INPUT: Enter any number: 7854
OUTPUT: 7854 = 2 × 3 × 7 × 11 × 17
7854 IS NOT A HAMMING NUMBER
Example 4
INPUT: Enter a number: -120
OUTPUT: NEGATIVE NUMBER ENTERED. INVALID INPUT
270 degrees rotation
a 2D matrix
Code:
import java.util.Scanner;
class Rotate{
public static void main(String args[]){
Scanner in=new Scanner(System.in);
System.out.print("M = ");
int m=Integer.parseInt(in.nextLine());
System.out.print("N = ");
int n=Integer.parseInt(in.nextLine());
if(m<3 || n<3 || m>9 || n>9){
System.out.println("Invalid Input");
return;
}
int a[][]=new int[m][n];
int b[][]= new int[n][m];
int sum=0;
System.out.println("ENTER ELEMENTS:");
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
a[i][j]=Integer.parseInt(in.nextLine());
if(a[i][j]%2!=0)
sum+=a[i][j];
}
}
System.out.println("ORIGINAL MATRIX :");
display(a,m,n);
int q=0;
for(int i=m-1;i>=0;i--){
int p=0;
for(int j=0;j<n;j++){
b[p][q]=a[i][j];
p++;
}
q++;
}
System.out.println("ROTATED MATRIX BY 270 DEGREES
ANTICLOCKWISE");
display(b,n,m);
System.out.println("SUM OF THE ODD ELEMENTS = " +
sum);
}
public static void display(int a[][], int m, int n){
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
System.out.print(a[i][j]+ "\t");
}
System.out.println();
}
}
}
Examples:
Example 1
INPUT:
M=3
N=4
ENTER ELEMENTS: 8, 7, 9, 3, -2, 0, 4, 5, 1, 3, 6, -4
OUTPUT:
ORIGINAL MATRIX
8 7 9 3
-2 0 4 5
1 3 6 -4
1 -2 8
3 0 7
6 4 9
-4 5 3
Example 3
INPUT:
M=2
N = 10
OUTPUT: INVALID INPUT
Date Program
Code:
import java.util.Scanner;
class DayNumber{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("DAY NUMBER: ");
int dayNum = Integer.parseInt(in.nextLine());
System.out.print("YEAR: ");
int year = Integer.parseInt(in.nextLine());
System.out.print("N: ");
int n = Integer.parseInt(in.nextLine());
boolean invalidDay = false;
boolean invalidN = false;
if((dayNum == 366 && !isLeap(year)) || dayNum > 366
|| dayNum < 1){
System.out.println("INCORRECT DAY NUMBER");
invalidDay = true;
}
if(n < 1 || n > 100){
System.out.println("INCORRECT VALUE OF 'N'");
invalidN = true;
}
if(invalidDay || invalidN)
return;
String monthNames[] = {"", "JANUARY", "FEBRUARY",
"MARCH", "APRIL", "MAY", "JUNE", "JULY", "AUGUST",
"SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"};
int monthDays[] = {0, 31, 28, 31, 30, 31, 30, 31, 31,
30, 31, 30, 31};
if(isLeap(year))
monthDays[2] = 29;
int i = 1;
while(dayNum > monthDays[i]){
dayNum -= monthDays[i];
i++;
}
System.out.println("ENTERED DATE: " + monthNames[i] +
" " + dayNum + ", " + year);
int num = n;
while(n > 0){
dayNum++;
n--;
if(dayNum > monthDays[i]){
dayNum = 1;
i++;
}
if(i > 12){
i = 1;
year++;
if(isLeap(year))
monthDays[2] = 29;
}
}
System.out.println(num + " DAYS LATER: " +
monthNames[i] + " " + dayNum + ", " + year);
}
public static boolean isLeap(int y){
if((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
return true;
return false;
}
}
Examples:
Example 1
INPUT: DAY NUMBER: 50
YEAR: 2023
N: 25
OUTPUT: ENTERED DATE: FEBRUARY 19, 2023
25 DAYS LATER: MARCH 16, 2023
Example 2
INPUT: DAY NUMBER: 321
YEAR: 2023
N: 77
OUTPUT: ENTERED DATE: NOVEMBER 17, 2023
77 DAYS LATER: FEBRUARY 2, 2024
Example 3
INPUT: DAY NUMBER: 400
YEAR: 2023
N: 125
OUTPUT: INCORRECT DAY NUMBER
INCORRECT VALUE OF ‘N’