Program 1:To Sort an Array Using

Selection Sort
import java.io.*;
class SelectionSort
{int n,i;
int a[] = new int[100];
public SelectionSort(int nn) //parameterized constructor
{n=nn;
}
public void input() throws IOException //function accepting array elements
{BufferedReader br =new BufferedReader(new
InputStreamReader(System.in));
System.out.println("enter elements");
for(i=0;i<n;i++)
{a[i] = Integer.parseInt(br.readLine());
}}
public void display() //function displaying array elements
{System.out.println();
for(i=0;i<n;i++)
{System.out.print(a[i]+" ");
}}
public void sort() //function sorting array elements using selection sort
technique
{int j,temp,min;
for(i=0;i<n-1;i++)
{min =i;
for(j=i+1;j<n;j++)
{if(a[j]<a[min])
min =j;
}
if(min!=i)
{temp = a[i];
a[i] =a[min];
a[min] = temp;
}}}
public static void main(String args[]) throws IOException //main function
{SelectionSort x = new SelectionSort(5);
x.input();
System.out.print("Before sorting - ");
x.display();
System.out.print("After sorting - ");
x.sort();
x.display();
}}

Sample Output

Program 2: To Display Date From
Entered Day Number
import java.io.*;
class Day2Date
{
static BufferedReader br =new BufferedReader(new
InputStreamReader(System.in));
public void calc(int n, int yr) //function to calculate date
{
int a[ ] = { 31,28,31,30,31,30,31,31,30,31,30,31 } ;
String m[ ] = { "Jan", "Feb",
"Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" } ;
if ( yr % 4 == 0)
a[1] =29;
int t=0,s=0;
while( t < n) //loop calculating date
{
t =t + a[s++];
}
int d = n + a[--s] - t;
if( d == 1|| d == 21 || d == 31 )
{
System.out.println( d + "st" + m[s] + " , "+yr);
}
if( d == 2 || d == 22 )
{
System.out.println( d + "nd" + m[s] + " , "+yr);
}
if( d == 3|| d == 23 )
{
System.out.println( d + "rd" + m[s] + " , "+yr);
}
else {System.out.println( d + "th" + m[s] + " , "+yr);
}}
public static void main(String args[]) throws IOException //main function
{
Day2Date obj = new Day2Date();
System.out.println( "Enter day no = "); //accepting day no.
int n = Integer.parseInt(br.readLine());
System.out.println( "Enter year = "); //accepting year
int yr = Integer.parseInt(br.readLine());
obj.calc(n,yr);
}}

Sample Output .

}} . l =0. } public void readword() throws IOException //function to read input string {System.in)).out.io. c[j] = c[j+1]. char c[] = new char[100].readword(). str = br.readLine()."). c[j+1] = temp.i++) {c[i]= str.display().charAt(i).println(). obj.length(). class Alpha {String str.out. for(int i=0.i<l-1.out. }}}} public void display() //function to display the rearranged string {System.Program 3: To Display the Entered String in Alphabetical Order import java. int l. BufferedReader br =new BufferedReader(new InputStreamReader(System. obj.i++) {System. char temp.j.i<l.j<l-1-i.j++) {if(c[j] > c[j+1]) {temp = c[j]. }} public static void main(String args[]) throws IOException //main function {Alpha obj = new Alpha(). obj.i++) //loops for swapping of characters {for(j=0.i<l. } public void arrange() //function to arrange string in ascending order {int i.*.arrange(). for(i=0.println("enter word . public Alpha() //Alpha() constructor {str = "". } for(i=0. l = str.print(c[i]).

Sample Output .

print("Enter the lower range : ").out.isPrime(i+2) == true) { System. int q = Integer.in)).Program 4: To print Twin Prime Numbers within a range import java. i++) { if(ob.print("("+i+". i<=n. class TwinPrimeRange { boolean isPrime(int n) //funton for checking prime { int count=0. i<=(q-2). else return false. } } } } } . int p = Integer.parseInt(br.print("Enter the upper range : ").parseInt(br.*.readLine()). BufferedReader br=new BufferedReader(new InputStreamReader(System. System.out."+(i+2)+") "). } if(count == 2) return true. else { System.println("Invalid Range !"). for(int i=p. System. for(int i=1.println("\nThe Twin Prime Numbers within the given range are : ").out. if(p>q) System.out. i++) { if(n%i == 0) count++. } public static void main(String args[]) throws IOException { TwinPrimeRange ob = new TwinPrimeRange().readLine()).out.isPrime(i) == true && ob.io.

19) (29.5) (5.139) (149.13) (17.103) (107.43) (59.Sample Output Enter the lower range : 1 Enter the upper range : 200 The Twin Prime Numbers within the given range are : (3.7) (11.73) (101.193) (197.61) (71.151) (179.199) Program 5: To generate Fibonacci strings .181) (191.31) (41.109) (137.

import java. z = "" is sufficient } void accept()throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System. "+z).io.print(x). // mentioned in the question otherwise not required.out.parseInt(br.out. n = Integer. for(int i=3.in)). } void generate() { System.out.generate().out. System.print(x+". } } } public static void main(String args[]) throws IOException { FiboString ob = new FiboString().print("\nThe Fibonacci String Series is : ").print("\nEnter the number of terms : "). System. y = "b". y = z.z. else // If no of terms is more than or equal to 2 { System. x = y.*. int n. z = "ba". ob.accept().out. i++) { z = y+x. if(n <= 1) // If no of terms is less than or equal to 1 System.print(". class FiboString { String x. "+y). FiboString() // Constructor { x = "a". i<=n. } } Sample Output . ob.y.readLine()).

babba of terms : 2 . bab. ba. b 2) Enter the number of terms : 5 The Fibonacci String Series is : a. b.1) Enter the number The Fibonacci String Series is : a.

