Practice Assessment

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

PRACTICE ASSESSMENT - 1

-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;

import java.u l.Scanner;


import java.u l.HashMap;
public class test {
public sta c void main(String [] args) {
Scanner d = new Scanner(System.in);
System.out.print("Enter an integer : ");
int number = d.nextInt();
int numcop = number;
int[] arr = new int[20];
int[] arr2 = new int[20];
int k=0;
for (int i=2;i<numcop;i++) {
while(numcop%i==0) {
arr[k++] = i;
numcop=numcop/i;
}
}

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;

import java.u l.Scanner;


import java.u l.Arrays;
public class sortedFactors {
public sta c void main(String args[]) {
Scanner d = new Scanner(System.in);
System.out.print("Enter integer : ");
int number = d.nextInt();
int numcop = number;
int[] arr = new int[20];
int k=0;
for (int i=2;i<numcop;i++) {
while(numcop%i==0) {
arr[k++] = i;
numcop=numcop/i;
}
}

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;

import java.u l.Scanner;

public class Occurence {


public sta c void main(String args[]) {
Scanner d = new Scanner(System.in);
String a = d.next();
for(int i=0;i<a.length();i++) {
char ag=a.charAt(i);
if(i!=a.length()-1) {
if( ag!=a.charAt(i+1)) {
System.out.print( ag);
}
else {
while( ag!=a.charAt(i)) {
i++;
}
}
}
else {
System.out.println( ag);
}
}
}
}
ti
ti
fl
fl
fl
fl
fl
6.Java Program to Remove Characters from the Input String which are Present in the Mask
String Input: Enter String: Prac ce Java String Programs Enter Mask String: abc Output:
Pr e Jv String Programs

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;

public class onezero {


public sta c void main(String args[]) {
int [] a = new int [] {1,0};
int b;
for(int i=0;i<6;i++) {
for(int j=0;j<=i;j++) {
b=(i+j)%2;
System.out.print(a[b]+" ");
}
System.out.println();
}

}
}
ti
ti
ff
ti
ti
tt
ti
ff
ti
8.. Palindrome Triangle Pa ern

public class PalindromeTriangle {

public sta c void main(String[] args) {


int n = 6;
int[] startNumbers = {1, 2, 3, 5, 7, 11}
for (int i = 0; i < n; i++) {
for (int j = n - i - 1; j > 0; j--) {
System.out.print(" ");
}
for (int j = i; j >= 0; j--) {
System.out.print(startNumbers[j] + " ");
}
for (int j = 1; j <= i; j++) {
System.out.print(startNumbers[j] + " ");
}
System.out.println();
}
}
ti
tt
9.Bu er y Star Pa ern
package test;

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;

import java.u l.Scanner;


import java.u l.Arrays;
public class inverted_triangle{
public sta c void main(String args[]) {
for(int k=0;k<2;k++) {
if(k==0) {
for (int i=5;i>=k;i--) {
for(int m=(5-i);m>0;m--) {
System.out.print(" ");
}
for(int j=i;j>=0;j--) {
System.out.print((6-j)+" ");
}
System.out.println();
}
}
else {
for(int i=k;i<=5;i++) {
for(int m=(5-i);m>0;m--) {
System.out.print(" ");
}
for(int j=i;j>=0;j--) {
System.out.print((6-j)+" ");
}
System.out.println();
}
}
}
}
}
ti
ti
ti
tt
11.Hollow Hourglass Pa ern

package test;

public class sortedarray {


public sta c void main(String[] args) {
int n = 6;
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
System.out.print(" ");
}
for (int j = 0; j < 2 * (n - i) - 1; j++) {
if (i == 0 || i == n - 1) {
System.out.print("*");
} else {
if (j == 0 || j == 2 * (n - i) - 2) {

System.out.print("*");
} else {

System.out.print(" ");
}
}
}
System.out.println();
}

for (int i = 1; i < n; i++) {


for (int j = 0; j < n - i - 1; j++) {
System.out.print(" ");
}
for (int j = 0; j < 2 * i + 1; j++) {
if (i == n - 1 || j == 0 || j == 2 * i) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
ti
tt
12.K Pa ern

public class kpa ern {


public sta c void main(String args[]) {
for(int k=0;k<2;k++) {
if (k==0) {
for(int i=5;i>k;i--) {
for(int j=i;j>0;j--) {
System.out.print("*");
}
System.out.println();
}
}
else {
for(int i=k+1;i<6;i++) {
for(int j=i;j>0;j--) {
System.out.print("*");
}
System.out.println();
}
}
}
}
}
tt
tt
ti

You might also like