Computer Final Project
Computer Final Project
Write a program to create a matrix A[][] of size M*N and rotate it 270
degrees anti clock wise.
Algorithm
Step 3:Store the input number of rows and columns in variables 'M' and 'N'
respectively.
Step 8:Rotate the matrix by 270 degrees anti clock wise with the help of the
variable 'help'.
import java.util.Scanner;
class TwoSeventyDegrees
int M=sc.nextInt();
int N=sc.nextInt();
else
for(int i=0;i<M;i++)
for(int j=0;j<N;j++)
A[i][j]=sc.nextInt();
System.out.println("ORIGINAL MATRIX");
for(int i=0;i<M;i++)
for(int j=0;j<N;j++)
{
System.out.print(A[i][j]+"\t");
System.out.println();
int help=M-1;
for(int i=0;i<M;i++)
for(int j=0;j<N;j++)
ar[j][i]=A[help][j];
help--;
System.out.println();
System.out.println("ROTATED MATRIX(270 DEGREE ANTI CLOCK
WISE)");
for(int i=0;i<N;i++)
for(int j=0;j<M;j++)
System.out.print(ar[i][j]+"\t");
System.out.println();
}
VARIABLE DESCRIPTION
help int To help with 270 degree anti clock wise rotation.
INPUT/OUTPUT
INPUT: M=3
N=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
Algorithm
Step 3:Store its last character in a variable ‘ch’ and check if it ends with ‘.’,’?’ or
‘!’.
Step 5:Take out every word from the string and store it in the array ss[].
import java.util.*;
class Snowball
System.out.println("Enter a Sentence");
String str=sc.nextLine();
int ll=str.length();
//Taking out the last character of the string top chexk if it ends with '.','!' or '?'.
char ch=str.charAt(ll-1);
{
System.out.println("Invalid Input");
else
str=str.substring(0,str.length()-1);
int l=s.countTokens();
boolean f=true;
for(int i=0;i<l;i++)
ss[i]=s.nextToken();
if(ss[i+1].length() != ((ss[i].length())+1))
{
f=false;
break;
if(f)
System.out.println("Snowball String");
else
}
VARIABLE DESCRIPTION
INPUT/OUTPUT
Algorithm :
Step 2 : In the main method, input the value of m and n to store the upper and
lower limits of the range given by the user
Step 3 : count and print the total number of vampire numbers in the provided range
• Convert the given number into a string using String.valueOf function and
store it in a variable numString and store its length.
• Firstly, Check whether the number has even number of digits and then
proceed further.
• Extract all the factors of the given number using for-loop and then store the
original number string and factors-String in two different arrays of char data
type.
• Sort both the arrays in ascending order using sort function
• Compare both the arrays using equals function.
• If both arrays are equal ,return true else false
Step 5 : Create a method sort(char arr[]) that sorts the given array in ascending
order using bubble sort technique .
Step 6 : Create a Boolean method equals(char a1[],char a2[]) that checks for the
equality of the two arrays.
Program
import java.util.Scanner;
int m = sc.nextInt();
int n = sc.nextInt();
System.out.println("INVALID INPUT");
return;
}
int count = 0;
for(int i=m;i<=n;i++)
if(isVampire(i))
count++;
System.out.println(i);
int length=numString.length();
if(length%2!=0)
return false;
for(int i= (int)Math.pow(10,length/2-1);i<=(int)Math.pow(10,length/2);i++)
{
int factor1 = i;
if(factor1*factor2==number)
for(int x=0;x<length;x++)
originalChar[x]=numString.charAt(x);
for(int y=0;y<length;y++)
factorsChar[y]=factorString.charAt(y);
sort(originalChar);
sort(factorsChar);
if(equals(originalChar,factorsChar))
return true;
return false;
for(int x=0;x<arr.length;x++)
{
for(int y=0;y<arr.length-1-x;y++)
if(arr[y]>arr[y+1])
arr[y]=arr[y+1];
arr[y+1]=temp;
if(a1.length!=a2.length)
return false;
for(int i=0;i<a1.length;i++)
if(a1[i] != a2[i])
return false;
return true;
}
VARIABLE DESCRIPTION
Input :
1100
1800
Output :
1260
1395
1435
1530
Algorithm
Step 3:Check whether the input number is in range of not.(N>3 && N<9)
Step 5:Initialize a variable isMarkov to check whether the matrix is doubly markov
or not.
Step 10:If isMarkov is true print that the above matrix is doubly markov matrix.
import java.util.*;
class Doubly_Markov
System.out.println("N = ");
int n=Integer.parseInt(sc.nextLine());
if(n<3&&n>9)
return;
boolean isNegative=false;
boolean isMarkov=true;
for(int i=0;i<n;i++)
{
double row=0.0;
for(int j=0;j<n;j++)
m[i][j]=Double.parseDouble(sc.nextLine());
if(m[i][j]<0)
isNegative=true;
row +=m[i][j];
if(row != 1.0)
isMarkov=false;
if(isNegative)
return;
for(int j=0;j<n;j++)
double column=0.0;
for(int i=0;i<n;i++)
column += m[i][j];
if(column !=1.0)
isMarkov=false;
break;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
System.out.print(m[i][j] + "\t");
System.out.println();
if(isMarkov)
}
else
Variable description
N=3
0.5
0.25
0.25
0.25
0.75
0.0
0.25
0.75
Formed matrix:
Algorithm :
import java.util.Scanner;
import java.util.StringTokenizer;
String s = sc.nextLine().trim();
System.out.println("Invalid input");
else
s = s.toUpperCase().trim();
int w = word.length();
int c = 0, v = 0;
char x = word.charAt(j);
v++;
else
c++;
System.out.print(word + "\t");
{
con += "C";
vow += "V";
System.out.print(con + "\n");
System.out.println("\t" + vow);
Variable Description :
Input/Output
Sample Input :
Sample Output :
ITS CC
BEEN CC
VV
LONG CCC
SINCE CCC
VV
MET CC
V
YOU C
VV
Q6. Write a program in java to enter date in dd/mm/yyyy format and day of
the week on which a project was given. If a college allots 90 days (including
the project giving day) to complete the work, then calculate the date on which
the project is to be submitted along with the day name. if the final submission
day is Sunday then the students get one extra day and submit the project on
the next day that is Monday.
Also check the validity of the date entered and display your output in the
format as given below :
Enter the day of the week on which the project was given: TUESDAY
Algorithm :
import java.util.Scanner;
if (parts.length != 3)
return;
return;
while (daysForProject> 0)
if (day == daysInCurrentMonth)
day = 1;
if (month == 12)
month = 1;
year++;
}
else
month++;
else
day++;
daysForProject--;
if (submissionDayOfWeek == "SUNDAY")
}
public static boolean isValidDate(int day, int month, int year)
if (year < 1 || month < 1 || month > 12 || day < 1 || day > 31)
return false;
int startDayIndex = 0;
if (startDay.equals(daysOfWeek[i]))
{
startDayIndex = i - 1;
break;
if (finalDayIndex == 0 &&startDayIndex != 0)
finalDayIndex = 7;
Variable Description :
Input/Output
Sample Input:
Enter the day of the week on which the project was given: TUESDAY
Sample Output :
Q7. Hamming numbers are numbers whose prime factors are 2, 3 or 5 only.
Examples :
Algorithm :
Program
import java.util.Scanner;
import java.util.StringTokenizer;
int t = n;
if (t % i == 0)
prime = false;
break;
return prime;
int t = n;
return false;
if (t % i == 0 &&isPrime(i))
if (i != 2 &&i != 3 &&i != 5)
{
ham = false;
break;
return ham;
int n = sc.nextInt();
if (isHam(n))
else
}
Variable Description :
Sample Input :
20
Sample Output:
20 is a Hamming number
Q8. Write a program to create a 2D array of size n*n and fill the numbers in a
circular fashion in anticlockwise with natural numbers from 1 to n2
Algorithm:
Program
import java.util.Scanner;
public class Question8
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of the matrix: ");
int n = sc.nextInt();
int x = 1;
int arr[][] = new int[n][n];
int a = 0;
int b = n - 1;
while (a < n)
{
for (int i = a; i <= b; i++)
{
arr[i][a] = x++;
}
a++;
for (int i = a; i <= b; i++)
{
arr[b][i] = x++;
}
for (int i = b - 1; i >= a - 1; i--)
{
arr[i][b] = x++;
}
b--;
for (int i = b; i >= a; i--)
{
arr[a-1][i] = x++;
}
}
System.out.println("Circular Matrix Anti-Clockwise:");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(arr[i][j] + "\t");
}
System.out.println();
}
}
}
Variable Description:
Input/Output
Input: n = 5
Output:
1 16 15 14 13
2 17 24 23 12
3 18 25 22 11
4 19 20 21 10
5 6 7 8 9
Q9. Given a square matrix M of order m. The maximum value possible for m
is 10 and minimum is 3. Accept 3 different characters from the keyboard and
fill the array according to the output shown in the following example:
m=4
First character: *
Second character: ?
Third character: #
Output: # * * #
? # # ?
? # # ?
# * * #
Algorithm:
Program
import java.util.Scanner;
public class Square
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of the array: ");
int m = sc.nextInt();
if(m > 10 || m < 3)
{
System.out.println("***Size out of range***");
System.exit(0);
}
char arr[][] = new char[m][m];
System.out.println("Enter the first character: ");
char ch1 = sc.next().charAt(0);
System.out.println("Enter the second character: ");
char ch2 = sc.next().charAt(0);
System.out.println("Enter the third character: ");
char ch3 = sc.next().charAt(0);
for(int i = 0; i < m; i++)
{
for(int j = 0; j < m; j++)
{
if(i == j || (i + j) == m - 1)
arr[i][j] = ch3;
else if(i == 0 || i == m-1 )
arr[i][j] = ch1;
else if(j == 0 || j == m-1)
arr[i][j] = ch2;
else
arr[i][j] = ch3;
}
}
System.out.println("Filled array: ");
for(int i = 0; i < m; i++)
{
for(int j = 0; j < m; j++)
{
System.out.print(arr[i][j] + "\t");
}
System.out.println();
}
}
}
Variable Description
Input/Output
Input:
m=5
ch1 = *
ch2 = ?
ch3 = #
Output:
# * * * #
? # # # ?
? # # # ?
? # # # ?
# * * * #