else return false.io. } int sumDig(int n) // Function to return sum of digits of a number { int s = 0. } boolean isMagic(int n) // Function to check for Magic number { int a = sumDig(n).*. } if(a == 1) . while(n>0) { s = s + n%10. } return s.i<=n. class MagicComposite_ISC2014 { boolean isComposite(int n) // Function to check for Composite number { int count=0.Program 6: To print a composite magic number import java.i++) { if(n%i==0) count++. } if(count>2) return true. n = n/10. for(int i=1. while(a>9) { a = sumDig(a).

out.print(i).isComposite(i)==true && ob.out.println("nThe frequency of Composite Magic Integers is : "+c). else return false.print("Enter the upper limit(n) : "). } public static void main(String args[])throws IOException { MagicComposite_ISC2014 ob = new MagicComposite_ISC2014(). c++. } else System.out.println("OUT OF RANGE"). System. else System.isMagic(i)==true) { if (c==0) // Printing the first number without any comma System.out. } } System. System. } } . i<=n.readLine()).print(". int n=Integer. if (m<n) { System.return true. int m=Integer. int c=0.print("Enter the lower limit(m) : "). i++) { if(ob.parseInt(br.in)). BufferedReader br=new BufferedReader (new InputStreamReader(System.out.out. "+i).println("The Composite Magic Integers are: ").out. for(int i=m.readLine()).parseInt(br.

1288 The frequency of Composite Magic Integers is : 9 . 1216. 1234. 1270.Sample Output Enter the lower limit(m) : 1200 Enter the upper limit(n) : 1300 The Composite Magic Integers are: 1207. 1225. 1261. 1243. 1252.

with the first ‘d’ terms being the decimal digits of the number N.io. For example. class Keith { public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System. } int i=d. //inputting the number int n=Integer. 57. 17. 7.Program 7: To check for Keith number A Keith Number is an integer N with ‘d’ digits with the following property: If a Fibonacci-like sequence (in which each term in the sequence is the sum of the ‘d’ previous terms) is formed. then N itself occurs as a term in the sequence.*. System. 33. //storing the digits of the number in the array copy=copy/10.parseInt(br. //array for storing the terms of the series for(int i=d-1.. 197. ………. //finding the number of digits (d) in the number int arr[]=new int[n]. 197 is a Keith number since it generates the sequence 1. i--) { arr[i]=copy%10.toString(n).in)).length().readLine()). import java.out.sum=0. int copy=n. 107.print("Enter the number : "). int d=s. i>=0. while(sum<n) //finding the sum till it is less than the number { . 9. String s=Integer.

println("The number is a not a Keith Number"). then it is a Keith number System.println("The number is a Keith Number"). } arr[i]=sum. either the sum is equal to the number or greater than it */ if(sum==n) //if sum is equal to the number. j<=d. for(int j=1.sum = 0. }} Sample Output Enter the number : 197 The number is a Keith Number Enter the number : 14 The number is a Keith Number Enter the number : 53 The number is a not a Keith Number . //storing the sum in the array i++. j++) //loop for generating and adding the previous 'd' terms { sum=sum+arr[i-j].out.out. } /* When the control comes out of the while loop. else System.

i<n. } } } } . //inputting the number int n=Integer.out. we will be printing the series of consecutive numbers only if the sum is equal to that number */ if(sum==n) { for(int k=i.parseInt(br. System.out.*. then either the sum is equal to that number or is greater than that number So. else System. } /* when the above while condition is false. /* adding consecutive natural numbers till sum is less than the given number */ while(sum<n) {sum=sum+j.print(k).Program 8: To print the possible consecutive number combinations import java.i++) { sum=i.out. for(int i=1.k++) { if(k==i) System.c=0.k<j.out.println().in)). j=i+1.j=0. j++.print(" + "+k).readLine()). int sum=0.io.print("Enter a number : "). class ISC06PQ1 { public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System. } System.

. class RemoveDupChar { public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System. Enter a number : 15 1+2+3+4+5 4+5+6 7+8 2.io. Enter a number : 95 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 17 + 18 + 19 + 20 + 21 47 + 48 Program 9: To remove repeated characters in a word import java.in)).*.Sample Output 1.

// Variable to store the final result char ch1. // Extracting the first character ch2=s.System.ch2. i++) { ch1=s.println("Word after removing repeated characters = "+ans). // Extracting the next character // Adding the first extracted character to the result if the current and the next characters are different if(ch1!=ch2) { ans = ans + ch1.charAt(i+1). i<l-1. s = s + " ".out.out. // Finding the length of the word String ans="".readLine(). // Inputting the word String s = br.charAt(i). // Adding a space at the end of the word int l=s.print("Enter any word: "). } } System. for(int i=0.length(). // Printing the result } } Sample Output Example Enter any word: Word after removing repeated characters = Java Example Enter any word: Word after removing repeated characters = isfun 1: Jaaavvvvvvvvaaaaaaaaaaa 2: iiiiiisssssfffffffffffffuunnnnn .

.io. while(n>1) { if(n%i==0) . 22. The first few such numbers are 4. n=n/10.in)). //function for finding sum of digits int sumDig(int n) { int s=0. while(n>0) { s=s+n%10. 94. sum=0. 27. class Smith { static BufferedReader br=new BufferedReader(new InputStreamReader(System. } return s.Program 10: To check for Smith number A Smith number is a composite number. 58. import java. } //function for generating prime factors and finding their sum int sumPrimeFact(int n) { int i=2. the sum of whose digits is the sum of the digits of its prime factors obtained as a result of prime factorization (excluding 1). 85.*. 121 ……………….

