You are on page 1of 12

Design a program to accept a day number (between 1 and 366), year (in 4 digits) from the user to generate

and
display the corresponding date. Also, accept ‘N’ (1 <= N <= 100) from the user to compute and display the
future date corresponding to ‘N’ days after the generated date. Display an error message if the value of the day
number, year and N are not within the limit or not according to the condition specified.

import java.util.*;

class ISC2019Q1 {

public static void main(String[] args) {

int day_num;int yr;int N;

Scanner sc = new Scanner(System.in);

ISC2019Q1 date = new ISC2019Q1();

do {

System.out.println("Enter a day number (between 1 and 366)");

day_num = sc.nextInt();

if(day_num < 1 || day_num > 366)

System.out.println("INVALID DAY NUMBER:");

}while(day_num < 1 || day_num > 366);

do {

System.out.println("Enter the year");

yr = sc.nextInt();

if(Integer.toString(yr).length()!=4)

System.out.println("INVALID YEAR:");

}while(Integer.toString(yr).length()!=4);

do {

System.out.println("Enter the value of N:");

N = sc.nextInt();

if(N < 1 || N > 100)

System.out.println("INVALID DAY NUMBER (between 1 and 100):");

}while(N < 1 || N > 100);

date.setDate(day_num,yr,N);

System.out.println("OUTPUT:");

date.dateSpecifier();

}
private boolean flag;private static boolean leap;

private int dayNumber,year,dayAfter;

String[] months =
{"January","Feburary","March","April","May","June","July","August","Sepetember","October","November","
December"};

int[] mdays={31,28,31,30,31,30,31,31,30,31,30,31};

int[] ldays={31,29,31,30,31,30,31,31,30,31,30,31};

public void setDate(int dayNumber, int year, int dayAfter) {

this.dayNumber = dayNumber;

this.year = year;

this.dayAfter = dayAfter;

public void dateSpecifier() {

int m = 0,k=1;

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

if( checkLeap(year) == true ? k > ldays[m] : k > mdays[m] ) {

k = 1;

m++;

k++;

String prefix;

prefix = (k-1)%10 == 1 ? "st" : (k-1)%10 == 2 ? "nd" : (k-1)%10 == 3 ? "rd" : "th";

System.out.println("DATE: "+(k-1)+prefix+" "+months[m]+" ,"+year);

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

if( checkLeap(year) == true ? k > ldays[m] : k > mdays[m] ) {

k = 1;

m++;

if(m > 11) {

year++;
m = 0;

k++;

prefix = (k-1)%10 == 1 ? "st" : (k-1)%10 == 2 ? "nd" : (k-1)%10 == 3 ? "rd" : "th";

System.out.println("DATE AFTER "+dayAfter+" DAYS:"+(k-1)+""+prefix+" "+months[m]+" ,"+year);

private boolean checkLeap(int year) {

if(year%400==0)

leap=true;

else if (year%100==0)

leap=false;

else if (year%4==0)

leap=true;

else

leap=false;

return leap;

Write a program to declare a single dimensional array a[ ] and a square matrix b[ ] [ ] of size N, where N>2 and
N<10. Allow the user to input positive integers into the single dimensional array. Perform the following tasks
on the matrix: (a) Sort the elements of the single dimensional array in ascending order using any standard
sorting technique and display the sorted elements. (b) Fill the square matrix b[ ][ ] in the following format.
If the array a[ ] = { 5, 2, 8, 1 } then, after sorting a[ ] = {1, 2, 5, 8} Then, the matrix b[ ][ ] would fill as below:
1258

1251

1212

1125

(c)Display the filled matrix in the above format.

public class ISC2019Q2 {

static int N;

static int ar[];

static int Mat[][];


public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the value of N:");

N = sc.nextInt();

if(N >= 2 || N <= 10)

ar=new int[N];

Mat=new int [N][N];

ISC2019Q2 ob =new ISC2019Q2();

ob.inputSingleDimArray();

ob.SortMatrix();

ob.inputElementsToTwoDimMatrix();

ob.displayMatrix();

else

System.out.println("INVALID INPUT:");

public void inputElementsToTwoDimMatrix() {

for(int i = 0; i < N; i++) {

for(int j = 0; j < N; j++) {

if(i+j < N)

Mat[i][j]=ar[j];

else

Mat[i][j]=ar[(i+j)-N];

}
public void inputSingleDimArray() {

Scanner sc = new Scanner(System.in);

for(int i=0;i<N;i++) {

System.out.println("Enter element "+(i+1));

ar[i]=sc.nextInt();

if(ar[i] <= 0) {

System.out.println("Only positive values allowed,enter the value again:");

i--;

continue;

public void SortMatrix() {

int temp;

for (int i =0;i<N;i++) {

for(int j=i;j<N;j++) {

if(ar[i] > ar[j]) {

temp = ar[j];

ar[j]=ar[i];

ar[i]=temp;

public void displayMatrix() {

System.out.println("SINGLE DIMENSIONAL ARRAY AFTER SORTING:");

for(int k=0;k<N;k++)
System.out.print(" "+ar[k]);

System.out.println();

System.out.println("TWO DIMENSIONAL MATRIX:");

for(int i = 0; i < N; i++) {

for(int j = 0; j < N; j++) {

System.out.print(Mat[i][j]+" ");

System.out.println();

Write a program to accept a sentence which may be terminated by either ‘.’ , ‘?’ or ‘!’ only. The words are to be
separated by a single blank space and are in UPPER CASE. Perform the following tasks: (a) Check for the
validity of the accepted sentence.

(b) Convert the non-palindrome words of the sentence into palindrome words by concatenating the
word by its reverse (excluding the last character). Example: The reverse of the word HELP would be
LEH (omitting the last alphabet) and by concatenating both, the new palindrome word is
HELPLEH. Thus, the word HELP becomes HELPLEH. Note: The words which end with repeated
alphabets, for example ABB would become ABBA and not ABBBA and XAZZZ becomes
XAZZZAX

A Goldbach number is a positive even integer that can be expressed as the sum of two odd primes. Note: All
even integer numbers greater than 4 are Goldbach numbers. Write a program to accept an even integer ‘N’
where N > 9 and N < 50. Find all the odd prime pairs whose sum is equal to the number ‘N’

import java.util.*;

class Q1_2018

private boolean isPrime(int n)

for(int x=2;x<=n/2;x++)

if(n%x==0)

return false;

}
return true;

void print(int n)

for(int x=2;x<=n;x++)

for(int y=x;y<=n;y++)

if(isPrime(x)&&isPrime(y)&&x+y==n)

System.out.println(x+","+y);

public static void main()

Scanner sc=new Scanner(System.in);

Q1_2018 ob=new Q1_2018();

System.out.println("Enter any even no.");

int n=sc.nextInt();

if(n%2==0)

if(n>9 && n<50)

ob.print(n);

else

{
System.out.println("INVALID INPUT.NUMBER OUT OF RANGE");

else

System.out.println("INVALID INPUT.NUMBER IS ODD");

Write a program to declare a matrix A[ ] [ ] of order (M N) where ‘M’ is the number of rows and ‘N’ is the
number of columns such that the values of both ‘M’ and ‘N’ must be greater than 2 and less than 10. Allow the
user to input integers into this matrix. Perform the following tasks on the matrix: (a) Display the original
matrix. (b) Sort each row of the matrix in ascending order using any standard sorting technique. (c)
Display the changed matrix after sorting each row.

import java.util.*;

public class TwoDArr

void sort(int a[]) //Taking single dimension array as parameter

int i, j, n = a.length, tmp; //a.length gives the number of values in the 1D array

for(i=0;i<n;i++)

for(j=0;j<n-1-i;j++) //Sorting using bubble sort technique

if(a[j]>a[j+1])

tmp = a[j];

a[j] = a[j+1];

a[j+1] = tmp;

}
}

void display(int a[][]) //Taking 2D array as parameter

int i, j;

for(i=0;i<a.length;i++) //a.length here gives the number of rows

for(j=0;j<a[i].length;j++) //a[i] gives one row at a time. a[i].length gives the number
of values in each row

System.out.print(a[i][j]+"\t");

System.out.println();

void sort2D(int a[][])

{ this function.

int i, j;

for(i=0;i<a.length;i++)

sort(a[i]);
}

public static void main()

Scanner sc = new Scanner(System.in);

TwoDArr obj = new TwoDArr();


int a[][], m, n, i, j;

System.out.println("Enter the number of rows: ");

m = sc.nextInt();
System.out.println("Enter the number of columns: ");

n = sc.nextInt();

if(m<3||m>9||n<3||n>9)

System.out.println("Matrix out of range");

System.exit(0);
}

a = new int[m][n];
System.out.println("Enter the values for the matrix: ");

for(i=0;i<m;i++)

for(j=0;j<n;j++)

a[i][j] = sc.nextInt();

System.out.println("Original Matrix: ");

obj.display(a);
obj.sort2D(a);
System.out.println("Matrix after sorting rows: ");

obj.display(a);

}
import java.util.*;

public class Q3_2018

public static void main(String args[])


{

Scanner sc = new Scanner(System.in);

String ar[];

int n, x,y;

System.out.println("Enter the number of teams: ");

n = sc.nextInt();

if(n>2 && n<9)

ar = new String[n];

for(x=0;x<n;x++)

System.out.print("Team "+(x+1)+": ");

ar[x] = sc.next();

int max = 0;

for(x=0;x<n;x++)

if(max<ar[x].length())

max = ar[x].length();//Finding the length of longest string

System.out.println("OUTPUT:" );

for(x=0;x<max;x++)

for(y=0;y<n;y++)

/*character will be extracted only when x has a value less

* than the length of the string else only tabular space will be printed

*/

if(x<ar[y].length())
{

System.out.print(ar[y].charAt(x)+"\t");

else

System.out.print("\t");

System.out.println();

else

System.out.println("INVALID INPUT");

You might also like