20 Computer Programs Isc Java PDF
20 Computer Programs Isc Java PDF
Class XII
Note:
If you find this project useful and it saved your time, you can return the favour in two ways:
1. Since you’ve got the completed project and you have time to spare, try to write these
programs in your favourite IDE (preferably BlueJ) and see what output you’re getting.
Give some time to understand the solution. This will not only help you in your ISC exams
but will also come in handy if
i f the examiner asks you to explain a particular program.
Examiners choose random students for explanation and the less prepared you are, the
more chances you’ll be picked. So it’s better to go prepared.
2. Secondly, go through the tutorial section and read about different parts of programming
like loops, arrays, functions, classes etc. These topics are explained in detail. There are
solved questions on each topic some left unsolved for practice. If you have queries or
any program that you couldn’t solve, post it in the forum. We’ll explain you in detail how
it can be solved.
- Mentors
Note:
If you find this project useful and it saved your time, you can return the favour in two ways:
1. Since you’ve got the completed project and you have time to spare, try to write these
programs in your favourite IDE (preferably BlueJ) and see what output you’re getting.
Give some time to understand the solution. This will not only help you in your ISC exams
but will also come in handy if
i f the examiner asks you to explain a particular program.
Examiners choose random students for explanation and the less prepared you are, the
more chances you’ll be picked. So it’s better to go prepared.
2. Secondly, go through the tutorial section and read about different parts of programming
like loops, arrays, functions, classes etc. These topics are explained in detail. There are
solved questions on each topic some left unsolved for practice. If you have queries or
any program that you couldn’t solve, post it in the forum. We’ll explain you in detail how
it can be solved.
- Mentors
Index
Sno. Question Page no.
1. Write a program to input a natural number less than 1000 and display it in words. 4
2. Write a program to change the sentence of the odd rows with an encryption of
two characters ahead of the original character and even rows by storing the 8
sentence in reverse order.
3. Write a program to print the denominations in an amount entered by the user. 12
10. Write a program to insert a given element at the given position in an array. 34
12. Write a program to find the no of times a substring is present in the main string. 40
13. Write a program to print the number of vowels in each word of a sentence. 43
16. Write a program to arrange a sentence in ascending order of its word lengths. 53
19. Write a program to sort the border elements of a n x n matrix in ascending order. 63
20. Write a program to print the prime elements of a matrix along with their position. 67
Question 1
Write a program to input a natural number less than 1000 and display it in words.
Test your program on the sample data and some random data.
Example –
INPUT: 29
INPUT: 17001
INPUT: 119
INPUT: 500
Step-1: INPUT n
Step-3: Create three string arrays ones[], teens[] and tens[] and store ones, teens and
tens in words.
Step-4: IF n>=100 AND n<1000 THEN GOTO Step 5 ELSE GOTO Step 12
Step-8: IF n mod 10 = 0 AND n<>0 THEN GOTO Step 9 ELSE GOTO Step 11
Step-12: IF n>20 AND n<100 THEN GOTO Step 13 ELSE GOTO Step 16
Step-16: IF n>10 AND n<20 THEN GOTO Step 17 ELSE GOTO Step 19
Step-19: IF n<10 AND n<>0 THEN GOTO Step 20 ELSE GOTO Step 21
Step-20: END
Solution
import java.util.*;
class Question1{
public static void main(String args[])
throws InputMismatchException{
int n;
if(n>=1000)
{
System.out.println("OUT OF RANGE");
}else{
String result,h="",t="",o="";
int a,b,c;
String ones[]={"one", "two","three","four","five",
"six","seven","eight","nine"};
String teens[]={"eleven","twelve","thirteen","fourteen",
"fifteen","sixteen","seventeen","eighteen","nineteen"};
String tens[]={"ten","twenty","thirty","forty","fifty",
"sixty","seventy","eighty","ninety"};
else{
result=tens[a-1];
}
n=n%10;
}
if(n>20 && n<100){
a=n/10;
b=n%10;
if(result!=null){
result+=" and ";
result+=tens[a-1]+" "+ones[b-1];
}else{
result=tens[a-1]+" "+ones[b-1];
}
}
if(n>10 && n<20){
a=n%10;
if(result!=null){
result+=" and ";
result+=teens[a-1];
}else{
result=teens[a-1];
}
}
if(n<10 && n!=0){
if(result!=null)
{
result+=" and ";
result+=ones[n-1];
}else{
result=ones[n-1];
}
System.out.println("\n"+result.toUpperCase());
} //end of main
} //end of class
Question 2
Display an appropriate message if the size is not satisfying the given condition.
Define a string array of the inputted size and fill it with sentences row-wise.
Change the sentence of the odd rows with an encryption of two characters ahead of the
original character. Also change the sentence of the even rows by storing the sentence in
reverse order.
Display the encrypted sentences as per the sample data given below.
Test your program on the sample data and some random data.
INPUT: n=4
OUTPUT: KV KU ENQWFA. RAIN MAY IT. VJG YGCVJGT KU HKPG. COOL IS IT.
INPUT: n=13
Step-2: Start a loop and add a blank space at the end of each sentence so that the last word can
be extracted.
Step-3: If the sentence is on the odd row GOTO Step 4 else GOTO Step 8
Step-4: Run a loop and extract each character of the sentence one by one
Step-5: If the character is not a blank or a sentence terminator add 2 to its ASCII value
Step-7: Add the changed character to a temporary string and GOTO Step
Step-10: When a blank space is found extract the word from position+1 to p
Step-12: Keep on updating p to point to the last character of previous word until all the words
are extracted
Step-14: End
Solution
import java.io.*;
class Question2{
public static void main(String args[])
throws IOException{
BufferedReader br=new BufferedReader(
new InputStreamReader(System.in));
int nos;
System.out.print("Enter number of sentences : ");
nos=Integer.parseInt(br.readLine());
if(nos<1 || nos>=10)
System.out.println("\nInvalid Entry");
else{
int i,j,p,l;
String s[]=new String[nos];
for(i=0;i< nos;i++)
s[i]=(br.readLine()).toUpperCase();
for(i=0;i< nos;i++)
{
String t;
s[i]=" "+s[i];// add a blank space before each sentence
l=s[i].length();
if(i%2==0){
t="";
for(j=0;j< l;j++){
}
}
t=t+".";
s[i]=t;
}
}
System.out.println("\nOUTPUT:");
for(i=0;i< nos;i++)
System.out.print(s[i]);
}
} //end of main
} //end of class
Question 3
A bank intends to design a program to display the denomination of an input amount, upto 5
digits. The available denomination with the bank are of rupees 1000, 500, 100, 50, 20, 10 ,5,
2 and 1.
Design a program to accept the amount from the user and display the break-up in
descending order of denominations. (i,e preference should be given to the highest
denomination available) along with the total number of notes. [Note: only the
denomination used should be displayed]. Also print the amount in words according to the
digits.
Example 1:
INPUT: 14836
EXAMPLE 2:
INPUT: 235001
OUTPUT: INVALID AMOUNT
Algorithm
Step-2: Store the basic denominations (1000, 500, 100, 50, 20, 10, 5, 2, 1) in an array
Step-4: Divide the amount n by each value in the array to get the quotient
Step-5: If the quotient is not zero, display the denomination and update amount.
Step-6: To display the denomination digits in words, create an array and store the digits in
words
Step-7: Now run a while loop to reverse the original number.
Step-8: Run another loop and extract each digit of the reversed number.
Step-9: Print each digit in words using the array just created.
Step-10: End
Solution
import java.util.*;
class Question3{
public static void main(String args[])throws InputMismatchException{
Scanner scan=new Scanner(System.in);
int amt;
System.out.print("Enter a five-digit amount : ");
amt=scan.nextInt();
if(amt>99999)
{
System.out.println("INVALID AMOUNT.");
}else{
int a[]={1000,500,100,50,20,10,5,2,1};
int i,p,r,b,t;
p=amt;
for(i=0;i<a.length;i++){
t=amt/a[i];
if(t!=0){
System.out.println(a[i]+"X"+t+"="+(t*a[i]));
amt=amt%a[i];
}
}
String ones[]={"one","two","three","four","five","six","seven","eight","nine"};
r=0;
while(p>0){
r=r*10+p%10;
p/=10;
}
while(r>0){
b=r%10;
System.out.print(ones[b-1].toUpperCase()+" ");
r/=10;
}
}//end of if
} //end of main
} //end of class
Question 4
Given the two positive integers p and q, where p < q. Write a program to determine how
many kaprekar numbers are there in the range between 'p' and 'q'(both inclusive) and
output them. About 'kaprekar' number:
A positive whole number 'n' that has 'd' number of digits is squared and split into 2 pieces,
a right hand piece that has 'd' digits and a left hand piece that has remaining 'd' or 'd-1'
digits. If sum of the pieces is equal to the number then it's a kaprekar number.
SAMPLE DATA:
INPUT:
p=1
Q=1000
OUTPUT:
Step-3: For each number in the range, count the number of digits(d) in it
Step-10: If the sum is equal to the original number then it is a kaprekar number, print it and
count it.
Step-11: If the sum is not equal to the original number it is not a kaprekar number
Step-12: Continue the process till all the numbers in the range are checked
Step-14: End
Solution
import java.util.*;
class Question4{
public static void main(String args[])throws InputMismatchException{
int d,i,n,a,b,s,freq;
freq=0; // to find the frequency of kaprekar numbers
for(i=p;i<=q;i++)
{
n=i;
d=0; //to store the number of digits
//extract 'd' digits from the right of the square of the number
a=s%(int)Math.pow(10,d);
//extract 'd' or 'd-1' digits from the left of the square of the number
b=s/(int)Math.pow(10,d);
//Check if the two parts add up to the original number i.e. Condition for Kaprekar number
if(a+b==i){
System.out.print(i+" ");
freq++;
}
}
A smith number is a composite number, the sum of whose digits is the sum of the digits of
its prime factors obtained as a result of prime factorization (excluding 1).
The first few such numbers are 4, 22, 27, 58, 85, 94, 121.....
Example 1.
Write a program to input a number and check whether it is a smith number or not.
Sample data:
Input: 94 Output: SMITH Number
Input: 102 Output: NOT SMITH Number
Algorithm
Step-1: Input n
Step-5: If a factor is found, run a while loop to store its sum of digits
Step-7: Decrement the loop counter so that the same factor i s checked again
Step-8: Outside the loop compare if the two sums are equal or not
Step-9: If they are equal display “Smith number” else display “Not Smith number”
Step-10: End
Solution
import java.util.*;
class Question5{
int n=sc.nextInt();
int p,q,i,sod=0,sopf=0,t;
p=q=n;
while(p>0){
sod+=p%10;
p/=10;
}
for(i=2;i<=q;i++){
t=i;
i--; //decrement the factor so that next time the same factor is checked again and
again until it is not a factor. This is the prime factorization method.
}
if(sod==sopf) // if sum of digits and sum of prime factors are equal, it is smith number
System.out.println("Smith number");
else
} //end of main
} //end of class
Question 6
A unique-digit integer is a positive integer (without leading zeros) with no duplicate digits.
For example 7, 135, 214 are all unique-digit integers whereas 33, 3121, 300 are not.
Given two positive integers m and n, where m< n, write a program to determine how many
unique-digit integers are there in the range between m and n (both inclusive) and output
them.
The input contains two positive integers m and n. Assume m< 30000 and n< 30000.
You are to output the number of unique-digit integers in the specified range along with
their values in the format specified below:
SAMPLE DATA:
Step-7: If the array values of the two loops match, set the flag to false and break the loops
Step-9: Outside the loop display the count as frequency of unique- digit numbers
Step-10:End
Solution
import java.util.*;
class Question6{
int m=sc.nextInt();
int n=sc.nextInt();
int i,j,k,p,x,freq=0;
boolean flag;
for(i=m;i<=n;i++){
p=i;
x=0;
while(p>0){
a[x++]=p%10;
p/=10;
flag=true;
for(j=0;j<x;j++){
for(k=j+1;k<x;k++){
if(a[j]==a[k]){
flag=false;
j=x;
break;
if(flag){
System.out.print(i+" ");
freq++;
} //end of main
} //end of class
Question 7
Write a program to create an array of n integers and display the frequency of each element of
the array.
Example:
Input:
Enter the number of terms: 10
Enter 10 integers: 1 2 2 2 3 4 3 4 5 6
Output:
Frequency of 1: 1
Frequency of 2: 3
Frequency of 3: 2
Frequency of 4: 2
Frequency of 5: 1
Frequency of 6: 1
Algorithm
Step-5: If the elements a[i] and a[j] are equal and a[j] is not zero, count and put zero in a[j]
Step-6: Outside the inner loop, print frequency of a[i] if it is not zero
Step-7: End
Solution
import java.util.*;
class Question7{
public static void main(String args[])throws InputMismatchException{
int j,f;
for(i=0;i<n;i++)
{
f=1;
for(j=i+1;j<n;j++)
{
if(a[i]==a[j] && a[j]!=0)
{
f++;
a[j]=0;
}
}
if(a[i]!=0)
System.out.println("Frequency of "+a[i]+" : "+f);
}
} //end of main
} //end of class
Question 8
Write a program to create an array of n integers and sort the array in ascending order using
Insertion sort technique.
Example –
INPUT:
Enter the size of the array: 5
Enter 5 elements: 5 9 7 3 -4
OUTPUT:
-4 3 5 7 9
Algorithm
Step-3: Now to apply insertion sort on this array, run a loop ‘i’ from 1 to n
Step-5: Run a reverse loop ‘j’ from i-1 till j>=0 and k<a[j]
Step-10:End
Solution
import java.util.*;
class Question8 {
for(i=0;i<n;i++){
a[i]=sc.nextInt();
}
for(i=1;i<n;i++){
k=a[i];
for(j=i-1;j>=0 && k<a[j];j--){ //Shift the elements to the right until the condition is false
a[j+1]=a[j];
for(i=0;i<n;i++){
System.out.print(a[i]+" ");
}
}
}
Question 9
Example:
n = 109
109 = 9+0+1
=10 (Still a number, find the sum again)
10 =0+1
=1
The result is 1. Hence, 109 is a magic number.
n=18
18 =8+1
=9
The result is 9, which is not equal to 1, hence 18 is not a magic number.
Input: n=109
Output: 109 is a magic number
Input: n=18
Output: 18 is not a magic number.
Algorithm
Step-6: If it is greater than 9 it means it’s a number, copy it in p and reset the sum
Step-8: If the sum is equal to 1, display “Magic number” else “Not a magic number”
Step-9:End
Solution
import java.util.*;
int n=sc.nextInt();
int s=0,p,a;
p=n;
do{
while(p>0){
a=p%10;
s=s+a;
p=p/10;
if(s>9){
p=s;
s=0;
}while(p>0);
if(s==1)
else
} //end of main
} //end of class
Question 10
Write a program to create an array of n elements and insert a given element at the given
position in the array. Your program should display appropriate error message if the position is
invalid.
Example –
INPUT:
Size of the array: 5
Input 5 integers: 1 2 4 5 6
Enter the element to be inserted: 3
Enter the position at which the element should be inserted: 3
OUTPUT:
123456
INPUT:
Size of the array: 7
Input 5 integers: 4 3 5 8 6 2 7
Enter the element to be inserted: 13
Enter the position at which the element should be inserted: 10
OUTPUT:
Invalid position entered.
Algorithm
Step-7: If match not found keep checking with the rest of the array
Step-8: If match found run a loop from n to p and shift all the elements one place to the right,
store the element e in the desired position, raise a flag and break out of loop
Step-11: End
Solution
import java.util.*;
boolean flag=false;
for(i=0;i<n;i++){
if(i+1==p){
for(j=n;j>i;j--){
a[j]=a[j-1];
}
a[i]=e;
flag=true;
n++;
break;
}
}
if(flag){
for(i=0;i<n;i++)
System.out.print(a[i]+" ");
}else{
System.out.print("Position Invalid");
}
} //end of main
} //end of class
Question 11
Write a program to create an array of n elements and delete a given element from the array.
Your program should display appropriate error message if the element to be del eted is not
found in the array.
Example –
Input:
Size of the array: 5
Input 5 integers: 1 2 4 5 6
Enter the element to be deleted: 4
Output:
1256
Input:
Size of the array: 7
Input 5 integers: 4 3 5 8 6 2 7
Enter the element to be deleted: 1
Output:
Element not found in the array.
Algorithm
Step-10: decrease the size of loop, raise a flag and break out of loop
Step-11: Keep checking the rest of the array until the end
Step-12: If the flag is raised display the modified array else display error message
Step-13: End
Solution
import java.util.*;
boolean flag=false;
for(i=0;i<n;i++){
if(a[i]==e){
for(j=i;j<n-1;j++){
a[j]=a[j+1];
}
flag=true;
n--;
break;
}
}
if(flag){
for(i=0;i<n;i++)
System.out.print(a[i]+" ");
}else{
System.out.print("Element not found");
}
} //end of main
} //end of class
Question 12
Write a program to enter a main string and a substring and find the number of times the
substring is present in the main string ignoring case considerations.
Assume that substring is present only as a single word. It is neither found inside the string nor
as a group of words.
Example –
Input:
Enter a main string: The man goes to the theatre.
Enter a substring: the
Output:
No of times substring is present in the main string: 2
Algorithm
Step-9: Keep checking the rest of the string until the end
Step-11: End
Solution
import java.io.*;
int l=ms.length();
int i,p=0,freq=0;
for(i=0;i<l;i++){
char ch=ms.charAt(i);
if(ch==' '){
String word=ms.substring(p,i);
if(word.equalsIgnoreCase(ss)){
freq++;
}
p=i+1;
}
}
System.out.println("No of times substring is present in the main string: "+freq);
} //end of main
} //end of class
Question 13
Write a program to enter a string and print each word along with the number of vowels in it.
Example –
INPUT:
Enter a string: These are wonderful times
OUTPUT:
Word Vowel
These 2
are 2
wonderful 3
times 2
Algorithm
Step-10: Once the inner loop terminates display the word and the counter
Step-11: Keep checking the rest of the string until the end
Step-12: End
Solution
import java.io.*;
class Question13
{
public static void main(String fh[])throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a sentence : ");
String str=br.readLine();
str+=" ";
int l=str.length();
String t=" ";
int i,j,c=0,p=0;
for(i=0;i<l;i++)
{
char ch=str.charAt(i);
if(ch==' ')
{
t=str.substring(p,i+1);
c=0;
for(j=0;j<t.length();j++)
{
char k=t.charAt(j);
if(k=='a'||k=='e'|| k=='i' || k=='o' ||k=='u' || k=='A' || k=='E' || k=='I' || k=='O' ||
k=='U')
c++;
}
System.out.println(t+"\t"+c);
p=i+1;
}
} //end of main
} //end of class
Question 14
Write a program to enter a string and remove consecutively repeating characters from a string.
Example –
Input:
Output:
Step-10: Keep checking the rest of the string until the end
Step-12: Run a loop and add all the elements of the array to the string
Step-14: End
Solution
import java.io.*;
public class Question14{
public static void main(String fh[])throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a string : ");
String str = br.readLine();
char a[]=str.toCharArray();
int i,j,l=a.length;
for(i=0;i<l-1;i++){
if(a[i]==a[i+1]){
for(j=i;j<l-1;j++){
a[j]=a[j+1];
}
l--;
i--;
}
}
str="";
for(i=0;i<l;i++){
str+=a[i];
}
System.out.println("The modified string is: "+str);
} //end of main
} //end of class
Question 15
Write a program to enter a string and print the frequency of each word in a string.
Example –
Input:
Enter a string: The need for enlightenment is the need of the hour.
Output:
Word Frequency
The 3
need 2
for 1
enlightenment 1
is 1
of 1
hour 1
Algorithm
Step-5: Extract word from the string and store it in the string array
Step-6: Continue the process until all the words are stored in the array
Step-10: If equal, increment f by 1 and shift all the elements to the left
Step-12: Repeat the process for all the elements of the array
Step-13: End
Solution
import java.io.*;
class Question15
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a String");
String s=br.readLine();
s=s+" ";
int i,t=0,p=0,f=0,j,k;
int l=s.length();
String b;
for(i=0;i<l;i++)
if(s.charAt(i)==' ')
b= s.substring(p,i);
a[t]=b;
t++;
p=i+1;
}
for(i=0;i<t;i++)
f=1;
for(j=i+1;j<t;j++)
if(a[i].equals(a[j]))
f++;
for(k=j;k<t-1;k++)
a[k]=a[k+1];
t--;
j--;
} //end of main
} //end of class
Question 16
Write a program to enter a sentence and print it in ascending order of its word l engths.
A sentence may either terminate with a period (.), exclamation mark (!) or a question mark
(?).
Example –
INPUT:
OUTPUT:
Step-5: Extract word from the string and store it in the string array
Step-6: Continue the process until all the words are stored in the array
Step-9: If length of the ith element is greater than the length of the jth element swap them.
Step-11: Display the words in the array along with a blank space
Step-12: End
Solution
import java.io.*;
class Question16
String s = br.readLine();
s=s.toLowerCase();
int l= s.length();
int t=0,i,p,j;
String g;
char k;
p=0;
for(i=0;i<l;i++)
k=s.charAt(i);
if(k==' ' || k== '.' || k==’!’ || k==’?’)// Check for delimiters to extract words
String b = s.substring(p,i);
t++;
p=i+1;
}
//Using bubble sort to arrange the string array i n ascending order of word length
for(i=0;i<t;i++)
for(j=0;j<t-i-1;j++)
if(a[j].length()>a[j+1].length()) //if the word on the left is smaller in length, swap it.
g=a[j];
a[j]=a[j+1];
a[j+1]=g;
for(i=0;i<t;i++)
} //end of main
} //end of class
Question 17
Write a program to enter a sentence and sort it in alphabetical order. You can convert the
Example –
INPUT:
OUTPUT:
Step-7: Continue the process until all the words are stored in the array
Step-11: Continue the process till the entire array is sorted in ascending order
Step-13: End
Solution
import java.io.*;
str=str.toLowerCase();
int l=str.length();
String a[]=new String[l];
int i,j,p=0,x=0;
for(i=0;i<l;i++){
char ch=str.charAt(i);
if(ch==' ' || ch=='.'){ //Extract the
the word when a space or period is found
String temp=str.substring(p,i);
a[x++]=temp; //store the word in a string array
p=i+1;
}
}
} //end of main
} //end of class
Question 18
Example –
Example –
INPUT:
Enter 9 elements:
1 2 3
4 5 6
7 8 9
OUTPUT:
Step-6: If row is first or last or if column if first or last add the element
Step-9: End
Solution
import java.util.*;
public class Question18 {
public static void main(String args[])throws InputMismatchException{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of rows for a sqaure matrix: ");
int n=sc.nextInt();
} //end of main
} //end of class
Question 19
Example –
INPUT:
Enter 9 elements:
4 9 3
1 7 6
5 8 2
OUTPUT:
1 2 3
9 5 4
8 7 6
Algorithm
Step-6: Continue the process till all the border eleme nts are stored in b[]
Step-7: Now, sort the array b[] in ascending order using any sorting technique
Step-8: To store the sorted border elements in the matrix, run a loop for the
Step-10: Run a loop for last row and then first column to store the elements from b[] to matrix
Step-11: Finally, print the matrix that now has border elements sorted in ascending order.
Step-12: End
Solution
import java.util.*;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
System.out.print(a[i][j]+" ") ;
}
System.out.println();
}
} //end of main
} //end of class
Question 20
Write a program to create a m x n matrix and print the prime elements in it along with the row
Example –
INPUT:
Enter 12 elements:
4 5 1 6
8 25 30 2
16 9 45 3
OUTPUT:
5 0 1
2 1 3
3 2 3
Algorithm
Step-6: Now run a loop to find the number of factors of the matrix element
Step-7: If the number of factors is 2 print the element, its row index and column index
Step-9: End