println("Sum of Prime Factor = "+b).print("Enter a Number : ").out. int a=ob. }} Sample Output 1. Enter Sum Sum of It is a Smith Number a of 2. int n=Integer. //Here 'i' is the prime factor of 'n' and we are finding its sum n=n/i.out.println("Sum of Digit = "+a). } else i++.sumPrimeFact(n). System. } public static void main(String args[]) throws IOException { Smith ob=new Smith().out.print("It is Not a Smith Number").out. //finding sum of prime factors System. System.sumDig(n). if(a==b) System. Enter a Sum of Sum of It is Not a Smith Number 3. else System. } return sum.readLine()).print("It is a Smith Number").out.{ sum=sum+sumDig(i). Enter Sum Sum of It is a Smith Number a of Prime Number Digit : = Factor Number Digit Prime Prime = 102 3 13 = 4937775 42 42 : = Factor Number Digit = 94 13 13 : Factor = .parseInt(br.// finding sum of digit int b=ob.

out.println(“Enter a no.print(pas[j]+" ").”). pas[0] = 1.println( ).readLine()).out. //printing the Pascal Triangle elements System. class Pascal {public void pascalw()throws IOException //pascalw() function {BufferedReader br=new BufferedReader(new InputStreamReader(System. j--) pas[j]=pas[j]+pas[j-1]. j<=i. }}} .in)).out.parseInt(br. i++) //loop evaluating the elements {for (int j=0. j>0.io. System. ++j) System. for (int i=0. //accepting value int [ ] pas = new int [n+1]. for (int j=i+1.Program 11: To create a Pascal’s triangle import java. i<n.*. int n=Integer.

