0% found this document useful (0 votes)
3 views

Special Java Programs(Icse-2025).

The document contains sample programs for ICSE-2025, focusing on user-defined functions (UDF) and sorting algorithms in Java. It includes detailed specifications for classes like Courier, BookFair, and Student, along with methods for accepting input, calculating charges or discounts, and displaying results. Additionally, it covers array manipulation techniques such as binary search, bubble sort, and linear search, providing example code and variable descriptions for each program.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Special Java Programs(Icse-2025).

The document contains sample programs for ICSE-2025, focusing on user-defined functions (UDF) and sorting algorithms in Java. It includes detailed specifications for classes like Courier, BookFair, and Student, along with methods for accepting input, calculating charges or discounts, and displaying results. Additionally, it covers array manipulation techniques such as binary search, bubble sort, and linear search, providing example code and variable descriptions for each program.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

SAMPLE PROGRAMS(SECTION -B)(60 MARKS)

(ICSE-2025)
Special note: Practice following programs properly
(*****In Exam. Write variable descriptions for each program)
Programs based on UDF

// PRO 1 (A program to explain UDF with default constructor)


A Courier Service company charges for the parcels of its customers as per the following specifications given below:
Class name: Courier
Member variables:
String name – to store the name of the customer
int w – to store the weight of the parcel in Kg
double charge – to store the charge of the parcel
Member functions:
Courier() -- A default constructor to store legal values in data members
void accept ( ) – to accept the name of the customer, weight of the parcel from the user (using Scanner class)
void calculate ( ) – to calculate the charge as per the weight of the parcel as per the following criteria.
Weight in Kg Charge per Kg
Up to 10 Kgs ₹ 25 per Kg
Next 20 Kgs ₹ 20 per Kg
Above 30 Kgs ₹ 10 per Kg
A surcharge of 5% is charged on the bill if the weight is above 30 kgs.
void disp( ) – to print the name of the customer, weight of the parcel, total bill inclusive of surcharge in a tabular form in the
following format :
Name Weight Bill amount
------- --------- --------------
Define a class with the above-mentioned specifications, create the main( ) method, create an object and invoke the member
methods.
Ans)
import java.util.*;
class Courier
{
Scanner sc=new Scanner(System.in);
String name;
int w;
double charge;
Courier()
{
name=""; Variable Description:
w=0;charge=0.0;
} Name Type purpose
void accept()
name String To store name
{
System.out.println("Enter the name & weight-"); w int To store weight
name=sc.nextLine(); charge double to store charge
w=sc.nextInt();
}
void calculate()
{

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();
}
}

// PRO 3 (A program to explain UDF with parameterized constructor)


Define a Java class Student described as below:
Data members/instance variables:
Name, age, ml, m2, m3 (marks in 3 subjects), maxi, avg;
Member Methods:
Student(…….)---------A parameterized constructor to initialize the data members
void compute( )--------To compute the average and the maximum out of three marks
void disp( )-------------To display the name, age, marks in three subjects, maximum and average.
Write a main( ) method to create an object of a class and call the above member methods.
Ans )

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");
}
}

// PRO 2 Program to show the use of Bubble sorting


Write a Java program to input price of 50 products (with decimal part) in a one-dimensional array, then
arrange price in descending order using bubble sort method. Display only first ten elements of the list.

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]);
}}
**********

// PRO 3 Program to show the use of Selection sorting


Write a Java program to input price of 50 products (with decimal part) in a one-dimensional array, then
arrange price in ascending order using selection sort method. Display original array and the arranged array.

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

for(id=0;id<15;id++) c1 int to count lowercase letters

{ c2 int to count no. of uppercase vowels


if (Character.isLowerCase(ch[id]))
c1++;
if("AEIOU".indexOf(ch[id])!=-1) //vowel check
c2++;
} // end of for loop
System.out.println("No of lowercase letters="+c1);
System.out.println("No of uppercase vowels="+c2);
}
}
// PRO 2:
Define a class to accept and store 10 strings into the array then count and display the frequency of an input
word present in it. Also display the words of the array in one line (other than the input word)
ANS)
class Strpro_array
{
public void main(String s[],String wr)
{
Variable descriptions:
int i,c=0; Name Type Purpose
for(i=0;i<s.length ;i++) s[] String array to store 10 strings
{ i int to store index number
if (s[i].equals(wr)) wr String to store the word to be searched
c++; c int to store frequency of the searched word
else
System.out.print(s[i]+ " ");
}
12
System.out.println("\nFrequency="+c);
}}
// PRO 3:
Define a class to accept a string, first convert the same to a character array then count and display how
many alphabets, digits, whitespaces & other symbols are present in the array separately.
ANS)
class Counter
{
public void main(String s)
{
char ch[]=s.toCharArray(); // string is converted to an array
int c1=0,c2=0,c3=0,c4=0,i;
for(i=0;i<ch.length;i++) Variable descriptions:
{ Name Type Purpose
if(Character.isLetter(ch[i])) s String to store string
ch[] char array to store characters
c1++; // for letters i int to store index number
else if(Character.isDigit(ch[i])) c1,c2,c3,c4 int counters
c2++; // for digits
else if(Character.isWhitespace(ch[i]))
c3++; // for blank spaces
else
c4++; // for symbols
}
System.out.println("Letters="+c1);
System.out.println("Digits="+c2);
System.out.println("Whitespaces="+c3);
System.out.println("Symbols="+c4);
}
}

