Special Java Programs(Icse-2025).
Special Java Programs(Icse-2025).
(ICSE-2025)
Special note: Practice following programs properly
(*****In Exam. Write variable descriptions for each program)
Programs based on UDF
1
if(w<=10)
charge=25*w;
else if(w<=30)
charge=25*10+(w-10)*20;
else
{
charge=25*10+20*20+(w-30)*10;
charge=charge+charge*.05;
}}
void disp()
{
System.out.println("Name\tWeight\tBill");
System.out.println(name+"\t"+w+"\t"+charge);
}
public void main()
{
Courier obj=new Courier();
obj.accept();
obj.calculate();
obj.disp();
}
} // end class
********
// PRO 2 (A program to explain UDF with discount format)
Define a class named BookFair with the following description :
Instance variables /Data members :
String Bname - stores the name of the book
double price - stores the price of the book
Member methods :
BookFair() - Default constructor to initialize data members
void Input() - To input and store the name and the price of the book.
void calculate() - To calculate the price after discount. Discount is calculated based on the following criteria
Price Discount
Less than or equal to ₹1000 2% of price
More than ₹ 1000 and less than or equal to ₹ 3000 10% of price
More than ₹ 3000 15% of price
void display() - To display the name and price of the book after discount.
Write a main method to create an object of the class and call the above member methods.
Ans)
import java.util.*;
class BookFair
{
Scanner sc=new Scanner (System.in);
String Bname;
double price;
BookFair()
{
Bname="";
price =0.0;
}
void input()
{
2
System.out.println("Enter Name and Price " );
Bname=sc.next();
price=sc.nextDouble();
}
void calculate()
{
if(price<=1000) Variable Description:
price= price-price*.02;
else if(price<=3000) Name Type purpose
price =price-price*.10;
Bname String To store name
else
price double To store price
price =price-price*.15;
}
void display()
{
System.out.println("Name :" +Bname);
System.out.println("Price :" +price);
}
public static void main()
{
Bookfair obj=new BookFair();
obj.input();
obj.calculate();
obj.display();
}
}
class Student
{
String Name;
int m1,m2,m3,age;
double maxi,avg;
Student(String nm, int ag,int m11,int m21, int m31) // parameterized constructor
{
Name=nm;
m1=m11;m2=21;m3=m31;age=ag;
3
maxi=0;avg=0.0;
}
void compute()
{ Variable Description:
maxi=Math.max(m1,Math.max(m2,m3)); Name Type purpose
avg=(m1+m2+m3)/3;
name String To store name
}
m1,m2,m3 int To store marks
age int To store age
void show() maxi, avg double To store maximum, average
{
System.out.println("Name\tAge\tMark1\tMark2\tMark3\tMax\tAvg");
System.out.println(Name+"\t"+age+"\t"+m1+"\t"+m2+"\t"+m3+"\t"+maxi+"\t"+avg);
}
void main(String nm, int ag,int m11,int m21, int m31)//receiving parameters for instance variables
{
Student obj=new Student(nm, ag , m11, m21, m31);// passing arguments to parameterized constructor
obj.compute();
obj.show();
}}
Programs based on SDA(Array)
// PRO 1 Program to explain binary search
Write a Java program to accept N positive integers in an array in ascending order then perform binary
search to the check index no. of an input number. If it is found then display its position (index no),
otherwise display a message “Not found”.
ANS)
class Binary
{
public static void main(int ar[],int num)
{
int mid, beg, end,f=-1;
beg=0;
end=ar.length-1; // to store last index no
Variable Descriptions:
while(beg<=end)
Name Type Purpose
{
mid=(beg+end)/2; // for middle index no ar[] int To store numbers in ascending order
num int To store number to be searched
if(ar[mid]==num) mid int To store index no of middle element
{ beg int To store index no of first element
f=mid; end int To store index no of last element
f int flag variable to store index no of
break; searched element (if found)
}
else if(num>ar[mid]) // for descending list use < sign
beg=mid+1;
else
4
end=mid-1;
}
System.out.println(f!=-1? "The index no of element is-"+f : "Not found");
}
}
Ans)
class Bubble
{
public void main(double pr[])
{
int i, j;
double temp;// temp is for price swapping
for(i=0;i<=48;i++) //set upto 2nd last index no
{
for(j=0;j<=48-i;j++)
{
if (pr[j]<pr[j+1])
Variable Description
{
Name type purpose
temp=pr[j]; pr[] double An array to store prices
pr[j]=pr[j+1]; i ,j int for loop counters
pr[j+1]=temp; temp double Used for price swapping
}
}
}
for(i=0;i<=9;i++) // to display top ten elements
System.out.println(pr[i]);
}}
**********
Ans)
class Selection
{
public void main(double pr[])
{
int i, j,m;
double temp;// temp is for price swapping
for( i=0;i<=49;i++) // for original array
System.out.println(pr[i]);
5
for(i=0;i<=48;i++) //set upto 2nd last index no
{
m=i;
for(j=i+1;j<=49;j++)
{
if (pr[m]>pr[j]) Variable Description
m=j; Name type purpose
} pr[] double An array to store prices
temp=pr[i]; i ,j,m int for loop counters
temp double Used for price swapping
pr[i]=pr[m];
pr[m]=temp;
}
for(i=0;i<=49;i++) // to display arranged elements
System.out.println(pr[i]);
}}
**********
// PRO 4 Program to show the use of Bubble sorting on dual lists
Write a java class to accept N number of different names & their marks in the respectively cells of two
single dimensional arrays. Arrange the lists in ascending order on the basis of names.(using Bubble Sort
method ). Display the sorted lists.
Ans)
class Binary_dual
{
public void main(String nm[],int mk[])
{
int i, j ,temp; // temp is for marks swapping
String temp1; // temp1 for name swapping
for(i=0;i<=mk.length-2;i++) // set upto 2nd last index number
{
for(j=0;j<=mk.length-2-i;j++)
{
if (nm[j].compareTo(nm[j+1])>0) // for marks use (mk[j]>mk[j+1]) , for descending use < sign
{
temp1=nm[j];
nm[j]=nm[j+1]; Variable Description
Name type purpose
nm[j+1]=temp1;
nm[] String An array to store names
temp=mk[j]; mk[] double An array to store marks
mk[j]=mk[j+1]; i ,j int for loop counters
mk[j+1]=temp; temp double Used for mark swapping
} temp1 String Used for name swapping
}
}
for(i=0;i<=mk.length-1;i++) // to display lists
System.out.println(nm[i]+"\t"+mk[i]);
}}
//PRO 5 Program to explain linear search
6
Define a class to accept N numbers in an array then check and display the smallest number and the greatest
number present among them.
ANS)
class Linear
{
public void main(int ar[])
{
int gt,st,i;
gt=ar[0];st=ar[0] ;
for(i=0;i<ar.length;i++)
{
Variable Descriptions
if(ar[i]>gt) // check for greatest number Name Type Purpose
gt=ar[i]; ar[] int An array to store numbers
gt int to store the greatest number
else if(ar[i]<st) // check for smallest number
st int to store the smallest number
st=ar[i]; i int to generate index number
}
System.out.println("The Smallest No="+ st);
System.out.println("The Greatest No="+ gt);
}
}
*********
// PRO 6 Program to show the use of linear search for fixed length array
Define a class to accept 10 numbers in an array then count and display even and odd numbers present
among them separately. (without using Scanner class)
ANS)
class Count
{
public void main(int ar[ ])
{ Variable Descriptions
int id,c1=0,c2=0; Name Type Purpose
for(id=0;id<10;id++) ar[] int an array to store numbers
{ c1 int counter variable
c2 int counter variable
if(ar[id]%2==0)
id int to generate index number
c1++;
else
c2++;
}
System.out.println("Odd numbers="+c2);
System.out.println("Even numbers="+c1);
}
}
7
OR
// PRO 7: A program show the use of Scanner class for a fixed length array
Define a java class to accept 10 numbers in an array then count and display even and odd numbers present
among them separately.(by using Scanner class)
ANS)
import java.util.*;
class Count
{
Scanner sc=new Scanner(System.in);
public void main( )
{
int ar[ ]=new int[10];
int id,c1=0,c2=0;
System.out.println(“Enter 10 numbers=”);
for(id=0;id<10;id++) Variable Descriptions
{ Name Type Purpose
ar[id]=sc.nextInt(); ar[] int an array to store numbers
if(ar[id]%2==0) c1 int counter variable
c1++; c2 int counter variable
else id int to generate index number
c2++;
}
System.out.println("Odd numbers="+c2);
System.out.println("Even numbers="+c1);
}
}
**********
// PRO 8: A program to use double values in an array
Define a java class to accept marks of N number of subjects in an array (decimal part is allowed), then
calculate and display total and average of the same.
ANS)
class result
{ Variable Descriptions
public void main(double mk[]) Name Type Purpose
mk[] double an array to store marks
{ t double to store total
int i; avg double to store average
i int to generate index number
double t=0,avg;
for(i=0;i<mk.length;i++)
t=t+mk[i];
avg=t/mk.length;
System.out.println("The total="+t);
System.out.println("The average="+avg);
}}
8
// PRO 9: A program to use elements of two arrays simultaneously.
Write a Java program to accept name and average marks of N number of students in respective cells of two
single subscripted arrays nm[ ] and avg[ ], then check and display name and marks those students who have
scored 90% or above in tabular form with suitable headings.
ANS)
class Students
{
public void main(String nm[],double avg[])
{ Variable Descriptions
int id; Name Type Purpose
nm[] String an array to store names
System.out.println("Name\tAverage");// to display headings avg[] double an array to marks
for(id=0;id<nm.length;id++) id int to generate index number
{
if(avg[id]>=90)
System.out.println(nm[id]+"\t"+avg[id]);
}
}}
// PRO 10: A linear search program.
Define a Java class to accept 10 integers in an array called M[], then check and display multiples of an
input number present among them. Also count and display occurrences of the same. If not available, then
display “Not found” e.g. M[]={30,4,5,15,7,1,9,8,12,4} Input number =3
ANS: 30 15 9 12 Frequency = 4
ANS)
class Count
{
public void main(int M[], int num)
{
int i,c=0; Variable Descriptions
for(i=0;i<10;i++) Name Type Purpose
M[] int an array to store 10 numbers
{ num int to store number to find its multiples
if(M[i]%num==0) // to check multiple i int to generate index number
c int counter variable
{
System.out.print(M[i]+ " ")
c++;
}
System.out.println(c==0 ? "Not found":"Occurrences ="+c);
}} ******
9
// PRO 11:
Define a java class called List to accept N integers, then check and display the numbers in which the last
digit is same to the digit input by user.
ANS)
class List
{ Variable Descriptions
public void main(int num[], int d) Name Type Purpose
num[] int an array to store numbers
{ d int to store digit to be checked
int i; i int to generate index number
for(i=0;i<num.length;i++)
{
if(num[i]%10==d)
System.out.print(num[i]+ " ");
}
}
}
*******
// PRO 12:
Define a java class called Numarr to accept 10 real numbers, then count and display how many numbers are
in the range of 10 to 100 present among them. If not found the display “Not found” Use Scanner class to
accept numbers.
ANS)
import java.util.*;
class Numarr
{
Scanner sc=new Scanner(System.in);
public void main()
{
double num[]=new double[10];
int i,c=0; Variable Descriptions
System.out.println("Enter 10 numbers="); Name Type Purpose
num[] double an array to store numbers
for(i=0;i<10;i++) i int to generate index number
{ c int counter
num[i]=sc.nextDouble();
if(num[i]>=10 && num[i]<=100)
c++;
}
System.out.print(c==0?"Not found":"Numbers found="+c);
}
}
10
// PRO 13 : A program to explain how to initialize an array
Write a Java program to initialize following numbers in a single dimensional array then display positive
numbers followed by negative numbers present among them without changing their order.
INPUT : -5 , 6 , -2 , 9 , -4 , -2 , 7, -1 , 10, -45, 50, 15
OUTPUT : 6 9 7 10 50 15 -5 -2 -4 -2 -1 -45
Ans)
class Array_pro
{
public static void main( )
Variable Descriptions
{ Name type purpose
int i; A[] int To initialize given numbers
int A[]={-5,6,-2,9,-4,-2,7,-1,10,-45,50,15}; i int To get index number
for(i=0;i<A.length;i++) // for +ve numbers
{
if(A[i]>=0)
System.out.print(A[i]+" ");
}
for(i=0;i<A.length;i++) // for -ve numbers
{
if(A[i]<0)
System.out.print(A[i]+" ");
}
}}
// PRO 14: A program to merge two arrays
Write a program in Java to accept elements of two integer arrays from the user and join them into a third
array. Also find the sum and average of the numbers of final array.
Sample Input: Array A [ ] = {5,6,45,17,2,58,33}; Array B [ ] = {13,25,1,0,7,9};
Sample Output: The final Array after joining is : 5 6 45 17 2 58 33 13 25 1 0 7 9
Ans)
class array
{
public void main(int A[], int B[])
{
int C[]=new int[A.length+B.length]; // 3rd array is declared to store all the elements.
int i,j=0; // j is the index no. for new array
double s=0,avg;
for(i=0;i<A.length;i++,j++) Variable Descriptions
Name type purpose
{ st
A[] int To store numbers of 1 array
C[j]=A[i]; B[] int
nd
To store numbers of 2 array
rd
s=s+C[j]; C[] int To store numbers of 3 array
} i,j int To get index number
avg double To store average
for(i=0;i<B.length;i++,j++)
s double To store sum
{
C[j]=B[i];
s=s+C[j];
11
}
avg=s/C.length;
System.out.println("The sum="+s);
System.out.println("The Avg="+avg);
}
}
********
Programs based on String Class, Character class & Array
// PRO 1:
Define a java class to accept 15 characters in a single dimensional array called ch[ ],then perform the following:
• Count the number of lowercase letters in the array and print.
• Count the number of uppercase vowels in the array and print.
ANS)
class Arraypro
Variable descriptions:
{
Name Type Purpose
public static void main(char ch[])
ch [] char array to store characters
{
int id,c1=0,c2=0; id int to get index number
// PRO 4:
Write a program in Java to accept 10 names in a S.D.A. and display all those names whose first alphabet
matches with the alphabet given by the user. Also count how many such names are present.
ANS)
class Strpro
{
public void main(String nm[],char lt)
{
Variable descriptions:
int i,c=0; Name Type Purpose
for(i=0;i<10 ;i++) nm[] String array to store 10 names
{ i int to store index number
st
char ch=nm[i].charAt(0); ch char to store 1 letter of the name
lt char to store letter to be checked
if (ch==lt)
c int counter
{
System.out.print(nm[i]+ " ");
13
c++;
}
} // end of for loop
System.out.println("\nNames are found="+c);
}
}
// PRO 5:
Define a Java class called STRPRO to accept N words in an array then check and display the length of longest
and shortest words present in the same.
ANS)
class STRPRO
{
public void main(String wr[])
{
int i,=0,ln,st;
ln=wr[0].length();// length of 1st word
st=wr[0].length(); // length of 1st word
for(i=1;i<wr.length ;i++) // to get remaining words
{ Variable descriptions:
if (wr[i].length()>ln) Name Type Purpose
ln=wr[i].length(); wr[] String array to store words
i int to store index number
else if(wr[i].length()<st)
ln int to store length of longest word
st=wr[i].length(); st int to store length of shortest word
}
System.out.println("Longest length="+ln);
System.out.println("Shortest length="+st);
}
}
// PRO 6:
Define a Java class called WORDS to accept N words in an array then do the following tasks:
a) Convert each word to capital.
b) Check and display only the words having length not less than 7 and at the end “TION” is present.
ANS)
class WORDS {
public void main(String wr[])
{ Variable descriptions:
int i; Name Type Purpose
for(i=0;i<wr.length ;i++) wr[] String array to store words
i int to store index number
{
wr[i]=wr[i].toUpperCase();
if (wr[i].length()>=7 && wr[i].endsWith("TION"))
System.out.println(wr[i]);
} }}
14
Programs based on String Class & Character class
// PRO 7:
Define a class to accept two strings, convert them into uppercase, if the strings are not of equal length,
then display the message “Strings are not of equal length”. Otherwise merge both the strings to make
a new string as shown below. Finally display the new string.
Sample Input : s1=“SUPER” s2=“WORLD” Output : SWUOPRELRD
ANS)
class Stringpro
{
public static void main(String s1, String s2)
{
String s=""; // to store the new string
s1=s1.toUpperCase(); Variable descriptions:
s2=s2.toUpperCase(); Name Type Purpose
st
s1 String to store 1 word
if(s1.length()==s2.length()) s2 String
nd
to store 2 word
{ s String to store new string
i int for loop counter
for(int i=0;i<s1.length();i++)
s=s+s1.charAt(i)+s2.charAt(i);
System.out.println(s);
}
else
System.out.println("Both the strings are not of equal length");
}
}
//PRO 8: A String program to reverse word
Write a java program to input a word then make a new word by reversing its letters. Display both the words.
Also check the original & reverse are same or not.
INPUT : SMART OUTPUT: TRAMS
ANS)
class Strpro
{
void disp(String s)
Variable Descriptions:
{ Name type purpose
int i;String s1=""; s String To store original string
for(i=0;i<s.length();i++) s1 String To store word in reverse order
{ i int To get index number
ch char To get characters of string
char ch=s.charAt(i);
s1=ch+s1; // to reverse the word
} // END OF FOR
System.out.println(s+"\n"+s1);
System.out.println(s.equals(s1)?”words are same” : “words are not same”);
}}
15
// PRO 9:
Write a program to input a sentence. Produce the sentence as one word by removing spaces and characters
other than alphabets (if any). Display the original & newly formed sentence as one word.
eg. Thi@si#s a9 ni%c%e c8a*r^.
output:
Original : Thi@si#s a9 ni%c%e c8a*r^.
New : Thisisanicecar
Ans:
class strpro
{
public static void main(String s)
{
String s1=""; // to store final string
int i; Variable Descriptions
for(i=0;i<s.length();i++) Name type purpose
{ s String To accept string
s1 String To store final word
char ch=s.charAt(i); ch char To get characters of string
if(Character.isLetter(ch)) i int To get index number
s1=s1+ch;
}
System.out.println(s+"\n"+s1);
}
}
**********
// PRO 11:
Define a java class to accept a string then display the same in toggle case form.(toggle case means convert
uppercase letters to lowercase and vice-versa.)
e.g. input :HeLlo DeAr
output : hElLO dEaR
Ans)
class toggle_case
{
public void main(String s)
{
Variable Descriptions
String s1=""; Name Type Purpose
int i; s String To accept string
for(i=0;i<s.length();i++) s1 String To store final string
ch char To get characters of string one by one
{ i int To get index number
char ch=s.charAt(i);
if(Character.isUpperCase(ch))
s1=s1+Character.toLowerCase(ch);
else if(Character.isLowerCase(ch))
s1=s1+Character.toUpperCase(ch);
else
s1=s1+ch;
}
System.out.println(s+"\n"+s1);
}
}
// PRO 12:
Define a java class to accept a string with multiple words then convert the same to uppercase display the
same as shown below.
e.g. INPUT : This is a cat. OUTPUT : T.I.A.C.
Ans)
class show
{
public void main(String s)
{
String s1="";// to store result
s=" "+s; // a blank space is added at the beginning of the string
s=s.toUpperCase();
int x;
17
for(x=0;x<s.length();x++)
{ Variable Descriptions
char ch=s.charAt(x); Name type purpose
if(ch==' ') s String To accept string
ch char To store characters of string
s1=s1+s.charAt(x+1)+".";
x int To get index number
} s1 String To store final string
System.out.println(s1);
}
}
// PRO 13:
Define a java class to accept a word, convert it to capital then display the same by keeping vowels first then
Consonants.
e.g. INPUT : periodical OUTPUT: EIOIAPRDCL
ANS)
class Strpro
{
public void main(String s)
Variable Descriptions
{
Name type purpose
s=s.toUpperCase(); s String To accept string
String s1="",s2=""; s1 String To store vowel
int i; s2 String To store consonant
i int To get index number
for(i=0;i<s.length();i++) ch char To store character
{
char ch=s.charAt(i);
if("AEIUO".indexOf(ch)!=-1) // vowels check
s1=s1+ch;
else
s2=s2+ch;
}
System.out.println(s1+s2);
}}
//PRO 14:
Write a program to input a string and convert it into uppercase and print the pair of vowels and number of pair
of vowels occurring in the string.
Example:
Input: "BEAUTIFUL BEAUTIES "
: Pair of vowels: EA AU EA AU IE No. of pair of vowels: 5
Ans)
Variable Descriptions
class Strpro Name type purpose
{ s String To accept string
public void main(String s) ch,ch1 char To store characters of string
i int To get index number
{
c int To store frequency
int i,c=0;
s=s.toUpperCase();
18
char ch,ch1;
for(i=0;i<s.length()-1;i++) // set index up to 2nd last character
{
ch=s.charAt(i); // to get 1st character of the pair
ch1=s.charAt(i+1);// to get 2nd character of the pair
if(("AEIUO".indexOf(ch)!=-1) &&("AEIUO".indexOf(ch1)!=-1))
{
System.out.print(ch+""+ch1+" ");
c++;
}
}
System.out.println("\nNo of occurrences="+c);
}}
//PRO 15:
Write a Java program to accept a word then display its letters one below another with their respective Unicode.
(Assume the string is accepted in uppercase)
e.g. INPUT : LUCK
OUT:
L=76
U=85
C=67
K=75 Variable Descriptions
Ans) Name type purpose
class UNICODE s String To accept string
ch char To store characters of string
{ i int To get index number
public void main(String s)
{
s=s.toUpperCase();
int i;
for(i=0;i<s.length();i++)
{
char ch=s.charAt(i);
System.out.println(ch+"="+(int)ch);
}
}
}
19
//PRO 16:
Define a java class to accept a string, convert the same to uppercase and display only those characters, which
are consecutives.
e.g. Input : UNDERSTANDING OPERATORS.
Output :
D,E
R,S
S,T
O,P
R,S
ANS)
class letter_dis
{
public void main(String s)
{
s=s.toUpperCase(); Variable Descriptions
System.out.println(s1); Name type purpose
s String To accept string
int i; char ch,ch1; ch,ch1 char To store characters of string
for(i=0;i<s.length( )-1;i++)// up to 2nd last index i int To get index number
{
ch=s.charAt(i);
ch1=s.charAt(i+1);
if(ch1-ch==1)
System.out.println(ch+","+ch1);
}
}
}
//PRO 17:
Write a program to input a string then form a new word by taking first letter of each word then check the
newly formed word is palindrome or not.(ignore the case)
e.g.:
Input : “Mangoes are delivered after March"
Output : MadaM [It is palindrome]
ANS)
class word
{
public void main(String s)
{
int i; s=" "+s; // a blank space is added at the beginning
20
String wr="", wr1=""; Variable Descriptions
for(i=0;i<s.length();i++) Name type purpose
s String To accept string
{ ch char To store characters of string
char ch=s.charAt(i); i int To get index number
wr,wr1 String To store words
if(ch==' ')
{
wr=wr+s.charAt(i+1); // original word formation
wr1=s.charAt(i+1)+wr1; // reverse word formation
}
}// end of for
System.out.println(wr.equalsIgnoreCase(wr1)?“palindrome”:“Not a palindrome”);
}
}
//PRO 18:
Define a java class to accept a string, convert the same to uppercase and display only repeated consecutive
letters present in the string.
e.g. Input : CELL PHONE IS NOT ALLOWED IN THE CLASSROOMS
Output : LL LL SS OO
ANS)
class letter_dis
{
public void main(String s)
{
String s1=s.toUpperCase();
System.out.println(s1); Variable Descriptions
int i; char ch,ch1; Name type purpose
s String To accept string
for(i=0;i<s.length( )-1;i++)// up to 2nd last index ch,ch1 char To store characters of string
{ i int To get index number
ch=s1.charAt(i);
ch1=s1.charAt(i+1);
if(ch1==ch)
System.out.println(ch+""+ch1+" ");
}
}
}
21
//PRO 19: piglatin program.
Define a java class to accept a string, convert the same to uppercase and display same in piglatin form.
(piglatin form of a word is obtained with the first vowel present in the word with the remaining letters
present before the first vowel and ending with “AY”)
Sample INPUT : TROUBLE OUTPUT : OUBLETRAY
ANS)
class piglatin
{
public void main(String s)
{
s=s.toUpperCase(); Variable Descriptions
int i; Name type purpose
s String To accept string
for(i=0;i<s.length();i++) ch char To store characters of string
{ i int To get index number
char ch=s.charAt(i);
if("AEIOUaeiou".indexOf(ch)!=-1)
break;
}
System.out.println(s.substring(i)+s.substring(0,i)+"AY");
}}
//PRO 20 :
Define a class to accept two strings, convert them into uppercase, if both the strings are equal, then
display the message “Strings are same”. Otherwise display the words alphabetically in ascending order.
Sample Input-1: s1=“HELLO” s2=“HELLO” Output : “Strings are same”
Input-2: s1=“SKY” s2=“BLUE” Output : BLUE SKY
Input-3: s1=“GOOD” s2=“LUCK” Output : GOOD LUCK
ANS)
class String
{
public static void main(String s1, String s2)
{
s1=s1.toUpperCase();
s2=s2.toUpperCase(); Variable descriptions:
if(s1.equals(s2)) Name Type Purpose
st
s1 String to store 1 word
System.out.println("Strings are same"); s2 String
nd
to store 2 word
else if (s1.compareTo(s2)<0)
System.out.println(s1+" "+s2);
else
System.out.println(s2+" "+s1);
}}
22
//PRO 21 :
Define a class to accept a string, then check and print the index no of last occurrence of an input letter
in the same. If it is not found then display “Not Found”. Use Scanner class to accept word and letter
ANS)
23
//PRO 23:
Write a program to accept a sentence and print only those words which starts with Uppercase vowel.
Example :
INPUT :
“CARRY AN UMBRELLA AND COAT WHEN IT RAINS”
OUTPUT :
AN
UMBRELLA
AND
IT
A NS)
class strpro
{
public void main(String s)
{
s=s+" ";
s=s.toUpperCase();
Variable Descriptions
String wr=""; int i;
Name type purpose
for(i=0;i<s.length();i++) s String To accept string
{ i int To store last index no
ch,ch1 char To store character
char ch=s.charAt(i); wr String To store words one by one
if(ch!=' ')
wr=wr+ch;
else
{
char ch1=wr.charAt(0);
if(“AEIOU”.indexOf(ch1)!=-1)
System.out.println(wr);
wr="";
}
}
}}
//PRO 24: Dry Run following String program and get the answers or outputs.
class Strprog
{
public static void main()
{
24
String p="c:\\school\\class10\\myfile.txt";
int id=p.lastIndexOf('.');
int id1=p.lastIndexOf('\\');
System.out.println(p.substring(0,id1+1));
System.out.println(p.substring((id1+1),id));
System.out.println(p.substring(id+1));
}
}
RES :
c:\school\class10\
myfile
txt
*******
********
// PRO 3 -A program to explain use of constructor overloading
Design a Class called Check to following tasks by using the concept of constructor overloading
i) Check(int m , int n) : to display Prime numbers present between m and n
(A number is said to be a prime number if it is divisible only by 1 and itself and not by any other number.
Example: 3, 5, 7, 11, 13 etc.)
ii) Check(int n) : to check and display input number is an Automorphic number.
(An Automorphic number is the number which is contained in the last digit(s) of its square.
Example 25 is an Automorphic number as its square id 625 and 25 is present as the last two digits.)
iii) Check( ) to display first 10 Fibonacci numbers
First 10 Fibonacci numbers are ----- 0 1 1 2 3 5 8 13 21 34
Variable Description
Ans)
class Check Name type purpose
{ m,n int To store limits
Check(int m, int n) c int To store no of factors Method-1
{ x int To get number in series
int x,c,d; d int To get divisor
for(x=m+1;x<n;x++)
{ n int To store number
c=0; sq int To store square Method-2
for(d=1;d<=x;d++) s1,s2 String To store numbers in string
{
if(x%d==0) x ,y int To find numbers of series
c++; z int To store sum previous numbers Method-3
} t int for loop counter
27
if(c==2)
System.out.println(x+" ");
}
}
Check(int n)
{
int sq=n*n;
String s1=""+n; // to convert number to string
String s2=""+sq; // to convert square of the number to string
if(s2.endsWith(s1))
System.out.println(“Automorphic”);
else
System.out.println(“Not an Automorphic”);
}
Check()
{
int x=0,y=1,t,z;
System.lout.print(x+” “+y);
for(t=3;t<=10;t++)
{
z=x+y;
System.out.print(“ “+z);
x=y;
y=z;
}
}
}// end of class
*********
// PRO 4 : A program to explain use of constructor overloading
Write a java class called Disp to overload constructors to perform following tasks
i) Disp (double a, int y,int n) --- to calculate and display sum of following series
S= a/(y+2)3 + a/(y+3)4 + a/(y+4)5 +…………n terms
ii) Disp (double x, int n) – to calculate and display sum of following series
S=1-x2 + x3 – x4 +--------------------m terms
Ans)
Class Disp
{ Variable Description
Disp(double a, int y, int n) Name type purpose
{ a double To get variable of series
int x, p=2; double s=0;
y int To get variable of series Method-1
for(x=1;x<=n; x++)
n int To get the limit
{
s double To get sum
s=s+a/Math.pow(y+p,p+1);
x int for loop counter
p++;
}
System.out.print(“The sum=”+s); m int To get the limit
} s double To store sum Method-2
y int for loop counter
Disp(double x, int m) x int To get variable of series
{
int y; double s=1;
for(y=2;y<=m; y++)
{
if(y%2==0)
s=s-Math.pow(x,y);
else
28
s=s+Math.pow(x,y);
}
System.out.print(“The sum=”+s);
}
P2)
class pattern2
{ A 1 B 2 C 3 D 4
public void main() E1 F 2 G 3
{ H1 I 2
int i,j;char ch='A'; J 1
for(i=4;i>=1;i--)
{
for(j=1;j<=i;j++)
Variable Description
{
Name type purpose
System.out.print(ch+" "+j+" "); i,j int for loop counters
ch++; ch char To get characters
}
System.out.println();
}
}}
********
P3)
class pattern12
{ 4 3 2 1
public static void main() 4 3 2
{ 4 3
int i,j; 4
for(i=1;i<=4;i++)
{
for(j=4;j>=i;j--)
Variable Description
{
Name type purpose
System.out.print(j+" ");
i,j int for loop counters
}
31
System.out.println();
}}} *****
P4)
class pattern4
{ 1 2 3 4
public static void main() 2 3 4
{ 3 4
int i,j; 4
for(i=1;i<=4;i++)
{
for(j=1;j<=4;j++)
Variable Description
{ Name type purpose
String s=(j<i? " " :""+j); i,j int for loop counters
System.out.print(s+" "); s String To get the character of pattern
}
System.out.println();
}}}
*********
P5)
class pattern5 1 2 3 4 D
{ 5 6 7 C
public static void main() 8 9 B
{ A
int i,j,n=1;char ch='D';
for(i=4;i>=2;i--)
{ Variable Description
for(j=1;j<=i;j++) Name type purpose
{ i,j int for loop counters
System.out.print(n+" "); n int To get the digit of pattern
n++; ch char To get the character of pattern
}
System.out.println(ch--);
}
System.out.println(ch);
}}
********
P6)
class pattern6 1
{ 2 3
public static void main() 4 5 6
{ 7 8 9 10
int i,j,d=1;
for(i=1;i<=4;i++)
{ Variable Description
for(j=1;j<=i;j++) Name type purpose
System.out.print(d++ +" "); i,j int for loop counters
System.out.println(); d int To get numbers
}
}}
********
P7)
class pattern7
{
32
public static void main()
{ 1
int i,j,d=0; 2 3
for(i=1;i<=4;i++) 3 4 5
{ 4 5 6 7
for(j=1;j<=i;j++)
System.out.print(j+d+" ");
Variable Description
d++;
Name type purpose
System.out.println();
d int to get number
}}}
i,j int counters
********
P8)
class pattern8
{
public static void main()
54321
{
int i;
5432 Variable Description
for(i=54321;i>=1;i/=10) 543 Name type purpose
System.out.println(i); 54 i int to get number
5
}}
*******
P9)
class pattern9
{ 5
public static void main() Variable Description
54
{ Name type purpose
543 s int to get number
int i,s=0; 5432 i int for loop counter
for(i=5;i>=1;i--)
54321
{
s=s*10+i;
System.out.println(s);
}}
P10)
class pattern10
A
{
A B
public static void main()
{
A B C
char ch,ch1; A B C D
for(ch='A';ch<='D';ch++) A B C D E
{
for(ch1='A';ch1<=ch;ch1++) Variable Description
System.out.print(ch1+" "); Name type purpose
System.out.println(); ch,ch1 int to get letters
}}}
33
DDA Programs
D1) Define a class to accept values into a 3×3 array and check if it is a special array.
An array is a special array if the sum of the even elements = sum of the odd elements.
Example: A[][]={{ 4 ,5, 6}, { 5 ,3, 2}, { 4, 2, 5}};
Sum of even elements = 4+6+2+4+2 =18 Sum of odd elements= 5+5+3+5=18
Ans)
class DDA1
{
void main(int M[][])
{
int i,j,s1=0,s2=0;
for(i=0;i<=2;i++) Variable Description
{ Name type purpose
for(j=0;j<=2;j++) M[][] int array to store numbers
{ s1,s2 int to store sum of numbers
if(M[i][j]%2==0) i,j int index numbers
s1=s1+M[i][j];
else
s2=s2+M[i][j];
}
}
System.out.println(s1==s2 ? "Special Matrix" : "Not a special Matrix");
}
}
D2) Define a class to accept values into a 4×4 array and display the elements in transpose form. Also calculate
and print the sum of the elements present in boundary.
Ans)
class DDA2
{ Variable Description
void main(int M[][]) Name type purpose
{ M[][] int array to store numbers
s int to store the sum
int i,j,s=0;
i,j int index numbers
for(i=0;i<=3;i++)
{
for(j=0;j<=3;j++)
{
if(i==0 || j==0 || i==3 || j==3) // condition to get the elements present in boundary of a matrix of size 4 x 4
s=s+M[i][j];
System.out.print(M[j][i]+" "); // to display elements of each row
}
System.out.println();
}
System.out.println("The sum="+s);
}
}
34
D3) Define a class to accept values into a 4×4 array and display the elements in matrix form. Also calculate
and print the product of elements present in the diagonals.
Ans)
class DDA3
{
void main(int M[][]) Variable Description
{ Name type purpose
int i,j,p=1; M[][] int array to store numbers
for(i=0;i<=3;i++) p int to store the product
{ i,j int index numbers
for(j=0;j<=3;j++)
{
if(i==j || i+j==3) // formula to get the elements present in diagonals of a matrix of size 4 x 4
p=p*M[i][j];
System.out.print(M[i][j]+" "); // to display elements in a line
}
System.out.println();
}
System.out.println("The product="+p);
}
}
D4) Define a class to accept values into an array of size N×N and calculate and print average of double digit
numbers present in the same.
Ans) Variable Description
class DDA4 Name type purpose
{ M[][] int array to store numbers
void main(int M[][]) s int to store the sum
{ c int to count number of double digits
i,j int index numbers
int i,j,c=0,s=0;
for(i=0;i<=M.length-1;i++)
{
for(j=0;j<=M.length-1;j++)
{
if(M[i][j]>=10 && M[i][j]<=99)
{
s=s+M[i][j];
c++;
}
}}
System.out.print("The average = "+(s/c));
}
}
35
D5) Define a class to accept values into a 4×4 array and replace even numbers present in with 0(zero) and odd
numbers with 1(one) .Display the elements of the modified array in matrix form.
ANS)
class DDA5
{
void main(int M[][])
{
int i,j ;
for(i=0;i<=3;i++)
{
for(j=0;j<=3;j++)
{
M[i][j]=(M[i][j]%2==0 ? 0 : 1);
}
}
for(i=0;i<=3;i++) // to display the array elements in matrix form
{ Variable Description
for(j=0;j<=3;j++) Name type purpose
{ M[][] int array to store numbers
System.out.print(M[i][j]+" "); i,j int index numbers
}
System.out.println( );
}
}}
Number Programs
N1)A program to accept a digit number then check and print it is a DISARIUM number or not
A number will be called DISARIUM if sum of its digits powered with their respective position is equal to
the original number. e.g. 135 is a DISARIUM
(Workings 11+32+53 = 135, some other DISARIUM are 89, 175, 518 etc.)
Ans)
class DISARIUM
{
public void main(int num) Variable Description
{ Name type purpose
int c=0,d,s=0; num int to store number
s int to store the sum
d=num;
d int to store duplicate of input
c=(""+num).length(); // to count no of digits number
if (num>=100 && num<=999) c int to count no of digits
{ r int to store last digit of the number
d=num;
while(d>0) // to find sum of digits with their respective power
{
int r=d%10; // to last digit
s=s+ (int)Math.pow(r,c); // here c is power of the digit according its position
d=d/10;
c--; // to get power of next digit
}
System.out.println(s==num ? "DISARIUM NUMBER" : "NOT A DISARIUM NUMBER");
36
}
else
System.out.println("Invalid Number");
}
}
N2) A program to check and print all double digit Niven number. Also find the frequency of the same.
It is a number which is divisible by the sum of its digits. e.g. 21 is a Niven number because it is divisible
by the sum of its digits. e.g. : 21 –> sum of digits –> 2+1 = 3 and 21 is divisible by 3 –> 21/3 = 7.
ANS)
class Niven
{ Variable Description
public void main() Name type purpose
{ num int to store number
int num,c=0,d,s; s int to store the sum
for(num=10;num<=99;num++) d int to store duplicate of input
{ number
s=0;d=num; c int to store the frquency
while(d>0) r int to store last digit of the number
{
int r=d%10;
s=s+r;
d=d/10;
}
if(num%s==0)
{
System.out.println(num);
c++;
}
}
System.out.println("Frequency="+c);
}}
N3) A program to check and print lead numbers present between two input limits (lb and ub), where the value of lb
must be less than ub. (Lead number is a number in which the sum of its even digits is equal to the sum of odd digits)
ANS)
class Lead_num
{
public void main(int lb , int ub)
Variable Description
{
Name type purpose
int num,d,s1,s2; num int to store number
s int to store the sum
if(lb<ub) d int to store duplicate of input
{ number
for(num=lb+1;num<=ub-1;num++) r int to store last digit of the number
{ lb int to store lower limit
ub int to store upper limit
s1=0;s2=0;d=num;
while(d>0)
{
int r=d%10;
37
if(r%2==0)
s1+=r;
else
s2+=r;
d=d/10;
}
if(s1==s2)
System.out.println(num);
}}
else
System.out.println("Invalid limits");
}
}
Ans:
import java.util.*;
class demo
{
Scanner sc=new Scanner(System.in);
int n;
public void main()
{
int op;
System.out.println("Enter 1 for triangle or 2 for inverted triangle-");
op=sc.nextInt();
38
switch(op)
{
case 1:
pattern1(); Variable Description
break; Name type purpose
case 2: op int To store choice
pattern2(); n int To store no of rounds
break; i ,j int for loop counters
default:
System.out.println("Invalid");
}
}
void pattern1()// to display first pattern
{
System.out.println("Enter the number-");
n=sc.nextInt();
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
System.out.print(i);
System.out.println();
}
}
void pattern2()// to display first pattern
{
System.out.println("Enter the number-");
n=sc.nextInt();
int i,j;
for(i=n;i>=1;i--)
{
for(j=1;j<=i;j++)
System.out.print(i);
System.out.println();
}
} }// end of class
****Best Of Luck****
39