io. (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 import java.*.Program 12:To remove words (a) Accept the sentence and reduce all the extra blank space between two words to a single blank space. class RemoveWord_ISC2014 { public static void main(String args[])throws IOException .

if(x<1 || x>c) // Checking whether integer inputted is acceptable or not { System.print("Enter the word to delete : ").length.charAt(l-1). // Saving the words in an array using split() int c = word. // Inputting the sentence String s = br.toUpperCase(). // String variable to store the final result char last = s.toUpperCase().?! ]+").out.out.in)).length().parseInt(br. String del = br.'.' || last == '?' || last == '!') { String word[]=s.println("Sorry! The word position entered is out of range"). int x = Integer. } else { for(int i=0.out.print("Enter the word position in the sentence : "). String ans="". // Extracting the last character /* Checking whether the sentence ends with '. i<c.readLine().{ BufferedReader br=new BufferedReader (new InputStreamReader (System. i++) . System.split("[. // Finding the number of words System.readLine()).out. s = s. '?' or a '!' or not */ if(last == '. System.print("Enter any sentence : "). // Converting the sentence into Upper Case int l = s.readLine(). del = del.

’. End a sentence with either ‘. 2.{ /* Skipping if the word to delete and the position matches */ if(word[i].print("Output : "+ans.trim()+last).out. Enter any sentence : STUDY OUTPUT : Invalid Input.equals(del)==true && i == x-1) continue. '?' or '!'").println("Invalid Input.out. } } } Output: 1. } System. Enter the word Enter the word position is the sentence : 6 to delete : IS Output : A MORNING WALK IS A BLESSING FOR THE WHOLE DAY. } } else { System. ans = ans + word[i] + " ".'. Enter any sentence : A MORNING WALK IS A IS BLESSING FOR THE WHOLE DAY. End a sentence with either '. ‘?’ or ‘!’ WELL ## .

Program 13:To decode an encrypted code A simple encryption system uses a shifting process to hide a message. Double Q (QQ) was selected because no English word ends in Q or contains QQ. Text : A B C D E F G H I J K L M N O P Q R S T U V W X Y Z Code: U V W X Y Z A B C D E F G H I J K L M N O P Q R S T Fist an extra space is added to the end of the string. spaces within the original text are replaced with QQ before the text is encrypted. etc. For example a shift of 7 means that A = U. To make things little more difficult.C = W.i e. The value of the shift can be in the range 1 to 26. B =V. .

// inputting coded text String s = br. // converting the coded text into Uppercase s = s + " ".println("!!! Invalid Shift Value !!!"). else { System.parseInt(br.out. else { int a.out. .toUpperCase().readLine(). s = s. int l = s.io.length(). public class Decode_ISC2003 { public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System. import java. The last block might not contain six characters.Additionally the coded message is printed in blocks of six characters separated by spaces. System.print("Enter the Shift Value : "). b. // adding a space at the end if(l>=100) // checking whether length of inputted code is less than 100 System.print("Enter Coded Text : ").println("!!! Invalid Length of Coded Text !!!").*.out. int shift = Integer.in)).out. if(shift<1 || shift>26) // checking whether shift value is between 1 and 26 System.readLine()).

ch2.1. * then we subtract 26 from it. } /* If ASCII value after adding the shift becomes more than 90. i<l.charAt(i+1). i++) { ch1 = s. * then ch1 + shift .charAt(i). String dec="". // extracting the next character /* Below we are adding shift value to the characters * if ch1 = 'A' and shift = 7.1. // storing ASCII values after adding shift to the next character /* If the currrent character and the next character are both 'Q' then we have a 'space' * hence the ASCII value should be 32 */ if((char)a == 'Q' && (char)b == 'Q') { a = 32. * eg. but we want 'A' whose ASCII value is 65 * so 91-26 will give us 65 */ if(a>90) . to make it circular.char ch1. i++. //new String for storing the decoded text for(int i=0. 'U'+7-1 = 85+7-1 = 91. // storing ASCII values after adding shift to the current character b = ch2 + shift . // extracting characters one by one ch2 = s.1 will give us: 'A'+7-1 = 65+7-1 = 71 * which is the ASCII value of 'G' */ a = ch1 + shift .

26. Text Shift : Value 3: UHINBYLKKQCHHYLKK : 27 Program 14:To print given time in words import java.a = a . } } }} Output: Example Enter Coded Enter the Decoded Text : ANOTHER WINNER Text Shift : Value 1: UHINBYLKKQCHHYLKK : 7 Example Enter Coded Enter the Decoded Text : BEST OF LUCK Text Shift : Value 2: RUIJGGEVGGBKSAGG : 11 Example Enter Coded Enter the Invalid Shift Value. public class TimeInWords_ISC2003 { public static void main(String args[])throws IOException .out.io.*. // finally adding the decoded character to the new String } System.println("Decoded Text : "+dec). if(ch1 != ' ') dec = dec + (char)a.

if((h>=1 && h<=12) && (m>=0 && m<=59)) // checking whether given input is legal or not. int h=Integer."Twelve". "Twenty nine"}. "Twenty three". int m=Integer. /* The below code is for finding whether to print the word 'minute' or 'minutes' */ String plu."Fifteen". "Twenty two". "Four".out. we face a problem at hour = 12 * because if we print an hour ahead of 12. "Eight". "Twenty five".readLine()).parseInt(br.print("Enter Minutes : ")."Twenty seven". "Twenty". it will be thirteen. if(m == 1) plu = "Minute".readLine()). "Two"."Fourteen"."Twenty eight".in)). "Eleven"."Seventeen".print("Enter Hours : ")."Sixteen". "One"."Nineteen". "Five"."Thirteen". else plu = "Minutes". /* Inputting hours and minutes */ System.parseInt(br."Twenty one". a. "Six". System. "Twenty six". but we want 1 * so the below code checks this & decides what hour to print in words when minutes is from 31-59 */ if(h==12) . we print the hour ahead of the given hour * like 6:55 will be 5 minutes to 7 and not 5 minutes to 6 * when we print the hour ahead of the given hour. "Twenty four"."Eighteen". { /* creating an array containing numbers from 1-29 in words */ String words[]={"". "Nine". "Three".{ BufferedReader br=new BufferedReader(new InputStreamReader(System.out. /* When we have minutes from 31-59."Seven"."Ten".

println("Quarter past "+words[h]).print("Output : "+h+":"+m+" ----. } //end of outer if else System. then storing in words.println("Invalid Input !"). */ System.println(words[m]+" "+plu+" past "+words[h]). else if(m==45) System.a = words[1].out. else // condition for minutes between 31-59 System.println(words[60-m]+" "+plu+" to "+a).println(words[h]+" O' clock").out. //printing the given time in numbers if(m==0) System. //if hour is not 12.out. //storing 'one' when hour is 12 else a = words[h+1].").out.out.out.println("Quarter to "+a). else if(m==30) System. //printing error message for illegal input }} Output: Example Enter Hours Enter Minutes Output : 12:39 —– Twenty one Minutes to One : : 1: 12 39 . else if(m==15) System. an hour ahead of given hour /* The below code checks minutes and accordingly prints the time in words using array.out.out. else if(m<30) // condition for minutes between 1-29 System.println("Half past "+words[h]).

Example Enter Hours Enter Minutes Output : 12:39 —– Twenty one Minutes to One : : 2: 12 39 Example Enter Hours Enter Minutes Output : 7:16 —– Sixteen Minutes past Seven : : 3: 7 16 Example Enter Hours Enter Minutes Output : 5:30 —– Half past Five : : 4: 5 30 .

System. // Creating the Magic Matrix int i.t. import java. } } .j++) { A[i][j] = 0.in)). Write a program to enter an integer number ‘n’.io.k.println("Enter a number between 1 to 5 "). if(n>5) System.out. each column and each diagonal is same.i<n. int n=Integer.parseInt(br. else { int A[][]=new int[n][n]. Create a magic square of size ‘n*n’. /*Initializing every cell of the matrix with 0 */ for(i=0.i++) { for(j=0.j<n. class Magic_Matrix { public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.print("\n\nEnter the size of the matrix : ").j.out. if the sum of each row.Program 15:To print a magic square A square matrix is said to be a Magic Square.*.readLine()).

// Making one step upward j++. . i--. while(k<=n*n) { A[i][j] = k++. } if(i<0) // Wrapping around the row if it goes out of boundary i = n-1. } } } /* When the size of the matrix is even */ else { k = 1. k = 1. if(A[i][j]>0) // Condition when the cell is already filled { i = i+2. j--. j--. // Moving one step to the right if(i<0 && j>n-1) // Condition for the top-right corner element { i = i+2./* When the size of the matrix is Odd */ if(n%2!=0) { i=0. if(j>n-1) // Wrapping around the column if it goes out of boundary j = 0. j = n/2.

/* Filling the matrix with natural numbers from 1 till n*n */
for(i=0;i<n;i++)
{

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

A[i][j] = k++;

}

}

j = n-1;
for(i=0; i<n/2; i++)
{ /* swapping corner elements of primary diagonal */
t = A[i][i];
A[i][i] = A[j][j];
A[j][j] = t;
/* swapping corner elements of secondary diagonal */
t = A[i][j];
A[i][j] = A[j][i];
A[j][i] = t;
j--;
}

}

/* Printing the Magic matrix */
System.out.println("The Magic Matrix of size "+n+"x"+n+" is:");
for(i=0;i<n;i++)
{

for(j=0;j<n;j++)
{ System.out.print(A[i][j]+ "\t");
}

System.out.println();

}

Output

}

}

}

Program 16:To check the validity of an IMEI
number
The International Mobile Station Equipment Identity or IMEI is a number, usually unique, to
identify mobile phones, as well as some satellite phones. It is usually found printed inside the battery
compartment of the phone.
The IMEI number is used by a GSM network to identify valid devices and therefore can be used for
stopping a stolen phone from accessing that network.
The IMEI (15 decimal digits: 14 digits plus a check digit) includes information on the origin, model,
and serial number of the device.
The IMEI is validated in three steps:
1.

Starting from the right, double every other digit (e.g., 7 becomes 14).

2.

Sum the digits (e.g., 14 → 1 + 4).

3.

Check if the sum is divisible by 10.

