Escolar Documentos
Profissional Documentos
Cultura Documentos
ob1.input(); ob1.display();
}
}
//write a program to find the sum of two angles. use object passing
import java.io.*;
public class angle
{
int deg, min;
public angle() //constructor
{
deg=0;
min=0;
}
public void inputangle() throws IOException
//input degrees and minutes of the
angle
{
DataInputStream ob=new DataInputStream(System.in);
System.out.println("enter degrees and minutes");
this.deg=Integer.parseInt(ob.readLine());
this.min=Integer.parseInt(ob.readLine());
}
public void dispangle()
//to display the angle
{
System.out.println("angle is "+this.deg+" deg "+this.min+"\'");
}
public angle sumangle(angle a, angle b)
//to find the sum of two angles
{
angle c=new angle();
//the object which has the sum and is to be returned
c.deg=a.deg+b.deg;
c.min=a.min+b.min;
if(c.min>=60)
{
c.deg=c.deg+1;
c.min=c.min-60;
}
return c;
}
public static void main() throws IOException
{
DataInputStream ob=new DataInputStream(System.in);
angle a=new angle();
angle b=new angle();
angle c=new angle(); //object to recieve the object sent by sumangle and
store the sum of angles
a.inputangle();
b.inputangle();
c=c.sumangle(a,b);
c.dispangle();
}
}
{
front++;
}
}
return p;
}
void show()
//to display the values in the queue
{
if(front==-1)
System.out.println("underflow!!!");
else
{
for(int i=front;i<=rear;i++)
{
System.out.println(num[i]);
}
}
}
}
public class array_queue
{
static queue q;
public static void main() throws IOException
{
DataInputStream ob=new DataInputStream(System.in);
System.out.println("enter size");
int size=Integer.parseInt(ob.readLine());
q=new queue(size);
int ch=0;
while(ch!=4)
{
System.out.println("1.insert\n2.delete\n3.display\n4.exit\nenter choice");
ch=Integer.parseInt(ob.readLine());
switch(ch)
//to check the choice of user to insert or delete value or
display the queue
{
case 1:System.out.println("enter number");
int n=Integer.parseInt(ob.readLine());
q.insert(n);
break;
case 2:System.out.println(q.delete()+" deleted");
break;
case 3:q.show();
break;
}
}
}
}
//TO INPUT 10 NUMBERS IN A ARRAY & STORE ALL UNIQUE NUMBERS IN ANOTHER
ARRAY
import java.io.*;
public class ARRAY_UNIQUE_NUMBER
{
public static void main() throws IOException
{
DataInputStream ob=new DataInputStream(System.in);
int a[]= new int[10];//ARRAY DECLARED TO GET 10 NUMBER'S FROM USER
int b[]= new int[10];//ARRAY TO STORE ALL UNIQUE NUMBER'S FROM ARRAY a
int k=0;
for(int i=0;i<10;i++)//LOOP TO GET 10 NUMBER'S FROM USER
{
System.out.print("Input a["+i+"]");
a[i]= Integer.parseInt(ob.readLine());
int flag=0;
for(int j=0;j<i;j++)//TO CHECK THE COUNT OF a[i] IN ARRAY a
{
if(a[i]==a[j])
flag++;
}
if(flag==0)
{
b[k]=a[i];//TO STORE THE UNIQUE NUMBER'S FROM ARRAY a[] TO ARRAY
b[]
k++;
}
}
for(int i=0;i<k;i++)//TO PRINT THE UNIQUE NUMBER'S STORED IN ARRAY b
{
System.out.println(b[i]);
}
}
}
//WRITE A PROGRAM TO SORT A 2D ARRAY ROW WISE
import java.io.*;
public class ARRAYSORTROWWISE
{
public static void main() throws IOException
{
DataInputStream ob=new DataInputStream(System.in);
int a[][]=new int[3][3];//TO DECLARE AN ARRAY OF 3X3
for(int i=0;i<3;i++)
//LOOPS TO INPUT VALUES IN THE ARRAY
{
System.out.println();
for(int j=0;j<3;j++)
{
a[i][j]= Integer.parseInt(ob.readLine());
System.out.print("
");
}
}
//LOOPS TO SORT THE ARRAY ROW WISE
for(int k=0;k<3;k++)
{
for(int i=0;i<3;i++)
{
for(int j=0;j<2-k;j++)
{
if(a[i][j]>a[i][j+1])
{
int b=a[i][j];
a[i][j]=a[i][j+1];
a[i][j+1]=b;
}
}
}
}
//LOOPS TO PRINT THE SORTED ARRAY ROW-WISE
for(int i=0;i<3;i++)
{
System.out.println();
for(int j=0;j<3;j++)
{
System.out.print(a[i][j] +"
");
}
}
}
}
/**
* Write a description of class b here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class b
{
// instance variables - replace the example below with your own
private int x;
/**
* Constructor for objects of class b
*/
public b()
{
// initialise instance variables
x = 0;
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return
the sum of x and y
*/
public int sampleMethod(int y)
{
// put your code here
return x + y;
}
}]
System.out.println("INPUT NUMBER");
a[i]=Integer.parseInt(ob.readLine());
}
//LOOPS FOR PERFOMATION OF BUBBLE SORTING
for(int i=0;i<10;i++)
{
for(int j=0;j<9-i;j++)
{
if(a[j]<a[j+1])//FOR COMPARISION OF THE VALUES
{
//FOR SWAPING
int t=a[j];
a[j]=a[j+1];
a[j+1] =t;
}
}
}
//LOOP FOR DISPLAYING THE SORTED ARRAY ON SCREEN
for(int k=0;k<10;k++)
{
System.out.println(a[k]);
}
}
}
//WRITE A PROGRAM TO PRINT THE DAY & DATE FOR THE DAY NUMBER WITH
RESPECT TO THE DAY NUMBER OF YEAR
import java.io.*;
public class CALENDAR
{
public static void main() throws IOException
{
DataInputStream ob=new DataInputStream(System.in);
System.out.print("input number");
int d= Integer.parseInt(ob.readLine());
//TO STORE THE DAY NUMBER
WITH RESPECT TO YEAR
String months[]
={"jan","feb","mar","apr","may","june","july","aug","sept","oct","nov","dec"};
String
days[]={"wednesday","thursday","friday","saturday","sunday","monday","tuesday"
};
int days_of_month[]={31,28,31,30,31,30,31,31,30,31,30,31};
int copy=d;
for(int i=0;i<12;i++)
//LOOP TO THE MONTH NUMBER OF
THE DAY INPUTED
{
if(d>days_of_month[i])
d=d-days_of_month[i];
else
{ System.out.print(d+" "+months[i]);
break;
}
}
//TO GET THE SPECIFIC DAY OF WEEK
int t=copy%7;
if(t==0)
System.out.print("Day is =" + days[6]);
else
System.out.print("day is =" +days[t-1]);
}
}
System.out.print(print+"\t");
final1++;
}
}
}
}
import java.io.*;
public class codes
{
public static void main() throws IOException
{
DataInputStream ob=new DataInputStream(System.in);
System.out.println("enter length and code");
int n=Integer.parseInt(ob.readLine());
String s=ob.readLine();
int t=0;
char ch[]={'A','C','E','G','I','K'};
if(s.length()!=n || n>6)
t=1;
else
{
for(int i=0;i<n;i++)
{
char c=s.charAt(i);
int k=0;
for(int j=0;j<6;j++)
{
if(c==ch[j])
{
k=1;
break;
}
}
if(c<65 || c>90 || k==0)
{
t=1;
break;
}
}
}
if(t==1)
System.out.println("invalid");
else
System.out.println("valid");
}
}
//create a class combine to merge two arrays with data members-int com[],int sizeand member functions-void inputarray(), void mix(combine ,combine), void
display(), void main()
import java.io.*;
public class combine
{
int com[];
int size;
public combine(int nn)
{
size=nn;
}
public void inputarray()throws IOException
{
DataInputStream ob=new DataInputStream(System.in);
com=new int[this.size];
for(int i=0;i<this.size;i++)
{
System.out.println("enter no.");
com[i]=Integer.parseInt(ob.readLine());
}
}
public void mix(combine A,combine B)
{
int j;com=new int[this.size];
for( j=0;j<A.size;j++)
{
com[j]=A.com[j];
}
for(int k=0;k<B.size;k++)
{
com[j+1]=B.com[k];
}
}
public void display()
{
for(int x=0;x<size;x++)
{
System.out.println(com[x]);
}
}
public static void main()throws IOException
{
DataInputStream ob=new DataInputStream(System.in);
System.out.print("enter size");
int n1=Integer.parseInt(ob.readLine());
int n2=Integer.parseInt(ob.readLine());
combine A=new combine(n1);
combine B=new combine(n2);
combine C=new combine(n1+n2);
A.inputarray();
B.inputarray();
C.mix(A,B);
C.display();
}
}
/*a library issues books on rental basis at a 2% charge on the cost price of the book
per day. if the book is returned after 7 days, fine is also charged as per the excess
days.
* number of excess days
fine per day
* 1 to 5
2.0
* 6 to 10
3.0
* above ten days
5.0
* design a super class library to display the name, author and price of the book. sub
class compute to calculate fine and display the details of books with total days, fine
and total amount
*/
import java.io.*;
class library
//super class
{
String name,author;
double p;
library(String n,String a,double p1)
//parametrized constructor
{
name=n;
author=a;
p=p1;
}
void show()
//display the details of the book
{
System.out.println("name-"+name+"\nauthor-"+author+"\nprice-Rs."+p);
}
}
public class compute extends library
//sub class
{
int d,f;
compute(String n,String a,double p1,int d1)
//parametrized constructor
{
super(n,a,p1);
d=d1;
f=0;
}
void fine()
//to calculate the fine as per the extra days
{
int extra=d-7;
if(extra<=0)
f=0;
else if(extra<=5)
f=2*extra;
else if(extra<=10)
f=3*extra;
else
f=5*extra;
}
void display()
//to display details of book, days taken to return, fine and total
amount to be paid
{
super.show();
fine();
double total=(2*super.p/100)+f;
System.out.println("days taken to return are "+d+"\nfine is Rs."+f+"\nthe total
amount to be paid is Rs."+total);
}
public static void main() throws IOException
//to input details of book and
call the other methods
{
DataInputStream ob=new DataInputStream(System.in);
System.out.println("enter the name of the book and name of the author");
String n=ob.readLine();
String a=ob.readLine();
System.out.println("price of the book");
double pr=Double.parseDouble(ob.readLine());
System.out.println("days of return");
int d=Integer.parseInt(ob.readLine());
compute ob1=new compute(n,a,pr,d);
ob1.display();
}
}//input a date and year and find the date after n days of the entered date(n be
entered by the user)
import java.io.*;
public class days
{
public static void main() throws IOException
{
DataInputStream ob=new DataInputStream(System.in);
System.out.println("DAY NUMBER");
int day=Integer.parseInt(ob.readLine());
System.out.println("YEAR");
int year=Integer.parseInt(ob.readLine());
System.out.println("DAY AFTER(N)");
int day_after=Integer.parseInt(ob.readLine());
int sum=0;
int newdate=0;
int p=0;
String t="";
int days=day;
int m[]={31,29,31,30,31,30,31,31,30,31,30,31};
String
years[]={"January","February","March","April","May","June","July","August","Septem
ber","October","November","December"};
if(year%4==0)
//to check for leap year
{
m[1]=29;
}
else
{
m[1]=28;
}
if((day>=1&&day<=366)&&(year>=1000&&year<=9999)&&(day_after>=1&&day
_after<=100))
//to check the validity of date
{
for(int i=0;i<12;i++)
//to print the date entered by user
{
if(day>=0&&day<=m[i])
{
System.out.println(day+" "+years[i]+" "+year);
break;
}
else
{
day=day-m[i];
p++;
}
}
System.out.println("DAY AFTER "+day_after+" DAYS");
days=days+day_after;
int k=0;
if(days>=366)
{
days=days-366;
k++;
}
for(int i=0;i<12;i++)
//to display date if the date is valid else
subtract the days of mont accordinf to month number
{
if(days>=0&&days<=m[i])
{
if(k!=0)
System.out.println(days+" "+years[i]+" "+(year+1));
else
System.out.println(days+" "+years[i]+" "+(year));
break;
}
else
{
days=days-m[i];
}
}
}
}
}
//decode the entered string. this encryption system uses a shifting process to hide a
message. The value of the shift can be in the range 1 to 26. For example a shift of 7
means that A = U, B =V,C = W, etc.
import java.io.*;
public class Decode
{
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter Coded Text : "); // inputting coded text
String s = br.readLine();
int l = s.length();
s = s.toUpperCase(); // converting the coded text into Uppercase
s = s + " "; // adding a space at the end
if(l>=100) // checking whether length of inputted code is less than 100
System.out.println("!!! Invalid Length of Coded Text !!!");
else
{
System.out.print("Enter the Shift Value : ");
int shift = Integer.parseInt(br.readLine());
if(shift<1 || shift>26) // checking whether shift value is between 1 and 26
System.out.println("!!! Invalid Shift Value !!!");
else
{
int a, b;
char ch1, ch2;
String dec=""; //new String for storing the decoded text
for(int i=0; i<l; i++)
{
ch1 = s.charAt(i); // extracting characters one by one
ch2 = s.charAt(i+1); // extracting the next character
/* Below we are adding shift value to the characters
* if ch1 = 'A' and shift = 7,
* then ch1 + shift - 1 will give us: 'A'+7-1 = 65+7-1 = 71
* which is the ASCII value of 'G'
*/
a = ch1 + shift - 1; // storing ASCII values after adding shift to the
current character
b = ch2 + shift - 1; // storing ASCII values after adding shift to the next
character
/* If the currrent character and the next character are both 'Q' then we
have a 'space'
* hence the ASCII value should be 32
*/
if((char)a == 'Q' && (char)b == 'Q')
{
a = 32;
i++;
}
/* If ASCII value after adding the shift becomes more than 90,
* then we subtract 26 from it, to make it circular,
* eg. 'U'+7-1 = 85+7-1 = 91, but we want 'A' whose ASCII value is 65
* so 91-26 will give us 65
*/
if(a>90)
a = a - 26;
if(ch1 != ' ')
dec = dec + (char)a; // finally adding the decoded character to the
new String
}
System.out.println("Decoded Text : "+dec);
}
}
}
}
// PROGRAM TO GET AN AMMOUNT FROM USER & TELL THE NUMBER OF NOTES OF
EACH DENOMINATIONS
import java.io.*;
public class DENOMINATIONS
{
public static void main() throws IOException
{
DataInputStream ob=new DataInputStream(System.in);
System.out.print("Input ammount");
int sum= Integer.parseInt(ob.readLine());//TO GET THE AMMOUNT FROM
THE USER
int a[]={1000,500,100,50,20,10,5,2,1};
for(int i=0;i<9;sum=sum%a[i],i++)//LOOP FOR GATING NUMBER OF NOTES
OF EACH DENOMINATIONS
{
int p=sum/a[i];//TO GET THE COUNT OF THE NOTES
if(p==0)
{
continue;
}
System.out.println(a[i]+ "*"+ p+"="+a[i]*p);
p=0;
}
}
}
//WRITE A PROGRAM TO INPUT NUMBERS IN AN 2D ARRAY OF ORDER[5]
[4]&DISPLAY THE NUMBERS//
import java.io.*;
public class DISPARRAYVALSIMPLY
{
public static void main() throws IOException
{
DataInputStream ob=new DataInputStream(System.in);
int a[][]=new int[5][4];//FOR DECLARING ARRAY OF ORDER [5][4]//
//LOOPS FOR INPUTING NUMBERS IN 2D ARRAY//
for(int i=0;i<5;i++)
{
for(int j=0;j<4;j++)
{
System.out.print("input number at a[" + i+"][" +j+"]");
a[i][j]=Integer.parseInt(ob.readLine());
}
}
//LOOPS FOR DISPLAYING THE VALUES OF ARRAY//
for(int i=0;i<5;i++)
{
for(int j=0;j<4;j++)
{
System.out.println("number at a[" + i+"][" +j+"]" +a[i][j]);
}
}
}
}
");
}
}
}
}
//to display the marks from the file student which are above 75
import java.io.*;
public class FILE_READER_MARKS
{
public static void read_File() throws IOException
{
FileReader fr=new FileReader("Student.txt");
BufferedReader br=new BufferedReader(fr);
int s=0;
int a[]=new int[10];
String t;
while((t=br.readLine())!=null)
{
int b=Integer.parseInt(t);
if(b>=75)
{
a[s]=b;
s++;
}
}
br.close();
fr.close();
for(int i=0;i<s;i++)
{
System.out.println(a[i]);
}
}
//a super class record has been defined to store names and age of n players. define
a sub class find to display the names and age of all players and find the name of
youngest player
import java.io.*;
class record
//super class
{
int age[];
String name[];
int n;
public record(int m)
//parametrized constructor
{
n=m;
age=new int[n];
name=new String[n];
}
{
DataInputStream ob=new DataInputStream(System.in);
System.out.println("enter number of players");
int n=Integer.parseInt(ob.readLine());
find ob1=new find(n);
ob1.youngest();
ob1.display();
}
}
//create a multiple functions program input a decimal number and display its
equivalent octal and hexadecimal value
import java.io.*;
public class hexoct
{
public static int input()throws IOException
//to input the decimal
number
{
DataInputStream ob=new DataInputStream(System.in);
System.out.print("Input decimal number");
int z=Integer.parseInt(ob.readLine());
return z;
}
public static int convertToOctal(int x)
//to convert the number into
octal
{
int a[]=new int[20];
int i=0;
for(;x>0;x=x/8)
{
a[i]=x%8;
i++;
}
int number=0;
for(int j=i-1;j>=0;j--)
{
number=(number*10)+a[j];
}
return number;
}
public static String convertToHexadecimal(int y)
//to convert the number
into hexadecimal
{
char a[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
int b[]= new int[16];
String result="";
int i=0;
for(;y>0;y=y/16)
{
int r=y%16;
b[i]=r;
i++;
}
for(int j=i-1;j>=0;j--)
{
result=result+a[b[j]];
}
return result;
}
public void display() throws IOException
values
{
int num=input();
int octal=convertToOctal(num);
System.out.println("number=" +num+"\n octalform="+octal);
String hexa=convertToHexadecimal(num);
System.out.print("number=" +num+"\n hexadecimalform="+hexa);
}
public static void main()throws IOException
function
{
hexoct ob=new hexoct();
ob.display();
}
}
/*A super class Detail has been defined to store the details of a customer. Define a
subclass Bill to compute the monthly telephone charge of the customer as per the
chart given below:
Number Of Calls
Rate
1 100
Only Rental charge
101 200
60 paisa per call + rental charge
201 300
80 paisa per call + rental charge
Above 300
1 rupee per call + rental charge
*/
import java.io.*;
class Detail //superclass
{
String name, address;
long telno;
double rent;
Detail(String n1, String a1, long t1, double r1)
{
name = n1;
address = a1;
telno = t1;
rent = r1;
}
void show()
{
System.out.println("Name of customer = "+name);
System.out.println("Address = "+address);
double r1=Double.parseDouble(br.readLine());
System.out.print("Enter the number of calls : ");
int c=Integer.parseInt(br.readLine());
Bill ob=new Bill(n1,a1,t1,r1,c); //creating object of subclass
System.out.println("*** Output ***");
ob.cal();
ob.show(); //calling show() function of subclass
}
}
//A super class Record has been defined to store the names and ranks of 50
students. Define a sub class Rank to find the highest rank along with the name.
import java.io.*;
class Record //superclass
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String name[];
int rnk[];
Record()
{
name = new String[5];
rnk = new int[5];
}
void readvalues()throws IOException
{
System.out.println("*** Inputting The Names And Ranks ***");
for(int i=0;i<5;i++)
{
System.out.print("Enter name of student "+(i+1)+" : ");
name[i]=br.readLine();
System.out.print("Enter his rank : ");
rnk[i]=Integer.parseInt(br.readLine());
}
}
void display()
{
System.out.println("Name\t\tRank");
for(int i=0;i<5;i++)
{
System.out.println(name[i]+"\t\t"+rnk[i]);
}
}
}
class Rank extends Record//subclass
{
int index;
Rank()
{
super(); //invoking the constructor of superclass
index = 0;
}
void highest()
{
int min = rnk[0];
for(int i=0;i<5;i++)
{
if(rnk[i]<min)
{
index = i;
}
}
}
void display()
{
super.display(); //calling the superclass function display()
System.out.println("nTop most rank = "+rnk[index]);
System.out.println("Student with topmost rank = "+name[index]);
}
}
public class inheritence_rank extends Rank //Class which will contain the main()
method and execute the program
{
public static void main()throws IOException
{
Rank ob=new Rank(); //creating object of subclass
ob.readvalues(); //calling radvalues() function of superclass to input the
names and ranks
ob.highest(); //calling the function highest() for finding index of topmost rank
System.out.println("*** Output ***");
ob.display(); //calling display() function of subclass
}
}
//create a mxm array(m entered by user) and arrange the values in that array from
1 to m^2 in a circular manner(from outside to inside)
import java.io.*;
public class INPUT_NUMBERS_IN_ARRAY_IN_CIRCULAR_MANNER
{
public static void main() throws IOException
{
DataInputStream ob=new DataInputStream(System.in);
int m=Integer.parseInt(ob.readLine());
int a[][]=new int[m][m];
int i=0,k=1,j=0,u=m-1,n=m,t=0,s=m-1,q=0;
outer:while(k<m*m+1)
{
for(j=k;j<n;j++)
//to input values in the upper row(left to right)
{
a[t][j]=k++;
if(k==m*m+1)
break outer;
}
for(i=t+1;i<n;i++)
//to input the values in the right collumn(top to
bottom)
{
a[i][s]=k++;
if(k==m*m+1)
break outer;
}
for(j=n-2;j>=t;j--)
//to input values in the lower row(right to left)
{
a[u][j]=k++;
if(k==m*m+1)
break outer;
}
for(i=n-2;i>t;i--)
//to input values in the left collumn(bottom to top)
{
a[i][q]=k++;
if(k==m*m+1)
break outer;
}
t++; s--;n--;q++;u--;
}
for(int c=0;c<m;c++)
{
System.out.println();
for(int d=0;d<m;d++)
{
System.out.print(a[i][j]+"\t");
}
}
}
}
}
else
{
System.out.print("
");
System.out.println();
}
}
}
}
}
}
}
}
/*a class Matrix has a 2-d array of order m x n(m,n<25). find the difference of two
matrices.
}
System.out.println("]");
}
}
public static void main() throws IOException
{
DataInputStream ob=new DataInputStream(System.in);
System.out.println("enter size of matrices m,n");
int m1=Integer.parseInt(ob.readLine());
int n1=Integer.parseInt(ob.readLine());
matrix a=new matrix(m1,n1);
matrix b=new matrix(m1,n1);
System.out.println("------1'st array------");
a.fillarray();
System.out.println("------2'nd array------");
b.fillarray();
matrix c=a.submat(b);
System.out.println("---1'st matrix---");
a.display();
System.out.println("---2'nd matrix---");
b.display();
System.out.println("--their difference---");
c.display();
}
}
//create a 4x4 matrix and display its mirror matrix,i.e., reverse the order of rows
import java.io.*;
public class MIRRORMAT2
{
public static void main() throws IOException
{
DataInputStream ob=new DataInputStream(System.in);
int a[][]=new int[4][4];
for(int i=0;i<4;i++)
//to input values in array
{
for(int j=0;j<4;j++)
{
System.out.print("Input Number");
a[i][j]= Integer.parseInt(ob.readLine());
}
}
for(int i=3;i>=0;i--)
//to display the mirror image of array
{
System.out.println();
for(int j=0;j<4;j++)
{
System.out.print(a[i][j]+"
");
}
}
}
}
//wap to input a 3x3 matrix and display its mirror matrix,i.e.,reverse the order of
collumns
import java.io.*;
public class MIRRORMATRIX
{
");
}
}
//check if a number is a neon number.A neon number is that where sum of digits of
square of the number is equal to the number.
import java.io.*;
public class Neon
{
public void main() throws Exception
{
DataInputStream br=new DataInputStream(System.in);
System.out.println("Enter the number:");
int n=Integer.parseInt(br.readLine());
int sq=n*n;
int sum=0;
while(sq>0)
//to find the sum of digits of square of number
{
sum=sum+sq%10;
sq=sq/10;
}
if(n==sum)
System.out.println(n+ " is a neon number");
else
System.out.println(n+ " is not a neon number");
}
}
//to input a number and print the longest series of non-prime numbers from 1 to
that number
import java.io.*;
public class non_prime_series
{
public static void main() throws IOException
{
}
}
//INPUT 10 NUMBER IN A ARRAY & CHECK IF THEY ARE PERFECT NUMBER OR NOT IF
IS THEN STORE IN ANOTHER ARRAY
import java.io.*;
public class PERFECT_NUMBER
{
public static void main() throws IOException
{
DataInputStream ob=new DataInputStream(System.in);
int a[] =new int[10];
//TO DECLARE ARRAY A &PROVIDING
SIZE OF 10
int b[] =new int[10];
//TO DECLARE ARRAY B &PROVIDING
SIZE OF 10
int d=0;
for(int i=0;i<10;i++)
//TO INPUT 10 NUMBERS IN ARRAY A
{
System.out.println("input a["+i+"]");
a[i]=Integer.parseInt(ob.readLine());
int sum=0;
for(int j=a[i];j>0;j=j/10)
//TO EXTRACT DIGITS OF NUMBER
{
int r=j%10;
int t=1;
for(int l=1;l<=r;l++)
//TO FIND FACTORIAL OF THE DIGITS
OF THE NUMBER
{
t=t*l;
}
sum=sum+t;
//TO GET SUM OF THE FACTORIAL
if(sum==a[i])
//TO COMPARE THE SUM WITH THE
ORIGINAL NUMBER
{
b[d]=a[i];
//TO GET THE PERFECT NUMBER IN ARRAY
A TO ARRAY B
d++;
}
}
}
for(int k=0;k<d;k++)
//TO PRINT THE PERFECT NUMBER
{
System.out.println("b[" +k+"]"+ b[d]);
}
}
}
//enter a three letter word and find all the permutations of those letters(unrepeated)
import java.io.*;
public class PERMUTATION
{ //CONCEPT:fix one letter of the word and display the rest two letters by
interchanging their places. repeat the same with other two letters.
public static void main() throws IOException
{
DataInputStream ob=new DataInputStream(System.in);
System.out.print("Enter a three letter word ");
String s=ob.readLine();
int index=0;
for(int i=0;i<3;i++)
{
char ch=s.charAt(index);
String r=remaining(s,ch);
String s1=shuffle(r);
System.out.println(ch+r);
System.out.println(ch+s1);
index++;
}
}
public static String remaining(String s,char ch)
letters
{
String r="";
for(int i=0;i<3;i++)
{
char c=s.charAt(i);
if(c!=ch)
r=r+c;
}
return r;
}
public static String shuffle(String r)
remaining letters
{
String k="";
for(int i=0;i<2;i++)
{
char c=r.charAt(i);
k=c+k;
}
return k;
}
}
//Write a program that encodes a word into Piglatin. To translate a word into a
Piglatin word, convert the word into uppercase and then place the first vowel of the
original word as the start of the new word along with the remaining alphabets. The
alphabets present before the vowel being shifted towards the end followed by AY.
import java.io.*;
class Piglatin
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader (new
InputStreamReader(System.in));
System.out.print("Enter any word: ");
String s=br.readLine();
s=s.toUpperCase(); //converting the word into Uppercase
int l=s.length();
int pos=-1;
char ch;
for(int i=0; i<l; i++)
{
ch=s.charAt(i);
if(ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U')
{
pos=i; //storing the index of the first vowel
break;
}
}
if(pos!=-1) //printing piglatin only if vowel exists
{
String a=s.substring(pos); //extracting all alphabets in the word beginning
from the 1st vowel
String b=s.substring(0,pos); //extracting the alphabets present before the
first vowel
String pig=a+b+"AY"; //adding "AY" at the end of the extracted words after
joining them
System.out.println("The Piglatin of the word = "+pig);
}
else
System.out.println("No vowel, hence piglatin not possible");
}
}
//input a number and display the pair of prime numbers from 1 to that number
which sum up to the number
import java.io.*;
if(f==2)
return true;
else
return false;
}
}
//make a multiple function program to input a sentence, store its words in an array
and sort them lengthwise(those with same length are to be sorted lexographically)
import java.util.*;
import java.io.*;
public class pro7
{
String S;
String word[];
String newS;
public void input() throws IOException
//to input the sentence
{
DataInputStream ob=new DataInputStream(System.in);
System.out.print("INPUT S");
S=ob.readLine();
}
p1.Sort();
}
}
if(n%2==0)
d=n/2;
else
d=(n-1)/2;
for(int j=0;j<d;j++)//LOOP FOR CHECKING FOR PSUEDOPODIC SERIES
{
if(a[j]==a[k])
{
s++;
k--;
}
}
if(s==d)
System.out.print("is a psudopodic series");
else
System.out.print("not a psudopodic series");
}
}
import java.io.*;
public class PUSHPOP
{
//create a class rack which can store the name of atmost 20 books from which a
book can be added or removed only from top.create functions to display books
whose name have less than 10 characters, all books, input a book name,remove
book from stack and check if rack is empty or not.
import java.io.*;
public class rack
{
String book[];
//array to store name of books
String name;
//to hold name of book
int limit, top;
//to hold the limit of books and to indicate the topmost book in
the rack
public rack(int nx)
//parametrized constructor
{
limit=nx;
book=new String[nx];
top=-1;
}
public boolean isempty()
//to check if the rack is empty
{
if(top==-1)
return true;
else
return false;
}
public void display()
//to display the name of books which have less than 10
characters
{
for(int i=0;i<=top;i++)
{
int l=book[i].length();
if(l<10)
System.out.println(book[i]);
}
}
public void putbook() throws IOException
//to insert the name of book
{
DataInputStream ob=new DataInputStream(System.in);
if(top>=limit)
System.out.println("rack full");
else
{
System.out.println("enter name");
name=ob.readLine();
book[++top]=name;
}
}
public void removebook()
//to remove a book from rack
{
if(isempty())
System.out.println("rack empty");
else
System.out.println(book[top--]+" removed");
}
public void displayall()
//to display the name of all books in rack
{
for(int i=0;i<=top;i++)
{
System.out.println(book[i]);
}
}
public static void main() throws IOException
{
DataInputStream ob=new DataInputStream(System.in);
int ch=0;
rack ob1=new rack(20);
do
{
System.out.println("1.add book\n2.remove book\n3.display books whose
name is less than 10 characters\n4.display all\n5.exit\nenter choice");
ch=Integer.parseInt(ob.readLine());
switch(ch)
{
case 1:ob1.putbook();
break;
case 2:ob1.removebook();
break;
case 3:ob1.display();
break;
case 4:ob1.displayall();
break;
}
}while(ch!=5);
}
}
//display the details of the volunteers whose first name is entered by the user from
the file volunteer.txt(there can be more than one volunteer of same first name)
import java.io.*;
import java.util.*;
public class read_volunteer
{
public static void main() throws IOException
{
DataInputStream ob=new DataInputStream(System.in);
System.out.println("enter the first name of volunteer");
String nf=ob.readLine();
FileReader f=new FileReader("volunteer.txt");
BufferedReader b=new BufferedReader(f);
String a="";
while((a=b.readLine())!=null)
{
StringTokenizer s=new StringTokenizer(a);
String s1=s.nextToken();
if(s1.equals(nf))
{
System.out.println("name-"+s1+" "+s.nextToken()
+"\naddress-"+s.nextToken()+"\ncontact number-"+s.nextToken());
}
}
}
}
System.out.print("NUMBER OF VOWELS"+s);
}
}
}
return (x*factorial(x-1));
}
}
//input a number and use recusion to display numbers from 1 to that number
import java.io.*;
public class RECURSION_PRINT_1_N
{
static int l;
public static void main() throws IOException
{
DataInputStream ob=new DataInputStream(System.in);
System.out.print("INPUT NUMBER");
int a=Integer.parseInt(ob.readLine());
l=1;
print(a,l);
}
public static void print(int n,int l)//recursion function
{
if(l<=n)
{
System.out.println(l);
print(n,l+1);
}
}
}
ne=" ";
words(s,l-1,ne);
}
}
}
//input a and n and find the sum of series=>a^n + a^(n-2) +a^(n-4) +............
import java.io.*;
public class RECURSION_SUM
{
public static void main() throws IOException
{
DataInputStream ob=new DataInputStream(System.in);
System.out.print("INPUT NUMBERS");
int a=Integer.parseInt(ob.readLine());
int n=Integer.parseInt(ob.readLine());
double sum=sum(a,n);
System.out.print(sum);
}
public static double sum(int x,int d)
{
if(d==1)
return x;
else
return Math.pow(x,d)+sum(x,d-2);
}
}
//input a number and find the sum of its digits using recursion
import java.io.*;
public class RECURSION_SUM_OF_DIGITS_1_N
{
public static void main() throws IOException
{
DataInputStream ob=new DataInputStream(System.in);
System.out.println("ENTER NUMBER");
int n=Integer.parseInt(ob.readLine());
int sum=sum(n);
System.out.print(sum);
}
public static int sum(int a)
{
if(a==1)
return 1;
else
return (a+sum(a-1));
}
}
//input a number x and its maximum power n and find the sum of series-> (x/(2!)) +
((x^2)/(3!)) +((x^3)/(4!)) +...........+ ((x^n)/((n+1)!))
import java.io.*;
public class RECURSION_SUM_OF_SERIES
{
static double sum;
public static void main() throws IOException
{
DataInputStream ob=new DataInputStream(System.in);
System.out.print("INPUT NUMBER");
int a=Integer.parseInt(ob.readLine());
System.out.println("entter maximum power");
int b=Integer.parseInt(ob.readLine());
sum=0.0;
sum(a,b);
}
public static void sum(int x,int n)
//to find sum
{
if(n>0)
{
sum+=Math.pow(x,n)/fact(n+1);
sum(x,n-1);
}
else
System.out.print("SUM OF SERIES="+sum);
}
public static int fact(int c)
//to find factorial,i.e., value in denomenator
{
if(c==1)
return 1;
else
return c*fact(c-1);
}
}
//program to insert and delete items, count number of nodes, inorder, postorder and
preorder traversal and search in a binary tree using recursive functions
import java.io.*;
class treenode
{
treenode left,right;
int data;
treenode(int n)
//parametrized constructor
{
data=n;
left=null;
right=null;
}
void setleft(treenode n)
//to set the left part of the tree
{
left=n;
}
void setright(treenode n)
//to set the right part of the tree
{
right=n;
}
treenode getright()
//to access the right part of the tree
{
return right;
}
treenode getleft()
//to access the left part of the tree
{
return left;
}
int getdata()
//to get the value of the node of the tree
{
return data;
}
}
class binarytree
{
treenode root;
binarytree()
//default constructor
{
root=null;
}
void insert(int data)
{
root=insert(root,data);
}
treenode insert(treenode node,int data)
//to insert a node in the tree
{
if(node==null)
node=new treenode(data);
else
{
if(data<=node.getdata())
node.left=insert(node.left,data);
else
node.right=insert(node.right,data);
}
return node;
}
void delete(int k)
//to delete a value from the tree
{
if(root==null)
System.out.println("empty tree");
else if(search(k))
{
root=delete(root,k);
System.out.println(k+" deleted");
}
else
System.out.println(k+" doesn't exist");
}
treenode delete(treenode root,int k)
//to delete a value from the tree
{
treenode p,p2,n;
if(root.getdata()==k)
{
treenode lt=root.getleft();
treenode rt=root.getright();
if(lt==null && rt==null)
//delete root
return null;
else if(lt==null)
//if left subtree is null
{
p=rt;
return p;
}
else if(rt==null)
//if right subtree is null
{
p=lt;
return p;
}
else
//when it has both subtrees
{
p2=rt;
p=p2;
while(p.getleft()!=null)
{
p=p.getleft();
}
p.setleft(lt);
return p2;
}
}
//to switch between left and right subtrees
if(k<root.getdata())
{
n=delete(root.getleft(),k);
root.setleft(n);
}
else
{
n=delete(root.getright(),k);
root.setright(n);
}
return root;
}
int countnodes()
{
return countnodes(root);
}
int countnodes(treenode r)
//to count the number of nodes in the tree
{
if(r==null)
return 0;
else
{
int l=1;
l=l+countnodes(r.getleft());
l=l+countnodes(r.getright());
return l;
}
}
boolean search(int v)
{
return search(root,v);
}
boolean search(treenode r,int val)
//to search for a value in the tree
{
boolean f=false;
while(r!=null && f!=true)
{
int rval=r.getdata();
if(val<rval)
r=r.getleft();
else if(val>rval)
r=r.getright();
else
{
f=true;
break;
}
f=search(r,val);
}
return f;
}
void inorder()
{
inorder(root);
}
void inorder(treenode r)
//to display the values in the tree in inorder
{
if(r!=null)
{
inorder(r.getleft());
System.out.print(r.getdata()+" ");
inorder(r.getright());
}
}
void preorder()
{
preorder(root);
}
void preorder(treenode r)
//to display the values in the tree in preorder
{
if(r!=null)
{
System.out.print(r.getdata()+" ");
preorder(r.getleft());
preorder(r.getright());
}
}
void postorder()
{
postorder(root);
}
void postorder(treenode r)
//to display the values in the tree in postorder
{
if(r!=null)
{
postorder(r.getleft());
postorder(r.getright());
System.out.print(r.getdata()+" ");
}
}
}
public class recursive_binary_tree
{
static binarytree b=new binarytree();
public static void main() throws IOException
{
DataInputStream ob=new DataInputStream(System.in);
int ch=1;
do
{
//to display the menu and proceed accordingly
System.out.println("1.insert\n2.delete\n3.count number of nodes\n4.inorder
traversal\n5.preorder traversal\n6.postorder traversal\n7.search\n8.exit\nenter
choice");
ch=Integer.parseInt(ob.readLine());
switch(ch)
{
case 1:System.out.println("enter number");
int k=Integer.parseInt(ob.readLine());
b.insert(k);
break;
case 2:System.out.println("enter number to be deleted");
int k2=Integer.parseInt(ob.readLine());
b.delete(k2);
break;
case 3:System.out.println("number of nodes in the tree is
"+b.countnodes());
break;
case 4:System.out.println("inorder traversal");
b.inorder();
System.out.println();
break;
case 5:System.out.println("preorder traversal");
b.preorder();
System.out.println();
break;
case 6:System.out.println("postorder traversal");
b.postorder();
System.out.println();
break;
case 7:System.out.println("enter number to be searched");
int k1=Integer.parseInt(ob.readLine());
boolean a=b.search(k1);
if(a==true)
System.out.println("found");
else
System.out.println("not found");
break;
}
}while(ch<8);
}
}
//program to calculate sum of list elements, count number of nodes in the list and
print the list using recursive functions
import java.io.*;
class node
//implements node
{
int data;
node link;
node()
//default constructor
{
data=0;
link=null;
}
node(int d,node l)
//parametrized constructor
{
data=d;link=l;
}
void setlink(node n)
//stores paased reference in link part of node
{
link=n;
}
node getlink()
//returns the link part of node
{
return link;
}
int getdata()
{
return data;
}
}
class linkedlist
{
node start;
linkedlist()
//default constructor
{
start=null;
}
void insert(int val)
//insert node in the linked list
{
node nptr=new node(val,null);
if(start==null)
start=nptr;
else if(val<=start.getdata())
//to insert node in the beginning
{
nptr.setlink(start);
start=nptr;
}
else
{
node save=start;
node ptr=start.getlink();
int f=0;
while(ptr!=null)
//to insert node in middle
{
if(val>=save.getdata() && val<=ptr.getdata())
{
save.setlink(nptr);
nptr.setlink(ptr);
f=1;
break;
}
else
{
save=ptr;
ptr=ptr.getlink();
}
}
if(f==0)
//to insert in the end of the list
save.setlink(nptr);
}
}
void print_num()
{
print_num(start);
}
void print_num(node head)
//to display numbers in list using recursion
{
if(head!=null)
{
System.out.print(head.getdata()+" ");
print_num(head.getlink());
}
}
int sum_list()
{
return sum_list(start);
}
int sum_list(node head)
//to find the sum of numbers in the list using
recursion
{
if(head==null)
return 0;
else
return head.getdata()+sum_list(head.getlink());
}
int number()
{
return number(start);
}
int number(node head)
//to find the number of nodes using recursion
{
if(head.getlink()==null)
return 1;
else
return 1+number(head.getlink());
}
}
public class recursive_linked_list
{
static linkedlist s=new linkedlist();
public static void main() throws IOException
{
DataInputStream ob=new DataInputStream(System.in);
System.out.println("enter 5 numbers");
for(int i=0;i<5;i++)
{
int n=Integer.parseInt(ob.readLine());
s.insert(n);
}
System.out.println("list is");
s.print_num();
System.out.println("number of nodes in list is "+s.number()+" and their sum is
"+s.sum_list());
}
}
{
public static void main() throws IOException
{
DataInputStream ob=new DataInputStream(System.in);
int a[][]=new int[3][3];//TO DECLARE AN 2D ARRAY
//LOOPS FOR INPUTING NUMBERS IN THE ARRAY
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print("enter number");
a[i][j]=Integer.parseInt(ob.readLine());
}
}
//LOOPS TO FIND SADDLE POINT OF THE ARRAY
int c=0;
for(int i=0;i<3;i++)
{
int small=a[i][0];
for(int j=1;j<3;j++)
{
if(small>a[i][j])
{
small=a[i][j];
c=j;
}
}
int big=0;
for(int k=0;k<3;k++)
{
if(big<a[k][c])
big=a[k][c];
}
if(big==small)
{
System.out.print(big +"
is a saddle point of array");//TO PRINT SADDLE
POINT OF THE ARRAY
break;
}
}
}
}
}
}
p.close();
p1.close();
b.close();
b1.close();
f.close();
f1.close();
}
}
//input a paragraph and display the number of words and vowels in each sentence
import java.io.*;
public class SENTENCE_COUNT_VOWORDS
{
public static void main() throws IOException
{
DataInputStream ob=new DataInputStream(System.in);
System.out.print("INPUT A PARAGRAPH");
String a=ob.readLine();
int l=a.length();
String b="";
String c="";
int w=0;
int v=0;
for(int i=0;i<l;i++)
{
if((a.charAt(i)=='.')||(a.charAt(i)=='?'))
//find where the
sentence ends
{
b+=a.charAt(i);
System.out.println(b);
int n=b.length();
for(int j=0;j<n;j++)
//to count the number of
words and vowel
{
if((b.charAt(j)==' ')||(b.charAt(j)=='.')||(b.charAt(j)=='?'))
{
w++;
for(int k=0;k<c.length();k++)
{
if((c.charAt(k)=='a')||(c.charAt(k)=='e')||(c.charAt(k)=='i')||
(c.charAt(k)=='o')||(c.charAt(k)=='u'))
v++;
}
c="";
}
else
c+=b.charAt(j);
}
b="";
System.out.println("WORDS COUNT IN THE SENTENCE="+w);
w=0;
}
else
{
b+=a.charAt(i);
}
import java.io.*;
public class SENTENCE_PALIND_REVERSE_FUNC
{
public static void main() throws IOException
{
DataInputStream ob=new DataInputStream(System.in);
System.out.print("INPUT SENTENCE");
String a=ob.readLine();
int l=a.length();
String s="";
StringBuffer s1=new StringBuffer();
int e=0;
for(int i=0;i<l;i++)
{
if(a.charAt(i)==' ')
{
s1.append(s);
s1.reverse();
System.out.println(s1);
String t=s1.toString();
if(t.equals(s))
e++;
s="";
t="";
s1.delete(0,s1.length());
}
else
s+=a.charAt(i);
}
System.out.print(e);
}
}
//input a sentence and count the number of words which are palindromes
import java.io.*;
public class SENTENCE_PALINDROME_SIMPLE
{
public static void main() throws IOException
{
DataInputStream ob=new DataInputStream(System.in);
System.out.print("INPUT SENTENCE");
String a=ob.readLine();
int l=a.length();
String s="";
String t="";
int e=0;
for(int i=0;i<l;i++)
{
if(a.charAt(i)==' ')
{
int b=s.length();
for(int j=b-1;j>=0;j--)
//to check if the extracted word is a palindrome
{
t+=s.charAt(j);
}
if((t.equals(s))==true)
//to count the number of palindromes
{
e++;
s="";
t="";
}
}
else
s+=a.charAt(i);
}
System.out.print("NUMBER OF PALIBNDROMES IN SENTENCE="+e);
}
}
{
Node ptr=start;
System.out.print(start.getdata()+"--->");
ptr=start.getlink();
do
{
System.out.print(ptr.getdata()+"--->");
ptr=ptr.getlink();
}while(ptr.getlink()!=null);
System.out.print(ptr.getdata()+"!!!!"); //last node's info printed
System.out.println();
}
}
public class slist1
{
public static void main()throws IOException
{
DataInputStream ob=new DataInputStream(System.in);
linkedlist l=new linkedlist();
for(int i=0;i<5;i++)
{
System.out.print("enter no.");
int p=Integer.parseInt(ob.readLine());
l.insert(p);
}
l.display();
}
}
{
int data;
node link;
public node()
//default constructor
{
data=0;link=null;
}
public node(int d,node n) //parameterised constructor
{
data=d;
link=n;
}
public void setlink(node m) //stores passed reference n in link part of node
{
link=m;
}
public node getlink()
//returns link partof node
{
return link;
}
public void setdata(int v) //stores passed value v in data part of node
{
data=v;
}
public int getdata() //return data part of node
{
return data;
}
}
class linkedlist
{
node start;
public linkedlist()
{
start=null;
}
public void insert(int v) //insert a node in list pointed to by start
{
node np=new node(v,null);
if(start==null)
start=np;
else
{
np.setlink(start);
start=np;}
}
public void display() //to display the node
{
node ptr=start;
System.out.print(start.getdata()+"--->");
ptr=start.getlink();
while(ptr.getlink()!=null)
{
System.out.print(ptr.getdata()+"--->");
ptr=ptr.getlink();
}
System.out.print(ptr.getdata()+"!!!!"); //last node's info printed
System.out.println();
}
}
public class slist
{
public static void main()throws IOException
{
DataInputStream ob=new DataInputStream(System.in);
linkedlist l=new linkedlist();
for(int i=0;i<5;i++)
{
System.out.print("enter no.");
int p=Integer.parseInt(ob.readLine());
l.insert(p);
}
l.display();
}
}
{
System.out.print(a[i][j]+ "
");
}
}
}
}
{
b[d]=a[i][j];
d++;
}
}
}
for(int i=0;i<16;i++)
{
for(int j=0;j<15-i;j++)
{
if(b[j]>b[j+1])
{
int t=b[j];
b[j]=b[j+1];
b[j+1]=t;
}
}
}
for(int j=0;j<16;j++)
{
System.out.println(b[j]);
}
}
}
//input a sentence and arrange the letters of the word by entering the position of
word in circular manner
import java.io.*;
public class Special2
{
String Sentence;
String Word;
String Newsent;
int A;
public Special2()
{
Word="";
Newsent="";
}
public void input() throws IOException
//to input a sentence
{
DataInputStream ob=new DataInputStream(System.in);
System.out.print("Input Sentence");
Sentence= ob.readLine();
Sentence+=" ";
int l=Sentence.length();
int a=1;
for(int i=0;i<l;i++)
{
if(Sentence.charAt(i)==' ')
a++;
}
boolean t=true;
while(t)
{
read();
if(A>a)
//to check if the nmber is valid
{
System.out.print("Sorry! Re-enter the number");
t=true;
}
else
t=false;
}
}
public void read() throws IOException
//to input the position of word
to be changed as a number
{
DataInputStream ob=new DataInputStream(System.in);
System.out.print("Input A");
A=Integer.parseInt(ob.readLine());
}
public void Circular()
//to replace each letter of the chosen
word by the letter that follows it alphabaetically
{
String z="";
int l=Sentence.length();
int a=0;
for(int i=0;i<l;i++)
{
if(Sentence.charAt(i)==' ')
{
a++;
if(a==A)
{
int t=Word.length();
for(int j=0;j<t;j++)
{
char ch=Word.charAt(j);
ch=(char)(ch+1);
if(ch>122)
ch=(char)(ch-26);
z=z+ch;
}
Newsent+=z+" ";
z="";
Word="";
}
else
{ Newsent+=Word+" ";
Word="";
}
}
else
Word+=Sentence.charAt(i);
}
System.out.print(Newsent);
}
public static void main() throws IOException
with replaced word
{
Special2 p1=new Special2();
p1.input();
p1.Circular();
}
}
import java.io.*;
class node
{
}
class linked_stack
{
}
public class STACK_LINKED
{
public static void main() throws IOException
{
DataInputStream ob=new DataInputStream(System.in);
int i;
for(int a=0;a<5;a++)
{
i=Integer.parseInt(ob.readLine());
push(i);
System.out.print("PUSHED ="+i);
}
}
}
//input a sentence and extract the words. display the words sorted
(i)lexicographically (ii)lengthwise
import java.io.*;
import java.util.*;
public class stringlexico
{
public static void main()throws IOException
{
DataInputStream ob=new DataInputStream(System.in);
System.out.print("INPUT SENTENCE");
String sentence=ob.readLine();
StringTokenizer s=new StringTokenizer(sentence);
int k=s.countTokens();
String words[]=new String[k];
for(int i=0;i<k;i++)
{
words[i]=s.nextToken();
}
alphabets(words,k);
len(words,k);
}
public static void alphabets(String alpha[],int range)
//to arrange the
words alphabaetically
{
for(int i=0;i<range;i++)
{
String small=alpha[i];
int pos=i;
for(int j=i+1;j<range;j++)
{
if(alpha[j].compareTo(small)<0)
{
small=alpha[j];
pos=j;
}
}
String temp=alpha[pos];
alpha[pos]=alpha[i];
alpha[i]=temp;
}
System.out.print("alphabatically");
for(int k=0;k<range;k++)
//to display the sorted words
{
System.out.print(alpha[k]+" ");
}
System.out.println("
");
}
//input a sentence and display the number of palindromes using string tokenizer
import java.io.*;
import java.util.*;
public class STRTOKENPALIN
{
public static void main() throws IOException
{
DataInputStream ob=new DataInputStream(System.in);
System.out.print("Enter a string ");
String s=ob.readLine();
StringTokenizer s1=new StringTokenizer(s);
int n=s1.countTokens();
int palin=0;
//to store the number of palindromes
for(int i=0;i<n;i++)
//to count the number of palindromes
{
String word=s1.nextToken();
StringBuffer w=new StringBuffer(word);
w.reverse();
String j=w.toString();
if(j.equals(word))
{
System.out.println(word);
palin++;
}
}
System.out.print(palin);
}
}
for(int j=0;j<3;j++)
{
System.out.print(a[i][j]+"
");
}
System.out.println("SUM="+b[i]);
}
}
}
//write a menu driven program to input admission number and name of students,
display all the values entered, display the corresponding name for an admission
number and delete the admission number and name for the respective admission
number.USE NODES
import java.io.*;
import java.util.Scanner;
class node
{
String name;
int admno;
node link;
public node(int a,String n,node l)
{
admno=a;
name=n;
link=l;
}
public void setlink(node l)
{
link=l;
}
public node getlink()
{
return link;
}
public int geta()
{
return admno;
}
public String getn()
{
return name;
}
}
class linkedlist
{
node start=null;
public void insert(int a,String n)
{
node np=new node(a,n,null);
if(start==null)
{
start=np;
}
else
{
np.setlink(start);
start=np;
}
}
public void display(int a)
{
node ptr=start;
int k=0;
do
{
int a1=ptr.geta();
if(a1==a)
{
System.out.println(ptr.getn());
k++;
break;
}
else
ptr=ptr.getlink();
}while(ptr!=null);
if(k==0)
System.out.println("not found");
}
public void displayall()
{
node nptr=start;
do
{
System.out.println(nptr.geta()+"\t"+nptr.getn());
nptr=nptr.getlink();
}while(nptr.getlink()!=null);
System.out.println(nptr.geta()+"\t"+nptr.getn());
}
public void delete(int a)
{
node n=start;
node n1=n.getlink();
int k=0,count=0;
do
{
int a1=n.geta();
count++;
if(a1==a)
{
if(count==1)
{start=start.getlink();}
else
{n.setlink(n1.getlink());}
k++;
break;
}
else
{
n=n.getlink();
n1=n.getlink();
}
}while(n1!=null);
if(k==0)
System.out.println("not found");
else
System.out.println("deleted");
}
}
public class test
{
static linkedlist l=new linkedlist();
public static void main() throws IOException
{
DataInputStream ob=new DataInputStream(System.in);
int ch=0;
do
{
System.out.println("menu\n1.insert\n2.display\n3.display
all\n4.delete\n5.exit\nenter choice");
ch=Integer.parseInt(ob.readLine());
switch(ch)
{
case 1:System.out.println("enter admission number and name");
l.insert(Integer.parseInt(ob.readLine()),ob.readLine());
break;
case 2:System.out.println("enter admission number");
l.display(Integer.parseInt(ob.readLine()));
break;
case 3:l.displayall();
break;
case 4:System.out.println("enter admission number");
l.delete(Integer.parseInt(ob.readLine()));
break;
}
}while(ch<5);
}
}
//WRITE A PROGRAM TO INPUT TWO NUMBERS m,n & PRINT ALL THE UNIQUE
NUMBERS BETWEEN THEM
import java.io.*;
public class UNIQUE_NUMBER
{
public static void main() throws IOException
{
DataInputStream ob=new DataInputStream(System.in);
//STATEMENTS TO INPUT THE NUMBERS
System.out.print("input 1st number");
int m=Integer.parseInt(ob.readLine());
System.out.print("input 2nd number");
int n=Integer.parseInt(ob.readLine());
int s=0;
for(int i=m;i<=n;i++)//LOOP TO GET SINGLE NUMBER TO CHECK WHETHER
THE NUMBER IS THE UNIQUE NUMBER OR NOT
{
int f=0;//VARIABLE TO STORE THE COUNT OF DIGITS OF THE NUMBER
for(int j=i;j>0;j=j/10)////LOOP TO GET THE NUMBER OF THE DIGITS IN THE
NUMBER
{
f++;
}
int a[]=new int[f];//ARRAY INITIALLISED SO AS TO STORE THE DIGITS
SEPERATELY IN IT
for(int j=i,k=0;j>0;k++,j=j/10)//LOOP TO STORE THE DIGITS SEPERATELY IN
THE ARRAY DECLARED
{
int b=j%10;
a[k]=b;//TO STORE THE DIGIT IN THE ARRAY AT THE INDEX
}
int count=0;
int r=0;
//LOOPS TO CHECK THE NUMBER IS THE UNIQUE NUMBER OR NOT
for(int t=0;t<f;t++)
{
int t1=a[t];
for(int z=0;z<f;z++)
{
if(t1==a[z])//TO CHECK WHETHER THE DIGIT IN THE VARIABLE t1 IS
EQUAL TO THE OTHER DIGITS
count++;//IF THE DIGITS ARE EQUAL IT WILL GIVE AN INCREASE TO
COUNT
}
if(count==1)//IF THERE IS SINGLE OCCURANCE IN THE ARRAY OF THE
DIGIT THEN IT WILL GIVE AN INCREASE TO r
r++;
count=0;
}
if (r==f)//IF THE OCCURANCE OF A NUMBER INT THE ARRAY IS EQUAL TO
THE SIZE OF THE ARRAY THEN IT WILL PRINT THE NUMBER
{
System.out.println(i);//STATEMENT TO PRINT THE NUMBER
s++;//TO STORE THE COUNT OF THE UNIQUE NUMBER BETWEEN m&n
}
}
System.out.print(s);//TO PRINT THE COUNT OF THE UNIQUE NUMBERS
}
}
}
else
{
System.out.print("
");
System.out.println();
}
}
}
}
}
}
{
public static void main() throws IOException
{
DataInputStream ob=new DataInputStream(System.in);
System.out.println("enter length, breadth, height of cuboid");
double a=Double.parseDouble(ob.readLine());
double b=Double.parseDouble(ob.readLine());
double c=Double.parseDouble(ob.readLine());
volume ar=new volume(a,b,c);
ar.dovolume();
ar.show();
}
}
//store first name, last name, address and contact numbers of volunteers in a
campaign in a file volunteer.txt
import java.io.*;
public class volunteer_writer
{
public static void main() throws IOException
{
DataInputStream ob=new DataInputStream(System.in);
//design a super class worker which stores the name and basic pay of worker.
subclass wages calculates monthly wages=basic pay + (overtime hours X rate).
display all the details of worker
import java.io.*;
class worker
//super class
{
String name;
double basic;
worker(String n,double m)
//parametrized constructor
{
name=n;
basic=m;
}
void display()
//to display details of worker
{
System.out.println(name+" has basic pay Rs."+basic);
}
}
public class wages extends worker
//sub class
{
int hrs;
double rate;
double wage;
wages(String n,double b,int h,double r)
//parametrized constructor
{
super(n,b);
hrs=h;
rate=r;
}
double overtime()
//to calculate overtime
{
return (hrs*rate);
}
void display()
//to display the details of worker and his total wage
{
super.display();
System.out.println("wages is Rs."+(super.basic+overtime()));
}
public static void main() throws IOException
//to input details of the
worker and display wage
{
DataInputStream ob=new DataInputStream(System.in);
System.out.println("enter name, basic pay, overtime hours and rate per hour
of the worker");
String n=ob.readLine();
double b=Double.parseDouble(ob.readLine());
int h=Integer.parseInt(ob.readLine());
double r=Double.parseDouble(ob.readLine());
wages ob1=new wages(n,b,h,r);
ob1.display();
}
}
//input a sentence and display the number of digits, letters in uppercase, lowercase
and special characters using string tockenizer
import java.io.*;
import java.util.*;
public class WRAPPERCOUNT_typeofchar
{
public static void main() throws IOException
{
DataInputStream ob=new DataInputStream(System.in);
System.out.print("INPUT SENTENCE");
String a=ob.readLine();
StringTokenizer w=new StringTokenizer(a);
int tokens=w.countTokens();
int d=0;
int u=0;
int l=0;
int sp=0;
for(int i=0;i<tokens;i++)
{
String word=w.nextToken();
for(int j=0;j<word.length();j++)
{
char b=word.charAt(j);
if(Character.isDigit(b))
//to count the number of digits
d++;
else if(Character.isUpperCase(b))
//to count the number of letters in
upper case
u++;
else if(Character.isLowerCase(b))
//to count the number of letters in
lower case
l++;
else
//to count the number of special characters
sp++;
}
}
System.out.print("DIGITS IN THE SENTENCE ARE"+d +"\n");
System.out.print("THE UPPER CASE LETTERS IN THE SENTENCE ARE"+u
+"\n");
System.out.print("THE LOWER CASE LETTERS IN THE SENTENCE ARE"+l
+"\n");
System.out.print("SPECIAL CHARACTERS IN THE SENTENCE ARE"+sp +"\n");
}
}