18 Programs
18 Programs
18 Programs
circular fashion (clockwise) with natural numbers from 1 to n*n, taking ‘n’
import java.util.*;
class Circular_Matrix
int n = sc.nextInt();
while(k<=n*n)
for(int i=c1;i<=c2;i++)
A[r1][i]=k++;
for(int j=r1+1;j<=r2;j++)
A[j][c2]=k++;
for(int i=c2-1;i>=c1;i--)
A[r2][i]=k++;
}
for(int j=r2-1;j>=r1+1;j--)
A[j][c1]=k++;
c1++;
c2--;
r1++;
r2--;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
System.out.print(A[i][j]+ "\t");
System.out.println();
/*We will take a variable ‘k’ which will begin with 1 and
•c1 denotes the index of the column from where we have to begin.
filled completely.
The cells which will be filled are marked in the image above
in yellow color.
starting from Row R1+1 till R2. (The cells which will be filled
starting from Column C2-1 till C1. (The cells which will be
starting from Row R2-1 till R1+1. (The cells which will be
The above 4 steps will now repeat with the inner matrix.
‘n*n’ values.
*/
/**Wondrous Square..
following conditions:
17 24 2 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
Write a program to read n (2 < = n < = 10) and the values stored
E.g. 2, 3, 5, 7, 11,.......
Test your program for the following data and some random data.
SAMPLE DATA:
INPUT : N=4
16 15 1 2
6 4 10 14
9 8 12 5
3 7 11 13
2 0 3
3 3 0
5 2 3
7 3 1
11 3 2
13 3 3
*/
import java.util.*;
class wonderous
int arr[][],arr1[];
int n,i,j,x=0,r,c;
int flag;
void perform()
n=sc.nextInt();
if(n<=2||n>=10)
System.out.println("Invalid Range");
System.exit(0);
arr=new int[n][n];
arr1=new int[2*n];
for(i=0;i<n;i++)
for(j=0;j<n;j++)
arr[i][j]=sc.nextInt();
for(i=0;i<n;i++)
r=0;
c=0;
for(j=0;j<n;j++)
System.out.print(arr[i][j]+" ");
r=r+arr[i][j];
c=c+arr[j][i];
}
System.out.println();
arr1[x]=r;
arr1[x+n-1]=c;
x++;
for(i=0;i< x;i++)
break;
if(i==x)
else
for(i=0;i< n;i++)
for(j=0;j< n;j++)
if(prime(arr[i][j]))
{
int count=0,index;
for(index=2;index<no;index++)
if(no%index==0)
count=count+1;
if(count==0)
return true;
else
return false;
ob.perform();
import java.util.*;
class ISC2018Q3
String ar[];
int n, i, j;
n = sc.nextInt();
ar = new String[n];
for(i=0;i<n;i++)
{
ar[i] = sc.nextLine();
int max = 0;
for(i=0;i<n;i++)
if(max<ar[i].length())
max = ar[i].length();
System.out.println("OUTPUT:" );
for(i=0;i<max;i++)
for(j=0;j<n;j++)
*/
if(i<ar[j].length())
System.out.print(ar[j].charAt(i)+"\t");
else
System.out.print("\t");
System.out.println();
}
/**Question:
Test your program for the given sample data and some random data.
Sample Data:
Input: 29
Input: 17001
Input: 119
Input: 500
*/
import java.io.*;
class Num2Word_ISC2011
String ty[]={"","","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};
String ten[]={"","Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen",
"Eighteen","Nineteen"};
String unit[]={"","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
int n=Integer.parseInt(br.readLine());
if(n<1 || n>9999)
System.out.println("Out of Range");
else
System.out.print("Output = ");
if(th!=0)
System.out.print(unit[th]+" Thousand");
/*Condition for printing digit at hundred's place, is that it should not be zero*/
if(h!=0)
System.out.print(" And");
if(t==1)
//When digit at ten's place is 1, we have different words like Ten, Eleven etc.
System.out.print(" "+ten[u+1]);
else
/*Output:
Out of range*/
The first few such numbers are 4, 22, 27, 58, 85, 94,
121 ………………..
Examples:
1. 666
Prime factors are 2, 3, 3, and 37
2. 4937775
Sample data:
*/
import java.util.*;
class Smith
int sumDig(int n)
int d,s=0;
while(n>0)
d=n%10;
s=s+d;
n=n/10;
return s;
int sumPrimeFact(int n)
while(n>1)
if(n%i==0)
sum=sum+sumDig(i);
//Here'i'is the prime factor of 'n' and we are finding its sum
n=n/i;
else
i++;
return sum;
int n=sc.nextInt();
if(a==b)
else
/**Output:
1. Enter a Number : 94
Sum of Digit = 13
It is a Smith Number
Sum of Digit = 3
Sum of Digit = 42
It is a Smith Number
*/
import java.io.*;
class Date_Difference
{
static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int month[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int isLeap(int y)
if((y%400==0) || ((y%100!=0)&&(y%4==0)))
return 29;
else
return 28;
month[2]=isLeap(y);
return false;
else
return true;
*/
int dn=0;
month[2]=isLeap(y);
for(int i=1;i<y;i++)
if(isLeap(i)==29)
dn=dn+366;
else
dn=dn+365;
for(int i=1;i<m;i++)
dn=dn+month[i];
dn=dn+d;
return dn;
String date1=br.readLine().trim();
int p,q;
p=date1.indexOf("/");
int d1=Integer.parseInt(date1.substring(0,p));
q=date1.lastIndexOf("/");
int m1=Integer.parseInt(date1.substring(p+1,q));
//Extracting the year
int y1=Integer.parseInt(date1.substring(q+1));
String date2=br.readLine().trim();
p=date2.indexOf("/");
int d2=Integer.parseInt(date2.substring(0,p));
q=date2.lastIndexOf("/");
int m2=Integer.parseInt(date2.substring(p+1,q));
int y2=Integer.parseInt(date2.substring(q+1));
int a=ob.dayno(d1,m1,y1);
int b=ob.dayno(d2,m2,y2);
else
System.out.println("Invalid Date");
following property:
Some keith numbers are: 14 ,19, 28 , 47 , 61, 75, 197, 742, 1104,1537……………
*/
import java.io.*;
class Keith
int n=Integer.parseInt(br.readLine());
int copy=n;
String s=Integer.toString(n);
int d=s.length();
arr[i]=copy%10;
copy=copy/10;
int i=d,sum=0;
while(sum<n)
sum = 0;
for(int j=1; j<=d; j++)
sum=sum+arr[i-j];
i++;
/* When the control comes out of the while loop, either the
else
/**Output:
*/
Test your program with the sample data and some random data:
Example 1:
OUTPUT:Length: 6
Rearranged Sentence:
Example 2:
OUTPUT:
Length: 4
*/
import java.io.*;
class WordsInSentence
char ch;
while(true)
{
flag=0;
str=br.readLine();
len= str.length();
words=new String[len];
for(i=0;i<len;i++)
if(Character.isLowerCase(str.charAt(i)))
flag=1;
break;
if (flag==0)
break;
else
i=0;
while(i<len)
ch=str.charAt(i);
words[c]=stk;
c++;
i++;
stk="";
else
{
stk+=ch;
i++;
for(i=0;i<c;i++)
for(j=0;j<c-1;j++)
if((words[j].compareTo(words[j+1]))>0)
stk=words[j];
words[j]=words[j+1];
words[j+1]=stk;
System.out.println("Length= "+c);
System.out.println("\nRearranged Sentence:\n");
for(i=0;i<c;i++)
System.out.print(words[i]+" ");
ob.take();
Test your program with the sample data and some random data:
Example 1
INPUT : M = 3
4 16 12
8 2 14
4 1 3
OUTPUT :
ORIGINAL MATRIX
4 16 12
8 2 14
4 1 3
12 16 4
14 2 8
3 1 4
Example 2
INPUT : M = 22
OUTPUT : SIZE OUT OF RANGE
*/
import java.io.*;
class ImageMatrix_ISC2013
int m=Integer.parseInt(br.readLine());
for(int i=0;i<m;i++)
for(int j=0;j<m;j++)
A[i][j]=Integer.parseInt(br.readLine());
System.out.println("*********************");
System.out.println("*********************");
for(int i=0;i<m;i++)
for(int j=0;j<m;j++)
{
System.out.print(A[i][j]+"\t");
System.out.println();
for(int i=0;i<m;i++)
int k=0;
for(int j=m-1;j>=0;j--)
B[i][k]=A[i][j];
k++;
System.out.println("*********************");
System.out.println("*********************");
for(int i=0;i<m;i++)
for(int j=0;j<m;j++)
System.out.print(B[i][j]+"\t");
System.out.println();
else
}
}
/*Output:
10
11
12
13
14
15
16
*******
*******
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
*******
*******
4 3 2 1
8 7 6 5
12 11 10 9
16 15 14 13
*/
Composite number:
For example: 10
Factors are: 1, 2, 5, 10
Magic number:
digits is equal to 1
Test your program with the sample data and some random data:
Example 1:
INPUT:
m = 10
n = 100
OUTPUT:
Example 2:
INPUT:
m = 1200
n = 1300
OUTPUT:
Example 3:
INPUT:
m = 120
n = 99
OUTPUT:
INVALID INPUT
*/
import java.io.*;
class MagicComposite_ISC2014
boolean isComposite(int n)
int count=0;
for(int i=1;i<=n;i++)
if(n%i==0)
count++;
if(count>2)
return true;
else
return false;
int sumDig(int n)
int d,s = 0;
while(n>0)
d=n%10;
s = s +d;
n = n/10;
return s;
int a = sumDig(n);
while(a>9)
a = sumDig(a);
}
if(a == 1)
return true;
else
return false;
(new InputStreamReader(System.in));
int m=Integer.parseInt(br.readLine());
int n=Integer.parseInt(br.readLine());
int c=0;
if (m<n)
if (c==0)
System.out.print(i);
else
System.out.print(", "+i);
c++;
else
System.out.println("OUT OF RANGE");
/*Output:
*/
For example:
1. 0201103311:
10 * 0 + 9 * 2 + 8 * 0 + 7 * 1 + 6 * 1 + 5 * 0 + 4 * 3 +
3 * 3 + 2 * 1 + 1 * 1 = 55.
2. 007462542X:
10 * 0 + 9 * 0 + 8 * 7 + 7 * 4 + 6 * 6 + 5 * 2 + 4 * 5 + 3
* 4 + 2 * 2 + 1 * 10 = 176.
a valid ISBN.
3. 0112112425:
10 * 0 + 9 * 1 + 8 * 1 + 7 * 2 + 6 * 1 + 5 * 1 + 4 * 2 + 3 * 4 + 2 * 2 + 1 * 5 = 71.
a valid ISBN.
Verify the code for its validity in the format specified below:
Test your program with sample data and some random data.
Example 1:
OUTPUT:
SUM = 99
Example 2:
INPUT CODE: 035680324
Example 3:
OUTPUT:
SUM = 122
*/
import java.io.*;
class ISBN
isbn = isbn.trim().toUpperCase();
int sum = 0;
int n = 10;
if(len != 10)
System.out.println("INVALID INPUT");
return;
if(!(last >= '0' && last <= '9') && last != 'X')
{
System.out.println("INVALID INPUT");
return;
char ch = isbn.charAt(i);
wrong = true;
break;
else
int d = Integer.parseInt(Character.toString(ch));
sum += d * n;
n--;
if(wrong)
System.out.println("INVALID INPUT");
return;
if(last == 'X')
sum += 10;
else
sum += Integer.parseInt(Character.toString(last));
System.out.println("SUM = " + sum);
if(sum % 11 == 0)
else
either ‘.’ ‘?’ or ‘!’ only. Any other character may be ignored.
The words may be separated by more than one blank space and are in
UPPER CASE.
(a)Accept the sentence and reduce all the extra blank space between
(b)Accept a word from the user which is part of the sentence along
with its position number and delete the word and display the sentence.
Test your program with the sample data and some random data:
Example 1
WORD TO BE DELETED: IS
WORD TO BE DELETED: SO
Example 3
*/
import java.util.*;
class RemoveWord_ISC2014
String s = sc.nextLine();
s = s.toUpperCase();
int l = s.length();
System.out.println("Invalid Input. End a sentence with either '.', '?' or '!' only");
else
int c = str.countTokens();
int x = sc.nextInt();
if(x<1 || x>c)
else
w = str.nextToken();
if(w.equals(del)==true && i == x)
continue;
}
System.out.print("Output : "+ans.trim()+last);
/*Output:
1. Enter any sentence : A MORNING WALK IS A IS BLESSING FOR THE WHOLE DAY.
*/
/**Question:
correct answer.
Note: Array entries are line fed (i.e. one entry per line)
Test your program with the sample data and some random data:
Example 1
INPUT : N = 5
Participant 1 D A B C C
Participant 2 A A D C B
Participant 3 B A C D B
Participant 4 D A D C B
Participant 5 B C A D D
Key: B C D A A
OUTPUT : Scores :
Participant 1 = 0
Participant 2 = 1
Participant 3 = 1
Participant 4 = 1
Participant 5 = 2
Example 2
INPUT : N = 4
Participant 1 A C C B D
Participant 2 B C A A C
Participant 3 B C B A A
Participant 4 C C D D B
Key: A C D B B
OUTPUT : Scores :
Participant 1 = 3
Participant 2 = 1
Participant 3 = 1
Participant 4 = 3
Highest score:
Participant 1
Participant 4
Example 3
INPUT : N = 12
*/
import java.util.*;
class QuizResult_ISC2017
char A[][],K[];
int S[],n;
void input()
n = sc.nextInt();
if(n<4 || n>10)
System.exit(0);
A = new char[n][5];
S = new int[n];
A[i][j] = sc.next().charAt(0);
K[i] = sc.next().charAt(0);
void CalcScore()
S[i] = 0;
if(A[i][j] == K[j])
S[i]++;
}
}
void printScore()
int max = 0;
System.out.println("\nSCORES : ");
if(S[i]>max)
System.out.println();
for(int i=0;i<n;i++)
if(max==S[i])
ob.input();
ob.CalcScore();
ob.printScore();
/**Question:
Write a program to declare a square matrix A[][] of order (M x M)
where ‘M’ must be greater than 3 and less than 10. Allow the user
Example 1
INPUT :M = 4
9 2 1 5
8 13 8 4
15 6 3 11
7 12 23 8
OUTPUT:
ORIGINAL MATRIX
9 2 1 5
8 13 8 4
15 6 3 11
7 12 23 8
REARRANGED MATRIX
9 2 1 5
8 3 6 4
15 8 13 11
7 12 23 8
DIAGONAL ELEMENTS
9 5
3 6
8 13
7 8
import java.util.*;
class SortNonBoundary_ISC2016
int A[][],B[],m,n;
m=sc.nextInt();
if(m<4 || m>10)
System.out.println("Invalid Range");
System.exit(0);
else
A = new int[m][m];
n = (m-2)*(m-2);
for(int i=0;i<m;i++)
for(int j=0;j<m;j++)
A[i][j]=sc.nextInt();
*/
void convert(int s)
int x=0;
for(int i=0;i<m;i++)
for(int j=0;j<m;j++)
if(s==1)
B[x] = A[i][j];
else
A[i][j] = B[x];
x++;
void sortArray()
int c = 0;
if(B[i]>B[j])
c = B[i];
B[i] = B[j];
B[j] = c;
void printArray()
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
System.out.print(A[i][j]+"\t");
System.out.println();
void printDiagonal()
int sum = 0;
for(int i=0;i<m;i++)
for(int j=0;j<m;j++)
if(i==j || (i+j)==m-1)
System.out.print(A[i][j]+"\t");
else
System.out.print("\t");
System.out.println();
{
SortNonBoundary_ISC2016 ob = new SortNonBoundary_ISC2016();
ob.input();
System.out.println("*********************");
System.out.println("*********************");
ob.convert(1);
ob.sortArray();
ob.convert(2);
System.out.println("*********************");
System.out.println("*********************");
System.out.println("*********************");
System.out.println("*********************");
ob.printDiagonal();
/* Output:
Enter a value : 9
Enter a value : 2
Enter a value : 1
Enter a value : 5
Enter a value : 8
Enter a value : 13
Enter a value : 8
Enter a value : 4
Enter a value : 15
Enter a value : 6
Enter a value : 3
Enter a value : 11
Enter a value : 7
Enter a value : 12
Enter a value : 23
Enter a value : 8
*********************
*********************
9 2 1 5
8 13 8 4
15 6 3 11
7 12 23 8
*********************
*********************
9 2 1 5
8 3 6 4
15 8 13 11
7 12 23 8
*********************
*********************
9 5
3 6
8 13
7 8
odd primes.
numbers.Example:
6=3+3
10 = 3 + 7
10 = 5 + 5
Hence, 6 has one odd prime pair 3 and 3. Similarly, 10 has two
N < 50. Find all the odd prime pairs whose sum is equal to
Test your program with the following data and some random data:
Example 1:INPUT:N = 14
Example 2:INPUT:N = 30
7, 23
11, 19
13, 17
Example 3:
INPUT:
N = 17
OUTPUT:
INPUT:
N = 126
OUTPUT:
*/
import java.util.*;
class Goldbach
boolean isPrime(int n)
int f = 0;
if(n % i == 0)
f++;
if(f == 2)
return true;
else
return false;
int n;
int p = 3;
int q = 0;
n =sc.nextInt();
if(n % 2 != 0)
{
System.exit(0);
System.exit(0);
while(p < n)
q = n - p;
p =p+ 2;
Step 2: LET P = 3
Thus Exit.
Thus Exit.
Step 6: Let Q = N - P
then Print P, Q
Step 8: Let P = P + 2*/
/**
Test your program with the following data and some random data:
Example 1:
INPUT:
YEAR: 2018
OUTPUT:
Example 2:
INPUT:
YEAR: 2018
OUTPUT:
DATE: 26TH DECEMBER, 2018
Example 3:
INPUT:
YEAR: 2018
OUTPUT:
Example 4:
INPUT:
YEAR: 2018
OUTPUT:
*/
import java.io.*;
class FindDate2019
int n = Integer.parseInt(br.readLine());
System.out.println("INVALID YEAR.");
if(dayNum==366&&(isLeapYear(year)==false))
System.exit(0);
int m[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if(isLeapYear(year))
m[1] = 29;
String mn[] = {"JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY", "AUGUST",
"SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"};
int i = 0;
i++;
System.out.print("ST ");
else if((dayNum % 10 == 2 && dayNum < 10) ||(dayNum % 10 == 2 && dayNum > 12))
System.out.print("ND ");
else if((dayNum % 10 == 3 && dayNum < 10)||(dayNum % 10 == 3 && dayNum > 13))
System.out.print("RD ");
else
System.out.print("TH ");
i++;
if(i==12&&dayNum > 0)
i = 0;
year=year+1;
System.out.print("ST ");
else if((dayNum % 10 == 2 && dayNum < 10) ||(dayNum % 10 == 2 && dayNum > 12))
System.out.print("ND ");
else if((dayNum % 10 == 3 && dayNum < 10)||(dayNum % 10 == 3 && dayNum > 13))
System.out.print("RD ");
else
System.out.print("TH ");
if((y%400==0) || ((y%100!=0)&&(y%4==0)))
return true;
else
return false;
Example:
6-- 1+2+3=6,where 1,2,and 3 are factors of 6,excluding itself.
Methods/Member functions:
technique.
*/
import java.io.*;
class Perfect
int num;
Perfect(int n)
num = n;
}
int sumOfFactors(int i)
if(i == 1)
else
return 0;
void check()
if(num == sumOfFactors(1))
else
int n = Integer.parseInt(br.readLine());
obj.check();