import java.io.*;
class IMEI
{

int sumDig(int n) // Function for finding and returning sum of digits of a number
{
int a = 0;
while(n>0)
{ a = a + n%10;
n = n/10;
}

// Finding sum of the digits n = n/10.sumDig(d).toString(n). if(l!=15) // If length is not 15 then IMEI is Invalid System. } public static void main(String args[])throws IOException { IMEI ob = new IMEI(). i>=1. BufferedReader br=new BufferedReader(new InputStreamReader(System. long n = Long.out.length(). sum = 0. } System.parseLong(br. if(i%2 == 0) { d = 2*d.out. for(int i=15. // Converting the number into String for finding length int l = s.readLine()). else { int d = 0. // 15 digits cannot be stored in 'int' data type String s = Long.out. // Doubling every alternate digit } sum = sum + ob.println("Output : Sum = "+sum).return a.in)).print("Enter a 15 digit IMEI code : "). System. . i--) { d = (int)(n%10).println("Output : Invalid Input").

out.println("Invalid IMEI Code").out.println("Valid IMEI Code"). Enter a Output Invalid IMEI Code 15 3. Enter a Output : Invalid Input digit : digit : 15 digit IMEI Sum code IMEI Sum code IMEI code : = 654122487458946 80 = 799273987135461 79 : : 79927398713 . Enter a Output Valid IMEI Code 15 2. else System.if(sum%10==0) System. } }} Output: 1.

import java. System. class UniqueNumber { public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System. int n=Integer. 3121.out. For example 7.parseInt(br. 135.in)).*. 300 are not.print("Enter any number : ").io. .Program 17:To check whether a number is unique A Unique number is a positive integer (without leading zeros) with no duplicate digits.readLine()). 214 are all unique numbers whereas 33.

println("**** The Number is a Unique Number ****"). break. else System.j++) { if(s.charAt(i)==s. /* loop for checking whether there are repeated digits */ for(int i=0.i++) { for(int j=i+1.length().String s=Integer.println("**** The Number is Not a Unique Number ****"). then we know it is not a Unique Number { flag=1. } } } if(flag==0) System.j<l.toString(n).charAt(j)) //if any digits match. int flag=0. } } Output: Enter any number : 3121 **** The Number is Not a Unique Number **** .out.i<l-1. //converting the number into String form int l=s.out.

This number system consists of only eight basic digits i. 1.*. 4. For Example: 25 in the Decimal number system can be represented as 31 in the Octal number system. 2.e. 5. import java. 6 and 7.Enter any number : 5243 **** The Number is a Unique Number **** Program 18:To convert a decimal number to octal number Octal Number system is a number system which can represent a number in any other number system in terms of digits ranging from 0 to 7 only. 3. . 0.io.

*.readLine()).'1'. //adding the remainder to the result and reversing at the same time n=n/8. //finding remainder by dividing the number by 8 s=dig[r]+s.print("Enter a decimal number : "). int r. //variable for storing the result //array storing the digits (as characters) in the octal number system char dig[]={'0'. int n=Integer.out. }} Output: Enter a decimal number : 25 Output = 31 Enter a decimal number : 465 Output = 721 Program 19:To swap strings without a third variable import java.'6'.parseInt(br.'2'.'4'.'7'}.in)). while(n>0) { r=n%8.io.out.'3'. String s="". } System.println("Output = "+s).class Dec2Oct { public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader (new InputStreamReader(System. System.'5'. class Swap_Strings { public static void main(String args[])throws IOException .