// 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 10 ENCODING PROGRAM


Write a program to accept a string. Convert the string to uppercase, then shift each letter of English alphabet
one step towards right (Convert „Z‟ to „A‟). Characters other than alphabets remain same. Print the original &
new string.
Input : ABDFTRZAK@100
output : BCEGUSABL@100
ANS)
class strpro1
{ Variable Descriptions
public static void main(String s) Name Type Purpose
{ s String To accept string
s1 String To store final word
s=s.toUpperCase(); ch char To store characters of the string one by one
String s1=""; i int To get index number
int i;
for(i=0;i<s.length();i++)
{
char ch=s.charAt(i);
if(Character.isLetter(ch))
ch=(ch=='Z' ? 'A' :++ch);
16
s1=s1+ch; // character is added to new string
}
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)

import java.util.*; // By using Scanner class


class Check {
Scanner sc=new Scanner(System.in);
String s; char ch;
public void main()
{
System.out.println("enter the word-"); Variable Descriptions
s=sc.next(); Name type purpose
s String To accept string
System.out.println("enter the letter-"); ch char To store letter to be searched
ch=sc.next().charAt(0);
if(s.lastIndexOf(ch)==-1)
System.out.println("Not Found");
else
System.out.println("Index no of last occurrence="+s.lastIndexOf(ch));
}}
//PRO 22:
Define a class to accept a word, then display the same as specified below.
e.g. INPUT: coMpUter OUTPUT : RomputeC
ANS)
class demo
{ Variable Descriptions
public void main(String s) Name type purpose
s String To accept string
{ id int To store last index no
s=s.toUpperCase();
int id;
id=s.length()-1; // to store last index no
System.out.println(s.charAt(id)+s.substring(1,id).toLowerCase()+s.charAt(0));
}}

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
*******

Programs based on Method/Function & Constructor overloading


// PRO 1 Program to show the use of function overloading with main()
Design a class to overload a function Sum( ) as follows:
(i) int Sum(int L, int U) – with two integer arguments (L and U) calculate and return sum of all the odd
numbers present between L and U.
Sample input: L=4 and U=16
Sample output: sum = 5 + 7 + 9 + 11 + 13 + 15
(ii) double Sum( double N ) – with one double arguments(N) calculate and return the product of the
following series:
sum = 1.0 X 1.2 X 1.4 X …………. X N
(iii) int Sum(int N) - with one integer argument (N) calculate and return sum of only even digits of the
number N.
Sample input : N=43962
Sample output : sum = 4 + 6 + 2 = 12
Ans)
class overload
{ Variable Description
public int calc(int L , int U) Name type purpose
{ L,U int To store lower & upper limit
s int To store sum Method-1
int s=0,n;
n int for loop counter
for(n=L+1;n<U;n++)
{ N int To store upper limit
if(n%2==1) P double To store product Method-2
s=s+n; m int for loop counter
}
N1 int To store number
return(s);
s int To store sum Method-3
} d int To store last digit
public double calc(double N)
{
25
double p=1,m;
for(m=1.0;m<=N;m+=.2)
{
p=p*m;
}
return(p);
}
public int calc(int N1)
{
int s=0;
while(N1>0)
{
int d=N1%10;
if(d%2==0)
s=s+d;
N1=N1/10;
}
return (s);
} }//end of class
********

// PRO 2 A program to show the use function overloading without main( )


Design a class to overload a function Sum( ) as follows:
i) double Sum(int N) – with one integer argument(N) calculate and return sum of following series
S= ( 1+2 ) / (2*3) + (2+3) / (3 * 4) + (3+4)/(4 * 5) + ------------ N terms
ii)double Sum( double x, int N ) – with one double and one integer arguments(x,n) calculate and return
sum of following series
S=1+x2 /2! +x3/3!+--------- n terms
iii) void Sum()- with no argument to calculate and display following series
S= .5 + .55+.555+.5555+.55555
Ans)
class overload1
{
double Sum( int N)
{
int x; double s=0; // s is used to store sum
for(x=1;x<=n;x++)
{
s=s+(x+(x+1))/((x+1)*(x+2));
}
return(s);
}
double Sum( int x, int n)
{
int p,y,f;
double s=1; // s is to sum
for(p=2;p<=n;p++)
26
{ Variable Description
f=1; Name type purpose
for(y=1;y<=p;y++) N int To store limit/no of rounds
f=f*y; s int To store sum Method-1
s=s+Math.pow(x,p)/f; x int for loop counter
}
n int To store upper limit
return(s);
f double To store factorial Method-2
} p,y int for loop counter
void Sum() x int variable to get series terms
{ s int to store sum
double s=0,n=.5;
int x; x int for loop counter
for(x=1;x<=5;x++) s double To store sum Method-3
{ n int To get terms of series
s=s+n;
if(x<5)
System.out.print(n+"+");
else
System.out.print(n);
n=n/10+.5;
}
System.out.println("\nThe sum="+s);
}
} // end of class

