Practice Assessment
Practice Assessment
Practice Assessment
-SANJITH (23BCE0536)
1.. (Find future dates) Write a program that prompts the user to enter an integer for
today’s day of the week (Sunday is 0, Monday is 1, …, and Saturday is 6). Also prompt the
user to enter the number of days a er today for a future day and display the future day of
the week without using any inbuilt func on to nd the future day.
package test;
import java.u l.Scanner;
public class Date {
public sta c void main(String args[]) {
Scanner d = new Scanner(System.in);
String [] a = new String []
{"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
int val = d.nextInt();
int num = d.nextInt();
int res = val+num;
res = res%7;
System.out.println("Entered day : "+a[val]);
System.out.println("Final day : "+a[res]);
}
}
ti
ti
ft
ti
fi
2.A. Write a program that prompts the user to enter an integer m and nd the smallest
integer n such that m * n is a perfect square. (Hint: Store all smallest factors of m into an
array list. n is the product of the factors that appear an odd number of mes in the array
list. For example, consider m = 90, store the factors 2, 3, 3, 5 in an array list. 2 and 5 appear
an odd number of mes in the array list. So, n is 10.) Here are sample runs:
package test;
if(numcop>2) {
arr[k++] = numcop;
}
HashMap<Integer, Integer> count = new HashMap<>();
for (int i = 0; i < k; i++) {
int factor = arr[i];
count.put(factor, count.getOrDefault(factor, 0) + 1);
}
int l=0;
for(int factor: count.keySet()) {
if(count.get(factor)%2!=0) {
arr2[l++] = factor;
}
}
int value=1;
for (int i=0;i<l;i++) {
value*=arr2[i];
}
ti
ti
ti
ti
fi
ti
System.out.println("The smallst number n for m*n to be a pefect square is
"+value);
System.out.println("m*n is "+(value*number));
}
}
3.Write a program that prompts the user to enter a posi ve integer and displays all its
smallest factors in decreasing order. For example, if the integer is 120, the smallest factors
are displayed as 5, 3, 2, 2, 2. Use the StackOfIntegers class to store the factors (e.g., 2, 2, 2,
3, 5) and retrieve and display them in reverse order.
package test;
if(numcop>2) {
arr[k++] = numcop;
}
for(int i=0;i<k;i++) {
System.out.print(arr[i]+" ");
}
}
}
ti
ti
ti
ti
4.Write a program to sort the columns in a two dimensional array. The array size entered by
the user as n x n or m x n. All the values must be entered by the user. Finally display the
sorted array matrix.
package test;
import java.u l.Scanner;
import java.u l.Arrays;
public class sortedarray {
public sta c void main(String args[]) {
Scanner d = new Scanner(System.in);
System.out.print("Enter number of rows : ");
int m = d.nextInt();
System.out.print("Enter number of columns : ");
int n = d.nextInt();
int [][] a = new int [m][n];
int [][] res = new int [m][n];
int [] sort = new int [m];
for(int i=0;i<m;i++) {
for(int j=0;j<n;j++) {
a[i][j]=d.nextInt();
}
};
for(int k=0;k<n;k++) {
for(int l=0;l<m;l++) {
sort[l]=a[l][k];
}
Arrays.sort(sort);
for(int h=0;h<m;h++) {
res[h][k] = sort[h];
}
}
System.out.println("Sorted");
for(int i=0;i<m;i++) {
for(int j=0;j<n;j++) {
System.out.print(res[i][j]+" ");
}
System.out.println();
}
}
}
ti
ti
ti
5.Java Program to Remove All Adjacent Duplicates from String Input: String: ABBCCCD
Output: ABCD
package test;
package test;
import java.u l.*;
import java.io.*;
public class duplicates {
public sta c void main(String args[]) throws IOExcep on {
Bu eredReader bi = new Bu eredReader(new
InputStreamReader(System.in));
Scanner d = new Scanner(System.in);
String a = bi.readLine();
String b = d.next();
String mask = "["+b+"]";
String res = a.replaceAll(mask,"");
System.out.println(res);
}
}
7. Zero-One Triangle Pa ern
package test;
}
}
ti
ti
ff
ti
ti
tt
ti
ff
ti
8.. Palindrome Triangle Pa ern
public class bu er y {
public sta c void main(String args []) {
for(int i=1;i<7;i++) {
for(int j=0;j<i;j++) {
System.out.print("*");
}
for(int k=0;k<(6-i);k++) {
System.out.print(" ");
}
for(int j=0;j<i;j++) {
System.out.print("*");
}
System.out.println();
}
for(int i=6;i>0;i--) {
for(int j=0;j<i;j++) {
System.out.print("*");
}
for(int k=0;k<(6-i);k++) {
System.out.print(" ");
}
for(int j=0;j<i;j++) {
System.out.print("*");
}
System.out.println();
}
}
}
tt
fl
tt
ti
tt
fl
10.Mirror Image Triangle Pa ern
package test;
package test;
System.out.print("*");
} else {
System.out.print(" ");
}
}
}
System.out.println();
}