out.out. int len1=s1.out. /*Swapping Process Ends*/ System.out.println("2nd String = "+s2). System.println("Strings After Swapping : "). } } Output: Enter the 1st String : Java For Enter the 2nd String : School is Fun ——————————Strings Before Swapping : 1st String = Java For . System. System.out.readLine().{ BufferedReader br=new BufferedReader (new InputStreamReader (System.out. System.out.println("Strings Before Swapping : ").length(). System.len1).out.substring(len1). /*Swapping Process Begins*/ s1=s1+s2.println("2nd String = "+s2).print("Enter the 2nd String : ").println("1st String = "+s1).print("Enter the 1st String : "). String s1=br.println("-------------------------------"). System.in)). System.println("1st String = "+s1).out.substring(0. s2=s1.readLine(). System.println("-------------------------------"). System. s1=s1.out. String s2=br.

2nd String = School is Fun ——————————Strings After Swapping : 1st String = School is Fun 2nd String = Java For .

String s = br. //storing all alphabets from 'a' till 'z' in alph[] array freq[i]=0. //intializing the count of every alphabet with '0' c++.toLowerCase(). //converting the string into lowercase int l=s. //finding the length of the string char alph[]=new char[26].Program 20: To find frequency of letters in a string import java. class Alphabet_Freq { public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System. .print("Enter any string: ").readLine(). i++) { alph[i]=c.in)).length(). //array for storing frequency of all alphabets char c='a'. i<26. //array for storing alphabets from 'a' to 'z' int freq[]=new int[26].out. s=s. for(int i=0. } char ch.io.*. System.

/* Counting frequency of alphabets begins below */ for(int i=0. //this is just for styling the look of the output System.println(" "+alph[i]+"\t\t } } } Output: Enter any string: ilovejavaforschool Output: ========================== Alphabet Frequency ========================== a 2 c 1 e 1 f 1 h 1 i 1 j 1 "+freq[i]). then 'b' and so on freq[i]++.out.println("==========================").println("Output:"). j<l. i<26.out.println("==========================").charAt(j).out. .System. i<26. //increasing count of those aplhabets which are present in the string } } for(int i=0. i++) { for(int j=0.out.println("Alphabet\tFrequency"). System. i++) { if(freq[i]!=0) //printing only those alphabets whose count is not '0' System. j++) { ch=s. //extracting characters of the string one by one if(ch==alph[i]) //first checking the whole string for 'a'. System.out.

io."Three"."Forty"."Nine"}."Nineteen"}. String ten[]={""."Four"."Thirteen"."Seventy"."Seventeen". else { int th=n/1000."".l o r s v 2 4 1 1 2 Program 21: To print a number(1-9999) in words import java.parseInt(br."Thirty". String unit[]={""."Fifty". String ty[]={""."Eight"."Eleven". //finding the digit at hundred's place ."Five".out.in))."Two".out. class Num2Word_ISC2011 { public static void main(String args[]) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System."Ten"."Ninety"}."Fourteen".print("Enter a Number : ").readLine()). /*checking whether the number is in the range [1-9999] or not*/ if(n<1 || n>9999) System."Twenty"."One". System. int n=Integer."Sixteen"."Twelve". "Eighteen"."Sixty"."Fifteen".*. //finding the digit at thousand's place int h=(n/100)%10.println("Out of Range")."Six"."Eighty"."Seven".

print(" "+ty[t]+" "+unit[u]). } }} Output: 1.out.print(" "+unit[h]+" Hundred"). System.int t=(n/10)%10. /*Condition for printing digit at ten's place*/ if(t==1) //When digit at ten's place is 1. is that it should not be zero*/ if(h!=0) System.print(" "+ten[u+1]). //finding the digit at unit's place System. Eleven etc. we have different words like Ten. /*Condition for printing digit at hundred's place. else //if it is not 1 then we print the words following a normal pattern System.print(unit[th]+" Thousand").out. /*Condition for printing digit at thousand's place.out. Enter a Number Output = Eight Thousand Three Hundred And Seven : : 129 8307 .out.print(" And"). //finding the digit at ten's place int u=n%10.print("Output = ").out. is that it should not be zero*/ if(th!=0) System. Enter a Output = One Hundred And Twenty Nine Number 2.out. /*Condition for printing the word "And"*/ if((t!=0 || u!=0)&&(th!=0 || h!=0)) System.

io. then n*n = 16. hence the array will be filled as given below. class Circular_Matrix { . Enter Out of Range a Number : 54987 Program 22: To print a circular(spiral) matrix For example: if n = 4. import java.*.3.

i++) { A[r1][i]=k++. r1=0.in)).i>=c1. c1=0. . c2=n-1.i--) { A[r2][i]=k++. System.out.j++) { A[j][c2]=k++.j>=r1+1. r2=n-1.j--) { } A[j][c1]=k++.j<=r2. int n=Integer. int A[][]=new int[n][n]. while(k<=n*n) { for(int i=c1. } for(int i=c2-1. } for(int j=r2-1.print("Enter the number of elements : "). } for(int j=r1+1.readLine()).public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System. int k=1.i<=c2.parseInt(br.

c1++. r2--.j<n.j++) { System. r1++.out.println("The Circular Matrix is:").i++) { for(int j=0. for(int i=0. c2--. } /* Printing the Circular matrix */ System.print(A[i][j]+ "\t").println(). } System.out.out.i<n. } Output: } } .

c.*.limit.in)). .b.Program 23:To print Fibonacci series using recursion import java.io. Recursion() //Constructor { a=0. class Recursion { static BufferedReader br=new BufferedReader(new InputStreamReader(System. int a.

for(int i=1. c=0.out. System. } void input()throws IOException //Function to input the limit { System. } . limit=0.readLine()).print("Enter the limit : ").i++) { c=fib(i). else return (fib(n-1)+fib(n-2)).b=1.out.out.i<=limit. } void generate_fibseries() //Function generating all the Fibonacci Series numbers upto 'n' terms { System. else if(n==2) return b.print(c+" "). limit=Integer.println("The Fibonacci Series is:").parseInt(br. } int fib(int n) //Recursive function generating the 'nth' term of Fibonacci Series { if(n<=1) return a.

} public static void main(String args[])throws IOException { Recursion ob=new Recursion(). ob. } } Output: Enter the limit : 11 The Fibonacci Series is: 0 1 1 2 3 5 8 13 21 34 55 Enter the limit : 20 The Fibonacci Series is: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 Program 24:To print Date in dd/mm/yyyy format import java.*.input(). . ob.in)).generate_fibseries().io. class Date_DDMMYY { public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.

//month in Integer format y = Integer. //array storing the month names String month[]={ "".31. yy. "July". "January".31.28. "September".e.31}. "November".substring(2. y. //array storing the maximum days of every month int maxdays[]={0. m.31. //inputting the date in String format l = date.31.30. "March". d. "February". //finding number of digits in the given input if(l==8) //performing the task only when number of digits is 8 { dd = date. month or year are outside acceptable limit").30. "October".println("The day. } /* checking whether the day. .parseInt(mm). "August".4).print("Enter any date in 8 digits (ddmmyyyy) format: "). "May". "June".out.readLine(). "April". mm. //day in Integer format m = Integer.substring(0.parseInt(yy). String date = br. there cannot be an input like 35012013 because 35/01/2013 is unacceptable*/ if(m<0 || m>12 || d<0 || d>maxdays[m] || y<0 || y>9999) // Performing Date Validation { System. //extracting the day in String format mm = date. //extracting the month in String format yy = date.substring(4).int l.parseInt(dd). String dd. "December" }. //extracting the year in String format d = Integer.length(). month and year are within acceptable range i.31. //year in Integer format if((y%400==0) || ((y%100!=0)&&(y%4==0))) // condition for leap year { maxdays[2]=29.30. System.out.30.31.2).

import java. month name. month name. yyyy format = 02 May.print("Date in dd.out. yyyy format = "+dd+" "+month[m]+". yyyy format = 12 November.*. month name. /* Second Part */ System. month or year are outside acceptable limit Program 25:To print the difference between 2 dates Accept 2 dates in the string format dd/mm/yyyy and find the difference in days between the 2 dates. "+yy).} else { /* First Part */ System.println("Date in dd/mm/yyyy format = "+dd+"/"+mm+"/"+yy). } } else System.println("Wrong Input"). 1963 3) Enter any date in 8 digits (ddmmyyyy) format: 252013 Wrong Input 4) Enter any date in 8 digits (ddmmyyyy) format: 29022013 The day.io. 2013 2) Enter any date in 8 digits (ddmmyyyy) format: 12111963 Date in dd/mm/yyyy format = 12/11/1963 Date in dd.out. .out. }} Output: 1) Enter any date in 8 digits (ddmmyyyy) format: 02052013 Date in dd/mm/yyyy format = 02/05/2013 Date in dd.