********
// 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);
}

}// end of class

Some special Java Series Programs


//PRO 1:
a) S= 1 + ( 1+2) + (1+2+3) +------------------- 10 terms (Find sum of the Series)
Ans)
class series1 Variable Description
Name type purpose
{
x int for loop counter
public void main()
sum double To store sum of series
{ s int To store term value
double sum=0,s=0,x;
for(x=1;x<=10;x++)
{
s=s+x;// in variable s sum of digits present in each term is stored stepwise – R1=1 , R2=1+2=3, R3=3+3=6…
sum=sum+s; // here sum is used to store final sum value
}
System.out.println("The sum="+sum);
}}
*****

b) S=a+ a2 / 2 + a3/ 3 + a4/ 4 + --------------- an/ n (Find sum of the Series)


Ans) Variable Description
class series2 Name type purpose
{ t int for loop counter
public void main(double a, int n) s double To store sum of series
{ n int To store no of rounds
int t; a double for the value of variable specified in series
double s=0;
for(t=1;t<=n;t++)
{
s=s+(Math.pow(a,t)/t);
}
System.out.println("the sum="+s);
}
}
******
c) S= (a+3)/5 + ( a+5) /8 +(a+7)/11 +------------ (a+21)/32 (Find sum of the series)
Ans)
class series3 Variable Description
{ Name type purpose
public void main (double a ) t int for loop counter
{ s double To store sum of series
int t; d int To get value of the terms of series
double s=0,d=5; a double for the value of variable specified in series
for(t=3;t<=21;t+=2)
29
{
s=s+((a+t)/d);
d+=3;
}
System.out.println(“the sum=”+s);
}}
*******
d) S= 1- a/2 + 3 – a/4 + 5 - a/6 -------------- n terms (Find sum of the series)
Ans)
Variable Description
class series4
Name type purpose
{ t int for loop counter
public void main(double a, int n ) s double To store sum of series
{ a double for the value of variable specified in series
int t; n int To store no of rounds
double s=0;
for(t=1;t<=n;t++)
{
if(t%2==1)
s=s+t;
else
s=s-(a/t);
}
System.out.println("the sum="+s);
}}
e) S= 1n + 2 n-1 + 3 n-2 +………… n 1 (Find sum of the series)
Ans)
class series5
{
public void main(double n )
{
int t;
double s=0,p=n; // value of “n” is copied to “p” Variable Description
for(t=1;t<=n;t++) Name type purpose
{ t int for loop counter
s=s+Math.pow(t,p); s double To store sum of series
p--; p int To get power
} n int To store no of rounds
System.out.println("the sum="+s);
}}
*******
f) Display the series : 1+12+123+1234+12345 , Also calculate the sum
Ans)
class series6 {
public void main( )
{ Variable Description
int t; long s=0,sum=0; // s to get value of the terms Name type purpose
for(t=1;t<=5;t++) { t int for loop counter
s=s*10+t; s long To terms of the series
if(t<=4)// up to 2nd last term sum long To store final sum
System.out.print(s+"+");
30
else
System.out.print(s);
sum=sum+s;
}
System.out.print("\nThe sum="+sum); }}

Some special Java Pattern Programs


P1)
class pattern1 SMAR T
{ SMAR
public void main(String s) // e.g. input string is “SMART” SMA
{ S M
char ch[]=s.toCharArray(); S
int i,j;
for(i=ch.length-1;i>=0;i--) // no of rounds Variable Description
{ Name type purpose
for(j=0;j<=i;j++) // no of sub rounds i,j int for loop counters
System.out.print(ch[j]+" "); ch[] int To store letters
System.out.println();
}}}
********

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");
}
}

Java Menu-driven Program


PRO 1 A program to show the use of explain use of Menu driven
Write a menu driven program to generate a triangle or an inverted triangle till n terms based upon the user‟s
choice of triangle to be displayed. (Use switch case)
Input: Type 1 for a triangle and type 2 for an inverted triangle
Example Input : Type : 1
Enter the number of terms 5
Output:
1
22
333
4444
55555
Example 2: Input: Type 2
Enter the number of terms 6
Output:
666666
55555
4444
333
22
1

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

You might also like