30. int m. int month[]={0. int y) .class Date_Difference { static BufferedReader br=new BufferedReader(new InputStreamReader(System.31.31.31. int m.31}. //function for checking for Leap Year int isLeap(int y) { if((y%400==0) || ((y%100!=0)&&(y%4==0))) return 29.in)). else return 28. } //function for checking date validation boolean dateValidate(int d.30.30. int y) { month[2]=isLeap(y).28.31.30. } //function for finding day number from year = 1 till the inputted year int dayno(int d. else return true.31. if(m<0 || m>12 || d<0 || d>month[m] || y<0 || y>9999) return false.31.

month[2]=isLeap(y). int p. for(int i=1.i<m. } dn=dn+d.out. for(int i=1.i<y. String date1=br.readLine(). else dn=dn+365.q. System.print("Enter the 1st date in (dd/mm/yyyy) format: "). .trim().i++) { if(isLeap(i)==29) dn=dn+366. } return dn. } public static void main(String args[])throws IOException { Date_Difference ob=new Date_Difference().{ int dn=0.i++) { dn=dn+month[i].

int m2=Integer.dateValidate(d2.out.m2. int m1=Integer.substring(q+1)).parseInt(date2.p)). p=date2.print("Output : Difference = "+Math.dateValidate(d1.y1). System.dayno(d1.substring(q+1)).m2.m1.abs(a-b)+" days.p)).y2)==true) { int a=ob.y1)==true && ob.trim().substring(0. //Validating both the dates if(ob.substring(p+1.readLine().substring(p+1.indexOf("/").parseInt(date2. int d1=Integer.lastIndexOf("/"). int y2=Integer.q)).substring(0. } else .parseInt(date1.lastIndexOf("/"). int d2=Integer.").parseInt(date2. //Extracting the month q=date1.parseInt(date1.m1.out.y2).//Extracting the day p=date1. //Extracting the year int y1=Integer.q)). System. q=date2.print("Enter the 2nd date in (dd/mm/yyyy) format: "). int b=ob.indexOf("/"). String date2=br.dayno(d2.parseInt(date1.

} } OUTPUT: Date 1 : 20/12/2012 Date 2 : 11/02/2013 Output:Difference = 54 days .out.println("Invalid Date").System.

} System. int n=Integer. int r.println("Output = "+s).out. //variable for storing the result char dig[]={'0'.Program 26:To convert Decimal numbers to binary numbers import java. }} Output: Enter a decimal number : 25 Output = 11001 Enter a decimal number : 47 Output = 101111 .print("Enter a decimal number : ").'1'}.readLine()). String s="".parseInt(br. class Dec2Bin { public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader (new InputStreamReader(System.io. //array storing the digits (as characters) in a binary number system while(n>0) { r=n%2.*. //adding the remainder to the result and reversing at the same time n=n/2.out. System.in)). //finding remainder by dividing the number by 2 s=dig[r]+s.

Enter a decimal number : 6 Output = 110 Program 27:To convert Decimal number to hexadecimal number Hexadecimal Number system is a number system which can represent a number in any other number system in terms of digits ranging from 0 to 9 and then A – F only.*.'4'.print("Enter a decimal number : "). 5. while(n>0) { r=n%16.'5'.'7'. D.'1'. This number system consists of only sixteen basic digits i.io.parseInt(br.'B'.'D'.'E'. String s="".out.println("Output = "+s).'8'. B. C.in)). System. 8. E and F.'F'}. int r. } System.'A'. 11 as B and so on till 15 which is represented as F. //variable for storing the result //array storing the digits (as characters) in a hexadecimal number system char dig[]={'0'.readLine()). 0.'2'. //adding the remainder to the result n=n/16.'6'.out.'9'.'3'. 7. 9. }} Output: Enter a decimal number : 47 Output = 2F . //finding remainder by dividing the number by 16 s=dig[r]+s. A. 3.'C'. class Dec2Hex { public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader (new InputStreamReader(System.e. 4. 6. 1. 2. int n=Integer. import java. Here 10 is represented as A.

for(int i=1.in)).) comma .) full stop.util. s = s + br.parseInt(br. System. (.) semicolon .io.*. //inputting the number of sentences to accept int n = Integer. import java.out. (:) colon and white space. class Sent_Merge_Rev { public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System. i<=n.print("Enter the number of sentences: ").*.print("Enter Sentence "+i+": "). (.readLine()).out. import java.Enter a decimal number : 1243 Output = 4DB Program 28:To input multiple sentences and reverse it without punctuation marks The input in this question will consist of a number of lines of English text consisting of the letters of the English alphabets. (. Write a program to print the words of the input in reverse order without any punctuation marks other than blanks. String s = "". //inputting multiple sentences and joining them in the same String } . the punctuation marks (‘) apostrophe.readLine(). i++) { System.

Enter Sentence 2: By Swami Vivekananda. Enter Sentence 3: Thanks to javaforschool! Output: javaforschool to Thanks easy really is computer ISC and ICSE know you Did .println("Output: "+rev).:!?"). String w="". //extracting one word at a time rev = w+" "+rev.out. Output: Vivekananda Swami By character is work to directed and controlled Emotions Example 2: Enter the number of sentences: 3 Enter Sentence 1: Did you know? Enter Sentence 2: ICSE and ISC computer is really easy. is character. int c=str.. controlled and directed to work.nextToken(). //joining the extracted words in reverse order } System. rev="". } } Output: Example 1: Enter the number of sentences: 2 Enter Sentence 1: Emotions.countTokens()." '. i<=c.. i++) { w = str. for(int i=1.//converting the sentence into StringTokenizer giving the given punctuation marks StringTokenizer str=new StringTokenizer(s.

(c)Find the sum of the elements of left diagonal and the sum of the elements of right diagonal of the matrix and display them. import java.readLine()).*. Allow the user to input integers into this matrix. int A[][]=new int[m][m]. Perform the following tasks: (a)Display the original matrix.out. Display an appropriate message for an invalid input. if(m>2 && m<10) // Checking for valid input of rows and columns size { . class SymetricMatrix_ISC2014 { public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)).Program 29:To check whether an array is a Symmetric matrix declare a square matrix A[ ] [ ] of order (M x M) where ‘M’ is the number of rows and the number of columns such that M must be greater than 2 and less than 10.print("Enter the number of elements : "). if the element of the ith row and jth column is equal to the element of the jth row and ith column. System. Accept the value of M as user input. A square matrix is said to be Symmetric. (b)Check if the given matrix is Symmetric or not.io.parseInt(br. int m=Integer.

System.i<m.i++) { for(int j=0. A[i][j]=Integer.println("nThe Original Matrix is : ").j++) { System.j++) .j<m.print("Enter the elements : ").out. } /* Checking whether the matrix is symmetric or not */ int flag = 0. for(int i=0.j<m.parseInt(br.j++) { System.println().i<m.out.i++) { for(int j=0.print(A[i][j]+"\t").i<m. for(int i=0.readLine()). } } /* Printing the Original Matrix */ System.i++) { for(int j=0. } System.out.println("nInputting the elements in the Matrix: n").out. for(int i=0.j<m.out.

i++) { for(int j=0.println("The sum of the left diagonal = "+ld).out. /* Finding sum of the diagonals */ int ld = 0. . } } } System. // Setting flag = 1 when elements do not match break. rd = 0.j++) { if(i == j) // Condition for the left diagonal { ld = ld + A[i][j].{ if(A[i][j] != A[j][i]) { flag = 1. for(int i=0.j<m.i<m. } } } if(flag == 1) System.out.println("nThe given Matrix is Not Symmetric"). else System.println("nThe given Matrix is Symmetric").out.out.println("The sum of the right diagonal = "+rd). } if((i+j) == (m-1)) // Condition for the right diagonal { rd = rd + A[i][j]. System.

out. }} Output: .println("The Matrix Size is Out Of Range").} else System.

Program 30:Inheritance .

specify the class Rank giving details of constructor().io. Record() . import java. int rnk[].in)). The details of both classes are given below: Class name : Record Data Members / instance variables: name[ ] : to store the names of students rnk[ ] : to store the ranks of students Member functions: Record() : constructor to initialize data members void readvalues() : to store names and ranks void display() : displays the names and the corresponding ranks Class name : Rank Data Members / instance variables: index : integer to store the index of the topmost rank Member functions Rank() : constructor to invoke the base class constructor and to initialize index to 0. String name[]. Using the concept of inheritance. Define a sub class Rank to find the highest rank along with the name. void highest() and void display().A super class Record has been defined to store the names and ranks of 5 students.*. class Record //superclass { BufferedReader br=new BufferedReader(new InputStreamReader(System. void readvalues(). void display(). Specify the class Record giving details of the constructor(). void highest() : finds the index location of the topmost rank and stores it in index without sorting the array 6 void display() : displays the name and ranks along with the name having the topmost rank.

i++) { System.println("-------tt-------"). rnk = new int[5].out.out. .println("*** Inputting The Names And Ranks ***").print("Enter name of student "+(i+1)+" : "). name[i]=br.out. You can skip it ! for(int i=0.print("Enter his rank : "). //this is just for styling the output. } } void display() { System.println(name[i]+"tt"+rnk[i]). for(int i=0.i<50.parseInt(br.readLine(). rnk[i]=Integer.println("NamettRank").{ name = new String[5]. } } } //end of superclass Record class Rank extends Record //subclass { int index.i<50. System.out. } void readvalues()throws IOException { System. System.out.i++) { System.out.readLine()).

} } //end of subclass Rank /* In your exams you don't need to write the below given code We are writing it so as to familiarize the students on how to run programs based on the concept of inheritance.out. //invoking the constructor of superclass index = 0.println("Student with topmost rank = "+name[index]).Rank() { super().i<50. } } } void display() { super.println("nTop most rank = "+rnk[index]).*/ public class Question11_ISC2011 //Class which will contain the main() method and execute the program { .out.i++) { if(rnk[i]<min) { index = i. //calling the superclass function display() System. } void highest() { int min = rnk[0].display(). for(int i=0. System.

readvalues().public static void main(String args[])throws IOException { Rank ob=new Rank(). //calling radvalues() function of superclass to input the names and ranks ob.display(). ob.out.println("*** Output ***"). //calling display() function of subclass } } Output: *** Inputting The Names And Ranks *** Enter name of student 1 : Aamir Enter his rank : 5 Enter name of student 2 : Zakir Enter his rank : 2 Enter name of student 3 : Saalim Enter his rank : 7 Enter name of student 4 : Samir Enter his rank : 3 Enter name of student 5 : Saif Enter his rank : 6 *** Output *** Name Rank —– —– Aamir 5 Zakir 2 Saalim 7 Samir 3 Saif 6 Top most rank = 2 Student with topmost rank = Zakir . //calling the function highest() for finding index of topmost rank System.highest(). //creating object of subclass ob.