Sei sulla pagina 1di 84

BIODATA

NAME: Shakher
ROLL NUMBER:016
CLASS : XII
SECTION: B
SUBJECT
COMPUTER :
SCIENCE
ASSIGNMENT
SUBJECT
TEACHER:MR.
MANISH TRIPATHI
SCHOOL:CITY
MONTESSORI
SCHOOL
PREFACE
I have tried my level best to prepare this project by
sheer dedication of time to this creation.In this
project ,I have
depicted various programs based on the basic
concepts of programming such as
Inheritance,Operations on arrays,strings,operations
on numbers,functions and methods,object
passing,and programs based on patterns.

I hope this project of mine will be acknowledged


thoroughly.To ensure that the viewer gets to know
about the programs extensively,I have inserted each
and every bit of information into each program with
sincere effort.
(Shakher trivade)
ACKNOWLEDGEM
ENT
Apart from the efforts of me,the success of any
project depends largely on the encouragement and
guidelines provided by others.I take this opportunity
to express my gratitude to the people who have
been instrumental in the successful completion of
the project.
I would like to show my greatest
appreciation to my computer teacher,Mr.Manish
Tripathi
I cant thank him enough for his guidelines given to
me which propelled the successful completion of the
project in the stipulated time.
--- (Shakher trivade)

INDEX
Serial Category of Programs Number Of Page Number
Numbe Programs
r
1. ARRAYS 10 6-34

2. FUNCTIONS AND 8 36-59


METHODS,OBJECT
PASSING
3. NUMBERS 6 61-72
4. STRINGS 8 74-95
5. INHERITANCE 3 97-108
6. PATTERNS 5 110-120
PROGRAMS
BASED ON
OPERATIONS
ON
ARRAYS
(SINGLE AND
DOUBLE
DIMENSIONAL)
TO ENTER THE TOTAL NO. OF DAYS IN
MONTH & THE FIRST DAY OF THE MONTH AND
PRINT THE CALENDAR OF THE MONTH.
Algorithm:-
STEP 1:Start
STEP 2:Declare an array or 6 rows & 7 columns.
STEP 3:Make a void function input() to input the no of days
in month & first day.
STEP 4:Make funtion void calc()
STEP 5:Make a loop from 0 to 5 in a.
STEP 6:Make a loop from 0 to 6 in b.
STEP 7:If a==0 and b==fd-1,c++.
STEP 8:If c>0 and c<=nd,n[a][b]=c++.
STEP 9:End both loops.
STEP 10:Make the main() function.
STEP 11:Print the names of days in one row.
STEP 12:Print those elements of array that are not 0.
STEP 13:End
import java.util.*;
class Calender
{
Scanner ob=new Scanner(System.in);
int fd,nd,n[][]=new int[6][7];
void input()throws Exception
{
System.out.println("Enter no.of days in the month:");
nd=ob.nextInt();
System.out.println("Enter I day's no.if 1 is for monday:");
fd=ob.nextInt();
}
void calc()
{
int a,b,c=0;
for(a=0;a<6;a++)
{
for(b=0;b<7;b++)
{
if(a==0&&b==fd-1)
c++;
if(c>0&&c<=nd)
n[a][b]=c++;
}
}
}
void main()throws Exception
{
String s[]={"MON","TUE","WED","THU","FRI","SAT","SUN"};
input();
calc();
for(int y=0;y<7;y++)
System.out.print(s[y]+"\t");
System.out.println();
for(int h=0;h<6;h++)
{
for(int t=0;t<7;t++)
{
if(n[h][t]!=0)
System.out.print(n[h][t]);
System.out.print("\t");
}
System.out.println();
}
}

VARIABLE DESCRIPTION:-

NAME Type FUNCTION


A Integer Loop variable
B Integer Loop variable
C Integer Counter
H Integer Loop Variable
T Integer Loop Variable
n[][] Integer Array to store dates
Y Integer Loop variable

INPUT & OUTPUT:-

TO ENTER THE NUMBER OF ROWS & THEN


PRINT THE MATRIX OF THAT
ORDER IN THE FOLLOWING FORM-
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
Algorithm:-
STEP 1:Start
STEP 2:Enter tha no of rows/column 'n' that is odd.
STEP 3:Create a 2-D array a[][] of rows & columns n.
STEP 4:Assign the central element as 0.
STEP 5:Assign ar[0][0]=1.
STEP 6:Declare c=0,r=0,d=1,v=n.
STEP 7:Make a loop to end when central element is not
0(Step 8 to 19).
STEP 8:Make a loop until c<v.
STEP 9:In loop,a[r][c]=++d & c++.
STEP 10:After loop,c-- & r++.
STEP 11:Make a loop until r<v.
STEP 12:In loop,a[r][c]=++d & r++.
STEP 13:After loop,c-- & r--.
STEP 14:Make a loop until c>=n-v.
STEP 15:In loop,a[r][c]=++d & c--.
STEP 16:After loop,c++,v-- & r--.
STEP 17:Make a loop until r>=n-v.
STEP 18:In loop,a[r][c]=++d & r--.
STEP 19:After loop,c++ & r++.
STEP 20:Print the array.
STEP 21:End.

class Spiral
{
public static void main()
{
int n;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("ENTER THE NUMBER");
n=Integer.parseInt(br.readLine());
int a[][]=new int[n][n],c=0,r=0,d=1,v=n;
a[r][c]=1;
a[n/2][n/2]=0;
for(c=1;a[n/2][n/2]==0;c++,r++)
{
System.out.println("IF THE INPUT IS "+n+" THEN OUTPUT IS");
while(c<v)
{
a[r][c]=++d;
c++;
}
c--;r++;
while(r<v)
{
a[r][c]=++d;
r++;
}
c--;r--;
while(c>=n-v)
{
a[r][c]=++d;
c--;
}
v--;c++;r--;
while(r>=n-v)
{
a[r][c]=++d;
r--;
}
}
for(r=0;r<n;r++)
{
for(c=0;c<n;c++)
System.out.print(a[r][c]+"\t");
System.out.println();
}
}
}

VARIABLE DESCRIPTION:-

NAME Type FUNCTION


a[][] Integer Array to store numbers
R Integer To store row no.
C Integer To store column no.
D Integer To store current number
N Integer store array size
V Integer Counter

INPUT & OUTPUT:-


TO ENTER THE TOTAL NO. OF DAYS IN
MONTH & THE FIRST DAY OF THE MONTH AND
PRINT THE CALENDAR OF THE MONTH.
Algorithm:-
STEP 1:Start
STEP 2:Declare an array or 6 rows & 7 columns.
STEP 3:Make a void function input() to input the no of days
in month & first day.
STEP 4:Make funtion void calc()
STEP 5:Make a loop from 0 to 5 in a.
STEP 6:Make a loop from 0 to 6 in b.
STEP 7:If a==0 and b==fd-1,c++.
STEP 8:If c>0 and c<=nd,n[a][b]=c++.
STEP 9:End both loops.
STEP 10:Make the main() function.
STEP 11:Print the names of days in one row.
STEP 12:Print those elements of array that aare not 0.
STEP 13:End
import java.util.*;
class Calender
{
Scanner ob=new Scanner(System.in);
int fd,nd,n[][]=new int[6][7];
void input()throws Exception
{
System.out.println("Enter no.of days in the month:");
nd=ob.nextInt();
System.out.println("Enter I day's no.if 1 is for monday:");
fd=ob.nextInt();
}
void calc()
{
int a,b,c=0;
for(a=0;a<6;a++)
{
for(b=0;b<7;b++)
{
if(a==0&&b==fd-1)
c++;
if(c>0&&c<=nd)
n[a][b]=c++;
}
}
}
void main()throws Exception
{
String s[]={"MON","TUE","WED","THU","FRI","SAT","SUN"};
input();
calc();
for(int y=0;y<7;y++)
System.out.print(s[y]+"\t");
System.out.println();
for(int h=0;h<6;h++)
{
for(int t=0;t<7;t++)
{
if(n[h][t]!=0)
System.out.print(n[h][t]);
System.out.print("\t");
}
System.out.println();
}
}

VARIABLE DESCRIPTION:-

NAME Type FUNCTION


A Integer Loop variable
B Integer Loop variable
C Integer Counter
H Integer Loop Variable
T Integer Loop Variable
n[][] Integer Array to store dates
Y Integer Loop variable

INPUT & OUTPUT:-

DESIGN A CLASS DATE TO HANDLE DATE RELATED


FUNCTIONS I.E FINDING THE FUTURE DATE N DAYS
AFTER CURRENT DATE.
FOR EXAMPLE -:
DATE AFTER 32 DAYS WILL BE 02/02
FINDING THE NUMBER OF DAYS BETWEEN THE
CURRENT AND THE DATE ON WHICH THE PROJECT
ENDS.
YOU MAY ASSUME THAT ALL THE DATE ARE IN
YEAR 2008 AND ARE VALID DATE TO MAKE
CALCULATION EASY EACH
DATE CAN BE CONVERTED TO ITS DATE
NUMBER.
DATE DATE NUMBER
01/01 2
20/02 20
02/02 33
03/03 63
31/12 366

Algorithm:-
STEP 1:Start
STEP 2:Take the values of dates through keyboard.
STEP 3:Calculate the date number of the take date.
STEP 4:Return the value of date number.
STEP 5:Take the values of date number through keyboard.
STEP 6:Return the equivalent date.
STEP 7:Take the days n.
STEP 8:Return future date.
STEP 9:End.
class name -date
Data members :-
dd-to store the date
mm-to store the month
Member methords :-
date(int nd,int nn)-constructer to assing nd to ddand nnto mm.
date_no()-return the date no equivalent to current date object.
date_no_todate(int dx)-return the date equivalent to given date number dx.
futuredate(int m)return future date in 2008 only.
Main function need not to be written*/

class date
{
int dd,mm;
date(int nd,int nn)
{
dd=nd;
mm=nn;
}
int date_no()
{
int s=0;
int a[]={31,29,31,30,31,30,31,31,30,31,30,31};
for(int b=0;b<(mm-1);b++)
{
s=s+a[b];
}
s+=dd;
return s;
}
int date_no_todate(int dx)
{
int b=0;
int a[]={31,29,31,30,31,30,31,31,30,31,30,31};
int s=0;
while(dx>a[b])
{
dx=dx-a[b];
b++;
}
return dx;
}
void futuredate(int m)
{
int dx=date_no();
dx+=m;
int b=0;
int a[]={31,29,31,30,31,30,31,31,30,31,30,31};
int s=0;
while(dx>a[b])
{
dx=dx-a[b];
b++;
}
System.out.println("THE FUTURE DATE AFTER "+m+"DAYS IS"+dx+"/"+(b+1));
}
}
VARIABLE DESCRIPTION:-

NAME TYPE FUNCTION


dd Integer to store the date

Mm Integer to store the month


M Integer to store the date number.

A Integer To store array.


S Integer To return date number.

INPUT/OUTPUT:-

A class Matrix has the following details.


Class name Matrix.
Data members:-
a[][] integer
type array.
m To store
size of
column
dimension
n To store
size of
row
dimension.
Member function:
Matrix(int r,int c)
constructor to assign r to m and c to n and
Create the
integer matrix.
void readMatrix() input the
matrix.
void displayMatrix() displays
the matrix.
void addMatrix(Matrix A,Matrix B) creates a
matrix
which is sum
of Matrix A and
Matrix B.

Matrix subMatrix(Matrix B) find and


returns the substracted matrix by Substracting it
from current Matrix object.
Write a program to specify the class Matrix giving
details of all member function.
main function need to be written.
ALGORITHM:
STEP-1: Start
STEP-2: Initialize class variables with initial values.
STEP-3: To enter the values of matrix through keyboard.
STEP-4: To display the inputed matrix.
STEP-5: To add the two matrices create a function
addMatrix().
STEP-6: To subtract two matrices create a function
subMatrix().
STEP-7: Create a main() method to specify above
functions.
STEP-8: Stop.

import java.io.*;
class Matrix
{
int a[][];
int m;
int n;
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
Matrix(int r,int c)
{
m=r;
n=c;
a=new int[m][n];
}
void readMatrix()throws IOException
{
System.out.println("ENTER THE MATRIX");
for(int c=0;c<m;c++)
for(int b=0;b<m;b++)
{
System.out.println("ENTER THE NUMBER");
String y=br.readLine();
a[c][b]=Integer.parseInt(y);
}
}
void displayMatrix()
{
for(int c=0;c<m;c++)
{
for(int b=0;b<m;b++)
System.out.print(a[c][b]+" ");
System.out.println();
}
}
void addMatrix(Matrix A,Matrix B)
{
int d=m,k=n;
Matrix ob=new Matrix(d,k);
for(int r=0;r<m;r++)
{
for(int c=0;c<n;c++)
{
ob.a[r][c]=A.a[r][c]+B.a[r][c];
}
}
ob.displayMatrix();
}
Matrix subMatrix(Matrix B)
{
int d=m,k=n;
Matrix ob=new Matrix(d,k);
if(n==B.n)
{
for(int r=0;r<m;r++)
{
for(int c=0;c<m;c++)
{
ob.a[r][c]=a[r][c]-B.a[r][c];
}
}
}
else
{
System.out.println("DATA IS INCORRECT");
}
return ob;
}
public void main(int q,int i)throws IOException
{
Matrix obm=new Matrix(q,i);
obm.readMatrix();
Matrix obj=new Matrix(q,i);
obj.readMatrix();
System.out.println("ADDED MATRIX IS");
Matrix obk=new Matrix(q,i);
addMatrix(obm,obj);
System.out.println("SUBSTRACTED MATRIX IS");
Matrix obn=new Matrix(q,i);
obk=subMatrix(obj);
obn.displayMatrix();
}
}

VARIABLE DESCRIPTION:-

NAME TYPE FUNCTION


A Integer To store the array.
M Integer To store size of row dimension.
N Integer To store size of column dimension.
R Integer To store temporary value.
C Integer To store temporary value.

INPUT&OUTPUT:-

WRITE A PROGRAM To DISPLAY THE MAGICAL MATRIX


Algorithm:-
STEP 1: Start.
STEP 2:Enter the size of matrix through n.
STEP 3:Create [n+1][n+1] size of matrix.
STEP 4:Initialize all the positions to 0(zero).
STEP 5:Initialize 1 to the last row and the middle
column of the row.
STEP 6:Increase each row and column by 1.
STEP 7:If row=n then transfer the subscript to 0(zero).
STEP 8:If column=n then transfer the subscript to
0(zero).
STEP 9:If the position is already occupied transfer the
subscript above initial position.
STEP 10:Store the numbers to the appropriate position
and print.
STEP 11:End.

class magic
{
public static void main(int n)
{
int a[][]=new int [n+1][n+1];
for(int x=0;x<(n+1);x++)
{
for(int y=0;y<(n+1);y++)
{
a[x][y]=0;
}
a[n-1][((n+2)/2)-1]=1;
int f=2;
int m=n-1,p=((n+2)/2)-1;
for(;f<=n*n;)
{
m=++m;
p=++p;
if((m==n)&&(p!=n)){
a[0][p]=f;f++;
m=0;
}
else if((m!=n)&&(p==n))
{
a[m][0]=f;
f++;
p=0;
}
else if((m==n)&&(p==n))
{
a[m-2][p-1]=f;f++;
m=m-2;
p=p-1;
}
else if(a[m][p]!=0)
{
a[m-2][p-1]=f;
f++;
m=m-2;
p=p-1;
}
else
{
a[m][p]=f;
f++;
}}
for( x=0;x<(n+1);x++)
{
for(int y=0;y<(n+1);y++)
{
if(a[x][y]!=0)
System.out.print(a[x][y]+" ");
}
System.out.println();
}
}
}
}

VARIABLE DESCRIPTION:-
NAME TYPE FUNCTION
n Int To store the size of matrix.
a[] Int To store the matrix.
x,y Int Loop variable.

INPUT/OUTPUT:-

PROGRAM TO ARRANGE GIVEN ARRAY IN


ODDS AND EVEN ORDER.

Algorithm:-
STEP 1- Start.
STEP 2-Initialise the array i.e. int a[ ]=new int[10].
STEP 3-Now declare the constructor arr( ) and within it
initialize 0 to all the values of array.
STEP 4-Now make another function input( ).
STEP 5-Initialise a loop from 0 to 9 i.e.
for(x=0;x<10;x++).
STEP 6-Within this loop accept the number by the user
and
store number in a[ ].
STEP 7-Now make another function arrange( ).
STEP 8-Within the function initialize a loop from 0 to 9
i.e.
for(x=0;x<10;x++) and initialise temp=0.
STEP 9-Run another loop from 0 to 8 i.e.
for(y=0;y<9;y++) and check if (a[y]%2==0 &&
a[y+1%2!=0).If true then
goto step 10.
STEP 10-Initialise temp=a[y], a[y]=a[y+1] and
a[y+1]=temp
and then close all the loops.
STEP 11-Initialise another loop for(x=0;x<10;x++) and
print
the values of a[x].
STEP 12-End.

import java.io.*;
class arr
{
int x;
int a[]=new int[10];
arr()
{
for(x=0;x<10;x++)
{
a[x]=0;
}
}
void input()throws IOException
{
int x;
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
for(x=0;x<10;x++)
{
System.out.println("Enter any number");
a[x]=Integer.parseInt(br.readLine());
}
}
void arrange()
{
int x,y;
for(x=0;x<01;x++)
{
int temp=0;
for(y=0;y<9;y++)
{
if(a[y]%2==0&&a[y+1]%2!=0)
{
temp=a[y];
a[y]=a[y+1];
a[y+1]=temp;
}
}
}
for(x=0;x<10;x++)
{ System.out.println(a[x]);
}
}
}
VARIABLE DESCRIPTION:-

NAME TYPE FUNCTION

a[ ] Intger Input10 numbers.

x,y Integer Loop variables


Temp Integer Counter variable

INPUT & OUTPUT:-

Program To Calculate The Sum Of The Diagonal


Elements.
Algorithm:-
STEP 1- Store the matrix in variable sq[ ][ ]
STEP 2- Run the for loop i.e. for(int f=0;f<a;f++) and inside this loop
set another for loop i.e. for(c=0;c<a;c++) and print the elements of
the matrix i.e. System.out.print(sq[f][c]+ )
STEP 3- Print the diagonal elements of the matrix by setting the for
loop i.e. for(int c=;c<a;c++).Inside the loop print the elements
System.out.print(sq[c][c]) and compute the sum of these elements
STEP 4- Now in the loop for(x=0;x<a;x++)run another loop
for(y=0;y<a;y++) and Check if(x==y || (x+y)==a-1) then print (sq[x]
[y]+" ") else print(" ") And close the loop.
STEP 5- Print the sum of first diagonal elements and then of second
diagonal elements.
STEP 6- End

import java.io.*;
class diagonalsum
{
public static void main()throws IOException
{
int c=0,sum=0,sum1=0;
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("ENTER THE INDEX FOR A SQUARE MATRIX");
int a=Integer.parseInt(br.readLine());
int [][]sq=new int [a][a];
for(int b=0;b<a;b++)
{
for(c=0;c<a;c++)
{
System.out.println("ENTER AN ELEMENT");
sq[b][c]=Integer.parseInt(br.readLine());
}
}
System.out.println();
for(int f=0;f<a;f++)
{
for(c=0;c<a;c++)
System.out.print(sq[f][c]+" ");
System.out.println();
}
System.out.println();
(for(c=0;c<a;c++)
sum=sum+sq[c][c];
int e=0;
for(c=a-1;c>=0;c--)
{
sum1=sum1+sq[e][c];
e++;
}
int x=0,y=0;
for(x=0;x<a;x++)
{
for(y=0;y<a;y++)
{
if(x==y || (x+y)==a-1)
System.out.print(sq[x][y]+" ");
else
System.out.print(" ");
}
System.out.println(" ");
}
System.out.println();
System.out.println("SUM OF FIRST DIAGONAL "+sum);
System.out.println("SUM OF SECOND DIAGONAL "+sum1);
System.out.println();
int sum2=0;
for(x=0;x<a;x++)
{
System.out.print("SUM OF ELEMENTS OF ROW "+(x+1)+":");
for(y=0;y<a;y++)
sum2=sum2+sq[x][y];
System.out.println(sum2);
sum2=0;
}
}
}

VARIABLE DESCRIPTION:-
NAME TYPE FUNCTION

A Integer Index For Square matrix.

b,c Intege Loop variables


R
x,y Integer Loop variables

E Integer Counter Variable

INPUT & OUTPUT:-

WRITE A PROGRAM TO PRINT CIRCULAR MATRIX


Algorithm:-
STEP 1- Start
STEP 2- Accept the size of the matrix I.e. number of rows and no. of
columns i.e. n
STEP 3- Iitialize an integer type variable r=0,c=-1,l,f,t=1.
STEP 4- Create a matrix a[][] i.e. int a[][]new =int [n][n]
STEP 5- Set a while loop i.e. while(n>0) if true then goto Step6 else
goto 12.
STEP 6- Inside a while loop set a for loop i.e.for(i=1;i<=n;i+=) inside
this loop store the nos. from 1 to n to the row r i.e. a[r][++c] =t++
STEP 7- Set a for loop i.e. for(i=1;i<n;i++) which will store the
numbers from t onwards in the column specified by c i.e. a[++r][c]=t+
+
STEP 8- Set a for loop i.e. for(i=1;i<n;i++) .In this loop no. from t
onwards will be stored in a row specified by c i.e. a[r][--c]=t++
STEP 9- Set a for loop i.e. for(i=1;i<n-1;i++).in this loop no. from t
onwards will be stored in a column specified by variable r i.e a[--r]
[c]=t++
STEP 10- Decrease the value of n i.e. the no. of columns or rows by 2
STEP 11- Goto step5
STEP 12- Print the matrix by setting two for loops
STEP 13- End

import java.io.*;
class Cir_Matrix
{
public static void main(int n)
{
int i,r=0,c=-1,t=1,l=n,j;
int a[][]=new int[n][n];
while(n>0)
{
for(i=1;i<=n;i++)
{
a[r][++c]=t++;
}
for(i=1;i<n;i++)
{
a[++r][c]=t++;
}
for(i=1;i<n;i++)
{
a[r][--c]=t++;
}
for(i=1;i<n-1;i++)
{
a[--r][c]=t++;
}
n=n-2;
}
System.out.println("Circular Matrix for input "+n+" is:-");
for(i=0;i<l;i++)
{
for(j=0;j<l;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
}

VARIABLE DESCRIPTION:-

NAME TYPE FUNCTION


A Integer Array variable
N Integer No of rows and columns of
matrix
i,j Integer For variable purpose
T Integer Counter variable
R Integer Variable representing row
C Integer Variable representing
column

INPUT&OUTPUT:-

A PROGRAM TO CHECK WHETHER THE


ENTERED DATE IS VALID OR NOT. IF THE
DATE IS INVALID THEN PRINT APPROPRIATE
MESSAGES.
Algorithm:-
STEP 1- Start.
STEP 2- Enter day,month and year.
STEP 3- Take an array to store last days of the 12 months.
STEP 4- Use ternary operator to check whether the year is
divisible by 400 and 100 or 4 to check leap year and add 1
int month of February.
STEP 5- Check whether the month is between 1-12
otherwise print
month is invalid.
STEP 6- Check whether the number of days according to
corresponding element of the array.
STEP 7- Check whether the year>0 and if all the conditions
are true then print date is valid.
STEP 8-End.

class Valid_Date
{
public static void main(int dd,int mm,int yy)
{
int k=0,e=0;
int m[]={30,28,31,30,31,30,31,31,30,31,30,31};
k=(yy%400==0)?yy%100==0?1:0:yy%4==0?1:0;
m[k]=m[k]+k;
if(mm<1 || mm>12)
{
e=1;
System.out.println(mm+"is invalid month");
}
if(dd<0 || dd>m[mm-1])
{
e=1;
System.out.println(dd+" is invalid date");
}
if(yy<1)
{
e=1;
System.out.println(yy+" is invalid year");

}
if(e==0)
System.out.println(dd+"-"+mm+"-"+yy+" is valid date");
}
}
VARIABLE DESCRIPTION:-

NAME TYPE FUNCTION

Dd Int To store day

Mm Int To store month

Yy Int To store year

m[] Int An array to store last days of months

K Int To test condition

E Int Counter variable

INPUT/OUTPUT:-

PROGRAMS
BASED
ON
FUNCTIONS ,
METHODS
AND
OBJECT
PASSING
PROGRAM TO CALCULATE THE INCOME TAX.
Algorithm:-
STEP 1-Start.
STEP 2-Enter your name, sex, monthly income,
investment in LIC,investment in NSC, investment in UTI
and other monthly investment in function get data.
STEP 3-calculate the yearly investment.
STEP 4-We have calculated Income Tax according to
given slab in the function income tax.
STEP 5-We have check whether the sex given is female
and if this condition found true then we have calculated
Income tax according to given slab in the function
display.
STEP 6-We have made main function and through the
object we have called the function made above and we
able to calculate income tax.
STEP 7-End.
import java.io.*;
class taxC
{
private String name; private char sex;
private int mi,yi,tax; int lic,uti,nsc,oth;
public void getdata()throws Exception
{
DataInputStream d=new DataInputStream(System.in);
System.out.println(" Please enter your name ");
name=d.readLine();
System.out.println(" Please enter your sex");
sex=d.readLine().charAt(0);
System.out.println(" Please enter your monthly income ");
mi=Integer.parseInt(d.readLine());
System.out.println("Please enter the following for your yearly investments);
System.out.println(" Yearly investment in LIC ");

lic=Integer.parseInt(d.readLine());
System.out.println(" Yearly investment in UTI ");
uti=Integer.parseInt(d.readLine());
System.out.println(" Yearly investment in NSC ");
nsc=Integer.parseInt(d.readLine());
System.out.println(" Other yearly investment ");
oth=Integer.parseInt(d.readLine());
yi=lic+uti+nsc+oth;}
public int incometax()
{
int inc=12*mi-25000,s=0;
if(inc>=75000 && inc<100000)
tax=(int)(.1*(inc-75000));
else if(inc>=100000 && inc<125000)
tax=2500+(int)(.15*(inc-100000));
else if(inc>=125000)
tax=6500+(int)(.18*(inc-125000));
if(sex=='f' || sex=='F')
tax-=5000;
tax-=.1*yi; s=(int)(.1*tax); tax+=s;
if(tax<=0)
tax=0;
return s;
}
public void display(int s)
{
System.out.println("\n\n Name\t\t\t\t:- "+name);
if(sex=='f')
{
System.out.println(" Sex\t\t\t\t:- Female");
}
else
System.out.println(" Sex\t\t\t\t:- Male");
System.out.println(" Monthly income\t\t\t:- "+mi);

System.out.println(" Yearly income \t\t\t:- "+mi*12);


System.out.println(" Standard deduction gives is :- 25000");
System.out.println("\t\t\t\t -------");
System.out.println(" Taxable Income\t\t\t:- "+(mi*12-25000));
System.out.println("\t\t\t\t -------");
System.out.println(" LIC investment\t\t\t:- "+lic);
System.out.println(" UTI investment\t\t\t:- "+uti);
System.out.println(" NSC investment\t\t\t:- "+nsc);
System.out.println(" Other investment\t\t:- "+oth);
System.out.println(" Total investment\t\t:- "+yi);
System.out.println(" Rebate (10% of all investments):- "+(int)(.1*yi));
if(sex=='f' || sex=='F')
System.out.println(" Rebate given to woman is :- 5000 ");
System.out.println(" Surcharge 10% of tax :- "+s);
System.out.println(" Total tax to pay :- "+tax);
}
}
class tax
{
public static void main(String args[])throws Exception
{
System.out.println(" Program to calculate income tax ");
System.out.println(" ------------------------------- ");
taxC ob=new taxC();
ob.getdata();
int s=ob.incometax();
ob.display(s);
}}

VARIABLE DESCRIPTION:-

NAME TYPE FUNCTION


Name String To store the name .
Sex char To store the sex of a person.
Mi int To the monthly income
lic , uti ,nsc ,oth ,yi int To store the LIC , UTI , NSC ,
Other and Total investment
respectively.
s , tax int To store surcharge and tax
respectively.

INPUT & OUTPUT:-

PROGRAM TO PRINT THE PAYROLL OF AN


EMPLOYEE.
Algorithm:-
STEP 1-Start.
STEP 2-Enter the name, post, street, city, pin code,
house number in the function get data.
STEP 3-Enter the basic pay , special pay, contribution
to gpf, contribution to group scheme, monthly income
tax deduction, compensatory allowance.
STEP 4-We have calculated gross pay and net pay
according to given slab in the function pay.
STEP 5-We have print all the above calculated things
and all the above entrances in the function display.
STEP 6-Use inheritance and made another class of
name payroll and create main function and call all the
function through object created in the class payrollC.
STEP 7-End.
import java.io.DataInputStream;
class payrollC
{
String name,post,city,hno,str;
int BP,SP,GPF,GIS,IT,CCA,DA,HRA,pin,GP,NP;
void getdata()throws Exception
{
DataInputStream d=new DataInputStream(System.in);
System.out.println(" Enter the following data for the employee ");
System.out.println(" Name ");
name=d.readLine();
System.out.println(" Designation or post ");
post=d.readLine();
System.out.println(" House number ");
hno=d.readLine();
System.out.println(" Street ");
str=d.readLine();
System.out.println(" City ");
city=d.readLine();
System.out.println(" PIN code ");
pin=Integer.parseInt(d.readLine());
DA=0;
HRA=0;
GP=0;
NP=0;
System.out.println(" Monthly basic pay ");
BP=Integer.parseInt(d.readLine());
System.out.println(" Special pay ");
SP=Integer.parseInt(d.readLine());
System.out.println(" Contribution to GPF ");
GPF=Integer.parseInt(d.readLine());
System.out.println(" Contribution to Group Scheme ");
GIS=Integer.parseInt(d.readLine());
System.out.println(" Monthly Income Tax deduction ");
IT=Integer.parseInt(d.readLine());
System.out.println(" Compensatory Allowence ");
CCA=Integer.parseInt(d.readLine());
}
void pay()
{
int bp=BP;
if(bp<=3500)
DA=114*bp/100;
else if(bp>3500 && bp<=6000)
DA=85*bp/100;
else
DA=74*bp/100;
if(bp<=1500)
HRA=250;
else if(bp>1500 && bp<=2800)
HRA=450;
else if(bp>2800 && bp<=3500)
HRA=900;
else
HRA=1000;
GP=bp+SP+HRA+DA+CCA;
NP=GP-(GPF+GIS+(IT*12));
}
void display()
{
pay();
System.out.println(" Name :- "+name);
System.out.println(" Designation or post :- "+post);
System.out.println(" House number :- "+hno);
System.out.println(" Street :- "+str);
System.out.println(" City :- "+city);
System.out.println(" PIN code :- "+pin);
System.out.println("\n Monthly basic pay :- "+BP);
System.out.println(" Special pay :- "+SP);
System.out.println(" Contribution to GPF :- "+GPF);
System.out.println(" Contribution to Group Scheme :- "+GIS);
System.out.println(" Monthly Income Tax deduction :- "+IT);
System.out.println(" Compensatory Allowence :- "+CCA);
System.out.println(" Total deduction :- "+(GPF+GIS+IT));
System.out.println(" Gross pay :- "+GP);
System.out.println(" Net pay :- "+NP);
}
}
class payroll
{
static void main(String args[])throws Exception
{
System.out.println(" Program to generate the payroll of an employee ");
System.out.println(" ----------------------------------------------- ");
payrollC ob=new payrollC();
ob.getdata();
ob.display();
}}
VARIABLE TYPE FUNCTION
DESCRIPTION:-
NAME

Name , post , city , String To enter the name, post, city, house
hno, str number, street
BP,SP,GIS,GPF,IT,CCA Integer To enter all the pays and
contributions
DA, HRA, GP, NP Integer To calculate the gross pay and net
pay

INPUT &
OUTPUT:-

WRITE A PROGRAM TO calculate THE


NUMBER OF PEOPLE VISITED THE TICKET
BOOTH AND PURCHASED THE TICKET OR
NOT:
CLASS NAME:-ticbooth
INSTANCE VARIABLES:- no_ppl: no.of people
visited.
amt: total amount
collected.
MEMBER METHODS:-
1.void initial()-to assign initial values to
instance variables.
2.void sold()-increament total number of
people visiting, purchasing and amount
collected.
3.void notsold()-increament total number
of people visiting and not purchasing.
4.void disp_totals()-to display no.of people
visiting the booth,purchasing and not
purchasing.
5.void dis_ticket()-to display the total no.of
sold and amount.
Algorithm:-
STEP 1-Start.
STEP 2-Create a method void initial to assign initial value to
instance variable.
STEP 3-Create a method void notsold() to increament total
number of people visited and purchasing.
STEP 4-Create a method void sold() increament total
number of people visiting, purchasing and amount
collected.
STEP 5-Create a method void dis_totals() to display no.of
people visiting the booth,purchasing and not
purchasing.
STEP 6-Create a method void disp_ticket() to display the
total no.of sold and amount.
STEP 7-Stop.
class tickbooth
{
double amt;
int no_ppl;
public void initial()
{
amt=no_ppl=0;
}
public void notsold()
{
no_ppl++;
}
public void sold()
{
no_ppl++;
amt+=2.5;
}
public void disp_totals()
{
int k=(int)(amt/2.5);
System.out.println("The no.of people who purchased the ticket "+k);
System.out.println("The no.of people who not purchased the ticket "+(no_ppl-k));
System.out.println("The no.of people who visited the booth "+no_ppl);
}
public void disp_ticket()
{
int d=(int)(amt/2.5);
System.out.println("The no.of ticket sold "+d);
System.out.println("Amount "+amt);
}
}

VARIABLE DESCRIPTION:-

NAME TYPE FUNCTION


Amt Double To store amount collected.
no_ppl Int To store no.of people visiting.
k,d Int To store no.of people who
purchased ticket.

INPUT&OUTPUT:-
A CLASS TELECALL CALCULATE THE
MONTHLY BILL OF A CONSUMER.

DATA MEMBERS-
phno:phone number.
n:no.of calls made.
name:name of consumer.
amt:bill amount.
MEMBER METHODS-
1.telecall():parameterized constructor.
2.void compute():to calculate the details
3.void dispdata():to display the details
1-100 Rs 500/- rental only
101-200 Rs 1.00/call+R.C
201-300 Rs 1.20/call+R.C
above 300 Rs 1.50/call+R.C
Algorithm:-
STEP 1-Start.
STEP 2-Create a non-parameterized constructor.
STEP 3-Create a method void compute() to calculate the bill.
STEP 4-Create a method void dispdata() to display the
details.
STEP 5-Stop.

class telecall
{
int phno,n;
double amt;
String name;
public telecall(int a,int b,String s)
{
phno=a;
n=b;
name=s;
}
public void compute()
{
double m;
if(n<=100)
m=500;
else if(n>100&&n<=200)
m=500+(n-100)*1;
else if(n>200&&n<=300)
m=1000+100+(n-200)*1.2;
else
m=1120+(n-300)*1.5;
amt=m;
}
void dipdata()
{
System.out.println("Phone number "+phno);
System.out.println(" Name "+name);
System.out.println(" Total Calls "+n);
System.out.println(" Amount "+amt);
}
}

VARIABLE DESCRIPTION:-

NAME TYPE FUNCTION


phno , n Int To store phone number and
no.of calls.
Name String To store the name of consumer.
amt , m Double To store the bill amount.

INPUT&OUTPUT:-
A CLASS CALLED EVEN SERIES HAS BEEN
DEFINED TO FIND THE SMALLEST VALUE OF
INTEGER N SUCH THAT 2+4/2!+8/3!+16/4!+.......
+2^N/N! >=S WHERE 2.0<S<7.0 SOME OF THE
MEMBERS OF CLASS EVEN SERIES ARE GIVEN
BELOW.
CLASS NAME - Evenseries
DATA MEMBERS/INSTANCE VARIABLES-
n - Long integer type to store number of number
of terms
s - float variables where 2.0<s<7.0
k - float variable to store the value of series
evaluated
MEMBER FUNCTION:
Evenseries() : constructor to initialise data
member to 0.
void accept() : To accept value of data member s.
long fact(long x):To compute and return factorial of
x.
void disp():calculates and displays the value of n.
SPECIFY THE CLASS EVENSERIES GIVING THE DETAILS
OF THE CONSTRUCTOR ,Evenseries(),void accept()
long fact(),void disp()
Algorithm:-
STEP 1:Start
STEP 2: Take the values through keyboard.
STEP 3: Calculate factorial value.
STEP 4:Calculate and displays the value of n.
STEP 5:End.

import java.io.*;
class Evenseries
{
long n;
float s,k;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Evenseries()
{
n=0;
}
void accept()throws IOException
{
System.out.println("ENTER THE NUMBER");
String y=br.readLine();
s=Integer.parseInt(y);
}
long fact(long x)
{
long a,f=1;
for(a=1;a<=x;a++)
f*=a;
return f;
}
void disp()
{
while(k<s)
{
n++;
k=(float)Math.pow(2,n)/fact(n)+k;
}
System.out.println("THE SUITABLE VALUE OF n IS "+n);
}
}

VARIABLE DESCRIPTION:-

NAME TYPE FUNCTION


N Long to store the value of integer n for which
the sum of series is to be calculated.
S Float to store sum of series.
K Float float variable to store the value of series
evaluated.
X Integer To calculate the factorial.
A Integer for looping.
F Integer for looping.
INPUT/OUTPUT:-

WRITE A PROGRAM TO INPUT TWO


COMPLEX NUMBERS BY USING THE CONCEPT
OF OBJECT . ADD THESE COMPLEX NUMBERS
AND ALSO MULTIPLY THEM . PROGRAM
SHOULD BE WRITTEN BY USING THE
CONCEPT OF MODULARITY:-
Algorithm:-
STEP 1:Start
STEP 2:Create a constructor which initiases data
members to zero
STEP 3:Create another method which inputs the values
of data members
STEP 4:Create a method which displays the complex
number
STEP 5:Create another method in which complex
numbers(inputed by using the call by reference concept)
are added
STEP 6:Create another method in which complex
numbers are multiplied and a object is returned
STEP 7:In main function,complex numbers are inputed
by using appropriate messages and results are displayed
using
appropriate messages
STEP 8:End

import java.io.*;
class Complex
{
float x,y;
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
Complex()
{
float x=0;
float y=0;
}
void readComplex()throws IOException
{
System.out.println("ENTER THE VALUE OF X");
String t=br.readLine();
x=Float.parseFloat(t);
System.out.println("ENTER THE VALUE OF Y");
t=br.readLine();
y=Float.parseFloat(t);
}
void showComplex()
{
System.out.println("THE COMPLEX NUMBER" +x+"+i"+y);
}
void addComplex(Complex a,Complex b)
{
Complex ob=new Complex();
ob.x=a.x+b.x;
ob.y=a.y+b.y;
ob.showComplex();
}
Complex mulComplex(Complex b)
{
Complex ob=new Complex();
ob.x=(float)(x*b.x-y*b.y);
ob.y=(float)(x*b.y+y*b.x);
return ob;
}
public void main()throws IOException
{
Complex ob=new Complex();
Complex obj=new Complex();
ob.readComplex();
obj.readComplex();
Complex obk=new Complex();
obk.addComplex(ob,obj);
Complex obo=new Complex();
obo=mulComplex(obj);
obo.showComplex();
}
}

VARIABLE DESCRIPTION:-

NAME TYPE FUNCTION


x,y Int stores the real and imaginary part
respectively of complex number
a,b Complex Reference type object
INPUT/OUTPUT:-

DEFINE A CLASS TIME WITH FOLLOWING


SPECIFICATION.
CLASS NAME : TIME
DATA MEMBER/INSTANCE VARIABLES
HR,MIN,SEC : INTEGER
MEMBER METHOD/FUNCTIONS
TIME() : DEFAULT
CONSTRUCTOR
VOID ACCEPT() : ACCEPT A TIME IN
HRS.MINUTES.SECONDS(H/M/S)
TIME ADDTIME(TIME T1) : ADD T1 TIME WITH
CURRENT TIME AND RETURN IT.
TIME DIFFTIME(TIME T2) : RETURN THE
DIFFERENCE T1 TIME WITH CURRENT TIME AND
RETURN IT.
WRITE MAIN() & SHOW WORKING OF TIME CLASS BY
DEFINING ALL THE METHODS & USING OBJECT OF
CLASS TIME.
Algorithm:-
STEP 1: Start
STEP 2: Assign class variables with initial values.
STEP 3: Accept the values of hours,minutes and seconds
through keyboard.
STEP 4: Add the dates one which was entered through
keyboard and the one passed through object.Check whether
the sum of minutes and seconds does not
exceeds 60 if so then increase hour and minute
respectively.
STEP 5: Similarly calculate the difference between the two
dates and print it.
STEP 6: Create a main describing the functions and printing
difference and addition.
STEP 7: End.
import java .io.*;
class Time
{
int hr,min,sec;
Time()
{
hr=0;
min=0;
sec=0;
}
public void accept()throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("ENTER THE TIME");
System.out.println("HOUR");
String s=br.readLine();
hr=Integer.parseInt(s);
System.out.println("MIN");
s=br.readLine();
min=Integer.parseInt(s);
System.out.println("SECOND");
s=br.readLine();
sec=Integer.parseInt(s);
}
public Time addtime(Time t1)
{
Time ob=new Time();
ob.hr=t1.hr+hr;
ob.min=t1.min+min;
ob.sec=t1.sec+sec;
if(ob.sec>=60)
{
ob.sec-=60;
ob.min++;
}
if(ob.min>=60)
{
ob.min-=60;
ob.hr++;
}
return ob;
}
public Time difftime(Time t2)
{
Time ob=new Time();
if(ob.sec<sec)
{
ob.sec+=60;
ob.min--;
ob.sec=ob.sec-sec;
}
else
{
ob.sec=ob.sec-sec;
}
if(ob.min<min)
{
ob.min+=60;
ob.hr--;
ob.min=ob.min-min;
}
else
{
ob.min=ob.min-min;
}
ob.hr=ob.hr-hr;
return ob;
}
public void main()throws IOException
{
Time obj=new Time();
Time oj=new Time();
obj.accept();
oj=addtime(obj);
System.out.println(oj.hr+" "+oj.min+" "+oj.sec);
}
}
VARIABLE DESCRIPTION:-

NAME TYPE FUNCTION


Hr Long to store the value of integer n for
which the sum of series is to be
calculated.
S Float to store sum of series.
K Float float variable to store the value of
series evaluated.
X Integer To calculate the factorial.
A Integer for looping.
F Integer for looping.

INPUT&OUTPUT:-

WRITE A PROGRAM TO COMPUTE THE SUM


OF ANGLES
DATA MEMBERS:
Int deg,min
MEMBER METHODS:
Angle() constructor
void inputAngle( ) to accept the Angle
SumofAngle(Angle,Angle) To compute sum of angle
Algorithm:-
STEP 1- Start.
STEP 2- Create a non-parameterized constructor to
initialized initial value zero to instance variable.
STEP 3- Create a method inputAngle() to accept the Angle
in degree and minutes.
STEP 4- Create a method SumofAngle(Angle,Angle) to
compute the sum of angles in degree and minutes.
STEP 5- Create a main method to illustrate the above
methods.
STEP 6- End.
import java.io.*;
class Angle
{
int deg,min;
public Angle()
{
deg=min=0;
}
public void inputAngle()throws IOException
{
BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));
System.out.println("Enter angle in degree");
deg=Integer.parseInt(br.readLine());
System.out.println("Enter angle in minutes");
min=Integer.parseInt(br.readLine());
}
public Angle SumofAngle(Angle T1,Angle T2)
{
Angle ob=new Angle();
ob.deg=T1.deg+T2.deg;
ob.min=T1.min+T2.min;
if(ob.min>=60)
ob.min=ob.min-60;
ob.deg++;
return ob;
}
public void main()throws IOException
{
Angle ob1=new Angle();
Angle ob2=new Angle();
Angle ob3=new Angle();
ob1.inputAngle();
ob2.inputAngle();
ob3=SumofAngle(ob1,ob2);
System.out.println(ob3.deg+" "+ob3.min);
}
}

VARIABLE DESCRIPTION:-

NAME TYPE FUNCTION


deg , min int Instance variable to store
angle in deg and min
INPUT&OUTPUT:-

PROGRA
MS
BASED
ON
NUMBER
S
WRITE A PROGRAM TO PRINT TWIN PRIME NUMBERS
BETWEEN TO A LIMIT.
Algorithm:-
STEP 1- Start
STEP 2- Store the limit in variable a.
STEP 3- Set for loop i.e. for(k=1;k<=a;k+)
STEP 4- Inside a loop find out the total number of
factors of the limit i.e. a by using the condition if (a
%k==0) if true then goto Step-5.
STEP 5- Increase the counter by 1i.e. c++.
STEP 6- Check whether (c==2) if true then goto Step7
if false then got to Step8
STEP 7- Return true .
STEP 8- Return false.
STEP 9- Make another main method
STEP 10-Accept a lower limit lo and an upper limitu.
upto which the twin numbers are to be printed .
STEP 11- Set a loop i.e. for(b=lo ;b<u;b++)
STEP 12- Inside this loop call the pime functioni.e.
if(prime(b)&&prime(b+2))
STEP 13- If the upper condition is true then print b&
b+2.
STEP 14- End.
import java.io.*;
class Twin_Prime
{
public static boolean prime(int a)
{
int k,c=0;
for(k=1;k<=a;k++)
{
if(a%k==0)
c++;
}
if(c==2)
return true;
else
return false;
public static void main()throws IOException
{
int lo,up;
InputStreamReader i=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(i);
System.out.println("Enter lower limit");
lo=Integer.parseInt(br.readLine());
System.out.println("Enter upper limit");
up=Integer.parseInt(br.readLine());
System.out.println("Twin Prime between "+lo+" to "+up+" are:-");
int b=0;
for(b=lo;b<=(up-2);b++)
{
if(prime(b)&&prime(b+2))
{
System.out.println(b+" "+(b+2));
}
}
System.out.println();
System.out.println();
}
}
VARIABLE DESCRIPTION:-
NAME TYPE FUNCTION
k,b Integer For Loop Purpose
C Integer Counter variable
A Integer Reference variable for limits
Lo Integer Lower limit
Up Integer Upper limit

INPUT & OUTPUT:-

PROGRAM TO INPUT A LIMIT AND PRINT


THE
LUCKY NUMBERS
Algorithm:-
STEP 1-Start
STEP 2-Accept the limit in variable n
STEP 3-Initialise b=2,t=n,g=1,m=2,p=0,hr=0,gt=0.
STEP 4-Declare a double dimension array i.e. int a[]
[]=new int[n/2][n]
STEP 5-Run a for loop from 1 to n i.e.for(x=1;x<=n;x+
+) and store the Values of x in a[0][x-1]
STEP 6-Run a while loop i.e while(b<=t) and initialize
f=b-1,h=0.
STEP 7-Run a for loop from 0 to (t-1) i.e for(int
y=0;y<t;y++) within the Above loop
STEP 8-Check if (y!=f).If the condition is true then
store the value of A[g-1][y] in a[g][h] and increase h by
1 and initialize p=h Otherwise goto step9
STEP 9-Add the values of m in f and close the for
loop
STEP 10-Increase the value of b, g and m by 1 and
initialize t=p and Hr=g-1 and close the while loop.
STEP 11-Run a for loop from 0 to (t-1) i.e.
for(gt=0;gt<t;gt++) and print The values of a[hr][gt].
STEP 12-End.
import java.io.*;
class lucky
{
public void main(int n)
{
int b=2,t=n,g=1,m=2,p=0,hr=0,gt=0;
int a[][]=new int[n/2][n];
for(int x=1;x<=n;x++)
a[0][x-1]=x;
while(b<=t)
{
int f=b-1,h=0;
for(int y=0;y<t;y++)
{
if(y!=f)
{
a[g][h]=a[g-1][y];
h++;
p=h;
}
else
f=f+m;
}
b++;g++;t=p;m++;hr=g-1;}
for(gt=0;gt<t;gt++)
System.out.print(a[hr][gt]+ );
}
}

VARIABLE DESCRIPTION:-

NAME TYPE FUNCTION


b,t,g,m,f,h Integer Counter variable
gt,x,y, Integer Loop variable
a[][] Integer Storing the lucky
nos.
P Integer Storing the no. of
rows
Hr Integer Position of row
where lucky nos.
are stored

INPUT/OUTPUT:-
A PROGRAM TO PRINT FIRST N PRIME NUMBERS.
Algorithm:-
STEP 1- Start.
STEP 2- Enter a number n.
STEP 3- Run a loop to check whether the counter=n or not.
STEP 4- Run a loop to check whether the no. is prime or not.
STEP 5- If the no. divisible only twice then print no. is prime.
STEP 6- Increment counter by 1 and go to Step3.
STEP 7- End.
class Prime
{
public static void main(int n)
{
int a,c=1,l=0,d=0;
System.out.println("The first "+n+" prime numbers are:");
while(l!=n)
{
for(a=1;a<=c;a++)
{
if(c%a= =0)
d++;
}
if(d= =2)
{
System.out.println(c);
l++;
}
d=0;
c++;
}
}
}
VARIABLE DESCRIPTION:-

NAME TYPE FUNCTION


N int To take n numbers
A int Loop variable
C int Counter variable
L int Counter variable
D int It stores the no. of times a no. is divided

INPUT/OUTPUT:-
A PROGRAM TO PRINT NUMERICAL VALUE OF A
ENTERED ROMAN NUMBER.
Algorithm:-
STEP 1-Start.
STEP 2-Accept a String x.
STEP 3-Create a String a to store roman number.
STEP 4-Create a array b[] to values corresponding to roman
number.
STEP 5-Extract each element of x and match it with the
corresponding values a[] and b[].
STEP 6-Do the sum of the corresponding values.
STEP 7-Stop.
class Roman
{
public static void main(String x)
{
String a="IVXLCDM";
int b[]={1,5,10,50,100,500,1000};
int c,d,e=0,f,n=0,i=0;
char ch;
int g[]=new int[100];
for(c=0;c<x.length();c++)
{
ch=x.charAt(c);
for(d=0;d<a.length();d++)
{
if(ch= =a.charAt(d))
{
e=d;
g[c]=b[e];
}
}
}
for(f=0;f<g.length-1;f++)
{
i=g[f];
if(i<g[f+1])
{
g[f]=-g[f];
}
}
for(int p=0;p<a.length();p++)
{
n=n+g[p];
}
System.out.println("The integer value of the roman no. "+x+" is "+n);
}}
VARIABLE DESCRIPTION:-

NAME TYPE FUNCTION


X String To enter a string
A String To store roman nos. in the array
b[] Int To store corresponding values of roman
nos
C Int Loop variable
Ch Char To extract character of string x
E Int Loop variable
F Int Loop variable
N Int To sum up the numbers
I Int To store the element of g[]
g[] Int To evaluate roman nu
D Int Loop variable

INPUT/OUTPUT:-

PROGRAM TO I NPUT A NUMBER AND PRINT


IT IN WORDS.
Algorithm:-
STEP 1-Start
STEP 2-Input the number in amt (Integer Type).
STEP 3-We have taken the Integer variable i.e.z&g.
STEP 4-We have taken the String array i.e.
String x1[]={, ONE, TWO, THREE, FOUR ,
FIVE ,SIX , SEVEN , EIGHT ,NINE}. String
x[]={ ,, TEN, ELEVEN, TWELVE,
THIRTEEN ,FOURTEEN, FIFTEEN, SIXTEEN ,
SEVENTEEN ,EIGHTEEN, NINTEEN};String
x2[]={, TWENTY, THIRTY,FOURTY ,
FIFTY ,SIXTY , SEVENTY , EIGHTY , NINTY}.
STEP 5-We have taken the remainder of inputted
number amt in z and the quotient of the Inputted Step
number in g.
STEP 6- We have check the condition i.e. (g!=1).If this
condition found true then Go to Step7 .
STEP 7-Print the Sting array i.e.(x2[g-1]+ +x1[z]).
STEP 8-Print the String array i.e.(x[amt-9]).
STEP 9-End.
import java.io.*;
class eng
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String x3;
System.out.println("Enter any Number(less than 99)");
int amt=Integer.parseInt(br.readLine());
int a,b,c,y,z,g;
String x[]={"
","Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eight
een","Nineteen"};
String x1[]={" ","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
String x2[]={" ","Twenty","Thirty","Fourty","Fifty","Sixty","Seventy","Eighty","Ninety"};
z=amt%10;
g=amt/10;
if(g!=1)
System.out.println(x2[g-1]+" "+x1[z]);
else
System.out.println(x[amt-9]);
System.out.println("----------*----------*---------- ");
System.out.println("----------*----------*---------- ");
}
}
VARIABLE DESCRIPTION:-

NAME TYPE FUNCTION


Amt Integer To input the number
X1 String To enter a string array
x2 String To print a string array

INPUT&OUTPUT:-

WRITE A PROGRAM TO CHECK WHETHER


NUMBER IS SPECIAL OR NOT.
CLASS NAME: special
DATA MEMBER: int n-to store the number
MEMBER METHODS:
1.special(int n)-parameterized constructor.
2.int factorial(int )-to calculate factorial of a
number.
3.long sum()-to calculate sum of factorial of
each digit of number.
4.void isspecial()-to check whether the number
is special or not.
Algorithm:-
STEP 1-Start.
STEP 2-Create a method int factorial() to calculate factorial
of given number.
STEP 3-Create a method long sum() to calculate the sum of
factorial of each digit.
STEP 4-Create a method void isspecial() to check whether
the number is special or not.
STEP 5-End.
class special
{
int n;
public special(int x)
{
n=x;
}
public int factorial(int x)
{
int f=1;
for(int i=1;i<=n;i++)
f=f*i;
return f;
}
public long sum()
{
int k=n;long s=0;
while(k>0)
{
s=s+factorial(k%10);
k=k/10;
}
return s;
}
public void isspecial()
{
long t=sum();
if((int)t==n)
System.out.println("The number is special");
else
System.out.println("The number is not special");
}
}
VARIABLE DESCRIPTION:-
NAME TYPE FUNCTION

N int To store the number to be checked .


F int To store the factorial of given
number.
S long To store the sum of factorial of each
digit.
i int Loop variable.

INPUT & OUTPUT:-

PROGRAMS
BASED
ON
OPERATIONS
ON
STRINGS

A PROGRAM TO FIND FREQUENCY OF DIGITS


IN A NUMBER AND PRINT SUM OF DIGITS.
Algorithm:-
STEP 1- Start
STEP 2- Enter a number
STEP 3- Take an array and initialize every element with 0
STEP 4- Run a loop until number >0
STEP 5- Take out last digit and increment the digit location
of array by 1
STEP 6- Divide the number by 10 and go to Step4
STEP 7- If the element of array is not =0 then print then
print
frequency of every digit repeat until the length of
array
STEP 8- End
class Frequency
{
public static void main(int num)
{
int oc[]=new int[10];
int b,k=0,d=0,s=0,y,n=y=num;
for(b=0;b<10;b++)
{
oc[b]=0;
}
while(n>0)
{
d=n%10;
oc[d]=oc[d]+1;
n=n/10;
}
for(k=0;k<10;k++)
{
if(oc[k]!=0)
{
System.out.println(k+" occurs "+oc[k]+" times");
}
}
while(y>0)
{
d=y%10;
s=s+d;
y=y/10;
}
System.out.println("Sum of the digits of no. "+num+" is "+s);
}
}

VARIABLE DESCRIPTION:-
NAME TYPE FUNCTION
Num int To enter a number
oc[] int An array to store frequency of digits
B int Loop variable
N int It stores the value of num
D int To store last digit of the number
Y int It stores the value of num
K int To initialize the elements with 0
S int To store sum of digits

INPUT/OUTPUT:-
PROGRAM TO CONVERT THE CASE OF INPUTTED
SENTENCE AND ARRANGE THE WORDS IN
ALPHABETICAL ORDER.
Algorithm:-
STEP 1-Start.
STEP 2-We have made parameterized constructor and
the parameter is String str1and we have assigned str to
str1.
STEP 3-Input the String in the function getdata.
STEP 4-We have convert the case of the String and find
out the length of the String in the function proceed.
STEP 5-We have done type casting and stored the
character in c we have found the character of the string
in ch as well as the next character of the string in ch1.
STEP 6-We have checked the condition i.e. (ch== ||
ch1== c).If this condition found true then go to
Step7 .
STEP 7-We have taken out the character of the string
and check the condition i.e.(ch!= ).If this condition
found true then go to step8.
STEP 8-We have taken the character of the String in
String s.
STEP 9-Apply break.
STEP 10-We have print the arranged string in the
function disp.
STEP 11-Use inheritance and we have made main
function in class alph.
STEP 12-We have called the function made above and
we have arrange the string in alphabetical order.
STEP 13-End.
import java.io.*;
class stream
{
String str;
stream(String str1)
{
str=str1;
}
void getdata()throws Exception
{
DataInputStream d=new DataInputStream(System.in);
System.out.println("enter a String");
str=d.readLine();
}
void proceed()
{
int i,l,j,k;
char ch,c,ch1;
String s= new String(" ");
str=" "+str+" ";
str=str.toUpperCase();
l=str.length();
for(i=65;i<90;i++)
{
c=(char)i;
for(j=0;j<l-1;j++)
{
ch=str.charAt(j);
ch1=str.charAt(j+1);
if(ch==' ' && ch1==c)
{
for(j++;j<l;j++)
{
ch=str.charAt(j);
if(ch!=' ')
{
s=s+ch;
}
else if(ch==' ')
{
s=s+" ";
break;
}
}
}
}
}
str=" ";
str=s;
}
void disp()
{
System.out.println(str);
}
}
class alph
{
public static void main(String args[])throws Exception
{
stream ob=new stream(" ");
ob.getdata();
ob.proceed();
ob.disp();
}
}

VARIABLE DESCRIPTION:-

NAME TYPE FUNCTION


i ,j Integer For Loop
L Integer Length of the string
ch, ch1 Character For storing the character
of the string
str, str1,s String For storing the String

INPUT & OUTPUT:-


Design a Class StringMagic to perform various
operation on strings
Class Name : StringMagic
Data Member
str : to store the string
Mamber functions
void input () : to accept a string
int pailin_count() : count the palindrome Words
in str
String WordsSort() : Sort the str according to
wordsr
length & Alpebatical orde
void display() : Display the orginal & sorted
String
Algorithm:-
STEP 1-Start.
STEP 2-We have input the string i.e. String str.
STEP 3-We have made function int pallin count in
which we have checked whether the inputted string
have pallindrome words and if there are pallindrome
words then we have count the pallindrome words.
STEP 4-We have made function Stringwordssort in
which we have sorted the inputted string.We have done
this process to sort the inputted string- int
l=0,p=0,x=0,y=0,q=0,sp=0,z=0,c=0,d=0,f=0,u=0;
String s1="",t=" ";
str=str+" ";
l=str.length();a
for(y=0;y<l;y++)
{
if(str.charAt(y)==' ')
{
sp++;
}
}
String s2[]=new String[sp];
int a[]=new int[sp];
for(x=0;x<l;x++)
{
if(str.charAt(x)!=' ' && str.charAt(x+1)==' ')
{
s1=str.substring(p,x+1);
s1=s1.trim();
p=x+2;
s2[c]=s1;
a[c]=s1.length();
c++;
}
}
for(d=1;d<str.length();d++)
{
for(f=0;f<sp;f++)
{
if(a[f]==d)
{
t=t+s2[f]+" ";
}
}
}
return t;
}
STEP 5-We have made disp function in order to print
the original string and sorted string.
STEP 6-End.

import java.io.*;
class StringMagic
{
String str;
public void input()throws IOException
{
InputStreamReader i=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(i);
System.out.println("Enter String");
str=br.readLine();
}
public int pailin_count( )
{
int l=0,p=0,x=0,y=0,q=0;
String s1="",s2="";
str=str+" ";
l=str.length();
for(x=0;x<l;x++)
{
if(str.charAt(x)!=' ' && str.charAt(x+1)==' ')
{
s1=str.substring(p,x+1);
s1=s1.trim();
p=x+2;
for(y=(s1.length()-1);y>=0;y--)
{
s2=s2+s1.charAt(y);
}
if(s2.equals(s1))
{
q++;
}
s2="";
}
}
System.out.println("No. of palindrome words are"+" "+q);
return q;
}
public String wordssort( )

{
int l=0,p=0,x=0,y=0,q=0,sp=0,z=0,c=0,d=0,f=0,u=0;
String s1="",t=" ";
str=str+" ";
l=str.length( );
for(y=0;y<l;y++)
{
if(str.charAt(y)==' ')
{
sp++;
}
}
String s2[]=new String[sp];
int a[]=new int[sp];
for(x=0;x<l;x++)
{
if(str.charAt(x)!=' ' && str.charAt(x+1)==' ')
{
s1=str.substring(p,x+1);
s1=s1.trim( );
p=x+2;
s2[c]=s1;
a[c]=s1.length( );
c++;
}
}
for(d=1;d<str.length();d++)
{
for(f=0;f<sp;f++)
{
if(a[f]==d)
{
t=t+s2[f]+" ";
}
}
}
return t;
}
void disp( )
{
System.out.println("the Original String is\n "+str);
System.out.println("the Sorted String is\n "+wordssort());
}
}
VARIABLE DESCRIPTION:-

NAME TYPE FUNCTION


str , s1 , s2 , t String To store the string.
x,d,f int Loop variable.
a[] int To a array

INPUT & OUTPUT:-


WRITE A PROGRAM TO PRINT ALL
POSSIBLE COMBINATION OF A STRING.
Algorithm:-
STEP 1- Start
STEP 2- Accept a string from a user.
STEP 3-Store the string in string variable s.
STEP 4- Find out the length of the string and store it in l i.e.
l=s.length().
STEP 5- Make a new character type array of length li.e. char
ch[]=new char[l].
STEP 6- Set afor loop from 0to l-1 i.e. for(x=0;x<=l-1;x++) and store
each character of string in this array.
STEP 7- Set a for loop i.e. for(x=0;x<l;x++) .In this loop swap each &
every character ofthe array with the first character of the same array
ch[0]=ch[x].
STEP 8- Set another loop from 1 to less than l.inside this loop set
another loop i.e. for(z=1;z<l-1;z++)
STEP 9- Inside the previous loop keep on swaping the adjacent
characters.
STEP 10-After swaping adjacent characters print the whole array using
loop in the previous Loop.
STEP 11-End.

import java.io.*;
class permutation
{
public void main(String s)
{
int pt=1;
System.out.println("INPUT--");
System.out.println(s);
System.out.println("OUTPUT--");
int l=s.length();
char ch[]=new char[l];
char tmp1,tmp2;
for(int z=0;z<l;z++)
ch[z]=s.charAt(z);
for(int x=0;x<l;x++)
{
tmp2=ch[0];
ch[0]=ch[x];
ch[x]=tmp2;
for(int y=1;y<l;y++)
{
for(int z=1;z<l-1;z++)
{
tmp1=ch[z];
ch[z]=ch[z+1];
ch[z+1]=tmp1;
System.out.print(pt+".");
pt++;
for(int p=0;p<l;p++)
{
System.out.print(ch[p]);
}
System.out.println();
}
}
}

VARIABLE DESCRIPTION:-
NAME TYPE FUNCTION
S String Inputted String
Ch Character Char array which stores character
string
L Integer Length of inputted string
x,y,z Integer For loop purpose
Temp Character For exchanging characters

INPUT & OUTPUT:-

WRITE A PROGRAM TO PRINT THE UNION AND


INTERSECTION OF TWO STRINGS
Algorithm:-
STEP 1-Start
STEP 2-int f=0,u=,t=,w=,i=.
STEP 3-Accept the string in the variable a & b.
STEP 4-If there are some tokens left,goto step 5 else
goto step 7.
STEP 5-w=ast.NextToken().
STEP 6-if(t.compareTo(w)==0)then f=f+1.
STEP 7-if f=0 then goto step 8 otherwise goto step 9.
STEP 8-u+ +t.
STEP 9-i=i+ +t.
STEP 10-Display union is u.
STEP 11-Display intersection is i.
STEP 12-End.

import java.util.*;
import java.io.*;
class Union_Inter
{
public static void main() throws IOException
{
String a,b,u="",i="",t="",w="";
int f=0;
InputStreamReader ip=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(ip);
System.out.println("Enter first string");
a=br.readLine();
System.out.println("Enter second string");
b=br.readLine();
u=a;
StringTokenizer bst=new StringTokenizer(b);
while(bst.hasMoreTokens())
{
t=bst.nextToken();
f=0;
StringTokenizer ast=new StringTokenizer(a);
while(ast.hasMoreTokens())
{
w=ast.nextToken();
if(t.compareTo(w)==0)
f=f+1;
}
if(f==0)
u=u+" "+t;
else
i=i+" "+t;
}
System.out.println("UNION-"+u);
System.out.println("INTERSECTION-"+i);
System.out.println();
System.out.println();
}
}

VARIABLE DESCRIPTION:-

NAME FUNCTION
TYPE
A String First string
B String Second string
T String Stores extracted words of second string
W String Stores extracted words of first string
U String Stores union of strings
I String Stores intersection of strings

INPUT/OUTPUT:-
A PROGRAM TO PRINT INITIALS OF A GIVEN OF A
GIVEN NAME.
e.g- Virendra Kumar Singh
V.K. Singh
Algorithm:-
STEP 1- Start.
STEP 2- Enter a String.
STEP 3- Remove spaces from front and back of the string.
STEP 4- Run a loop to extract a character.
STEP 5- Check whether the extracted character is a space
then store first character of the string.
STEP 6- Add the string.
STEP 7- End.
class Initials
{
public static void main(String s)
{
String a="",b="";
s=s.trim();
int n=s.length(),m;
for(m=0;m<n;m++)
{
char c=s.charAt(m);
if(c!=' ')
{
a=a+c;
}
else
{
b=b+a.charAt(0)+".";
a="";
}
}
b=b+a;
System.out.println("The initials of string "+s+" is "+b);
}
}
VARIABLE DESCRIPTION:-

NAME TYPE FUNCTION

S String to enter a string


A String to add total string
B String to store first letter of the string
N int to store length of string
M int loop variable

C char to extract characters

INPUT/OUTPUT:-

Write a program to input a sentence and arrange its words according to increasing length . In
case length of two or more words is same , arrange them in alphabetical order. In case a word is
present more than one in the sentence , remove it from the resultant sentence .

Check your program for the following:

INPUT:THIS IS WHAT HE IS DOING

OUTPUT:HE THIS WHAT DOING

ALGORITHM

1.Input the string and use the split() function to bifurcate each word as a separate array element.

2.Read all the array elements using a for loop.

3.Use Bubble Sort to arrange the sentence in order of increasing length of the words,length of
the words being different.

4.Use compareTo() function to arrange all the words in alphabetical order,if length of two or
more words is same.

6.Extract each array element and calculate its frequencies ,before and after the word ,
respectively.

If these frequencies come out to be zero,then add the words to form a new string.

7.Display the resultant string.

CODE

class string

public static void main(String s)


{

int i,j,c1,c;String s1="";

String a[]=s.split(" ");

for(i=0;i<a.length;i++)

for(j=0;j<a.length-i-1;j++)

if(a[j].length()>a[j+1].length())

s1=a[j];

a[j]=a[j+1];

a[j+1]=s1;

else if((a[j].length())==(a[j+1].length()))

if(a[j].compareTo(a[j+1])>0)

s1=a[j];

a[j]=a[j+1];

a[j+1]=s1;

for(i=0;i<a.length;i++)

{
c=0;c1=0;

for(j=i-1;j>=0;j--)

if(a[i].equals(a[j]))

c++;

for(j=i+1;j<a.length;j++)

if(a[i].equals(a[j]))

c1++;

if(c1==0 && c==0)

System.out.print(a[i]+" ");

VARIABLE DESCRIPTION

VARIABLE DESCRIPTION DATATYPE

a[] Array to atore each word of string int


i Looping variable int
j Looping variable int
c1 To store frequency of a word after the word int
c To store frequency of a word before the word int
s1 To swap two words(temporary variable) String
s To store inputted string String

OUTPUTS

INPUT 1
THIS IS WHAT HE IS DOING

Accept a sentence in upper case which is terminated by either .,?or !.Each word of sentence
is separated by single blank space.Arrange all the letters of the words in alphabetical order ,and
replace all the recurring characters by a *and print it.

ALGORITHM

START

1.Input the string and extract each word from the string.

2.Compare each letter of the word with the alphabet one by one,and store it as a separate word.

3.Store each rearranged word as a new sentence.

4.Replace each recurring letter by an asterisk,and display the resultant sentence.

STOP

CODE:

class String1

public static void main(String s)

String s1="",s2="",s3="",s4="";

s=s+" ";

for(int i=0;i<s.length();i++)

char ch=s.charAt(i);

if(ch!=' ' && ch!='.')

s1=s1+ch;

else

for(char a='A';a<='Z';a++)
{

for(int j=0;j<s1.length();j++)

char ch1=s1.charAt(j);

if(a==ch1)

s2=s2+ch1;

s3=s3+s2+" ";

s2="";

s1="";

System.out.println("String in Alphabetical Order");

System.out.println(s3);

System.out.println("String After Rearrangement");

for(int i=0;i<s3.length()-1;i++)

char a=s3.charAt(i);

System.out.print(a);

while(s3.charAt(i+1)==a)

System.out.print("*");

i++;

}
}

VARIABLE DESCRIPTION

VARIABLE DATATYPE DESCRIPTION


i Int Looping variable
a Char To arrange the letters in alphabetical order
S1 String To store each word of string
S2 String To store each rearranged word of string
S3 String To store each rearranged word in
alphabetical order
S4 String To store the final string
s String To store the inputted string
ch Char To store each character of a word

OUTPUT:

INPUT:MADAM ARORA TEACHES MALAYALAM

PROGRAMS
BASED
ON
THE
CONCEPT
OF
INHERITANCE
THE DISTANCE BETWEEN TWO POINTS(X1,Y1)
AND (X2,Y2) IS GIVEN BY:
((X1-X2)^2+(Y1-Y2)^2)^1/2
TO DETERMINE IF A POINT LIES WITHIN A
GIVEN CIRCLE THE DISTANCE OF THE POINT
FROM THE CENTRE OF THE CIRCLE IS
COMPUTED FIRST AND THEN THIS DISTANCE IS
COMPARED WITH THE RADIUS OF THE CIRCLE.
A CLASS POINT DEFINES THE COORDINATES OF
A POINT WHILE ANOTHER CLASS CIRCLE
REPRESENTS THE RADIUS AND CENTRE OF A
CIRCLE. THE DETAILS -*OF BOTH THE CLASSES
ARE GIVEN BELOW:
CLASS NAME-POINT
DATA MEMBERS-
DOUBLE X, Y-TO STORE THE X AND Y COORDINATES
MEMBER METHODS-
POINT( )-CONSTRUCTOR TO ASSIGN 0 TO X AND Y
VOID READPOINT( )-READS THE COORDINATES OF A
POINT
VOID DISPLAYPOINT( )-DISPLAYS THE COORDINATES
OF A POINT
CLASS NAME-CIRCLE
DATA MEMBERS-
DOUBLE R-TO STORE THE RADIUS
POINT O-TO STORE THE COORDINATES OF THE
CENTRE
MEMBER METHODS-
CIRCLE( )-CONSTRUCTOR
VOID READCIRCLE( )-READS THE RADIUS AND CENTRE
OF A CIRCLE
VOID DISPLAYCIRCLE( )-DISPLAYS THE RADIUS AND
CENTRE OF A CIRCLE
INT CONTAINS(POINT P)-RETURNS 1 IF THE POINT P
LIES WITHIN THE CURRENT CIRCLE OBJECT,
OTHERWISE RETURNS 0
WAP TO SPECIFY THE CLASS POINT AND CIRCLE
GIVING THE DETAILS OF ALL THE ABOVE METHODS.
Algorithm:-
STEP 1:Start
STEP 2:In class Point1,declare double x & y.
STEP 3:In constructor Point1,assign 0 to x & y.
STEP 4:In readPoint1() function,enter the value of x & y.
STEP 5:In displayPoint1(),display x & y.
STEP 6:Make a class Circle that extends Point1.
STEP 7:Declare a Point1 object o and double r.
STEP 8:Make an unparameterized constructor to give r=0 &
call Point1().
STEP 9:Also o=new Point1().
STEP 10:Make a function readCircle() to accept radius of
circle & coordinates.
STEP 11:Make a function displayCircle(),display radius &
coordinates.
STEP 12:Make an int returning function contains() accepting
a Point1 object p.
STEP 13:Calculate the distance between points o and p.
STEP 14:If d<=r,return 1 else 0.
STEP 15:End.
import java.util.*;
class Point1
{
double x,y;
public Point1()
{
x=y=0.0;
}
public void readPoint1()
{
Scanner sc=new Scanner(System.in);
x=sc.nextDouble();
y=sc.nextDouble();
}
public void displayPoint1()
{
System.out.println("The coordinates are :"+x+","+y);
}
}
import java.util.*;
class Circle extends Point1
{
double r;
Point1 o;
Circle()
{
super();
r=0.0;
o=new Point1();
}
public void readCircle()throws Exception
{
Scanner sc1=new Scanner(System.in);
System.out.println("Enter the radius & coordinates of circle");
r=sc1.nextDouble();
o.readPoint1();
}
public void displayCircle()
{
System.out.println("radius= "+r+"& centre of circle = ");
o.displayPoint1();
}
public int Contains(Point1 p)
{
double d=Math.sqrt(Math.pow((o.x-p.x),2)+Math.pow((o.y-p.y),2));
if(d<=r)
return 1;
else
return 0;
}
public void main()throws Exception
{
readCircle();
displayCircle();
Point1 ob1=new Point1();
System.out.println("Enter coordinates of circle");
ob1.readPoint1();
int n=Contains(ob1);
if(n==1)
System.out.println("Point lies");
else
System.out.println("Point not lies");
}
}
VARIABLE DESCRIPTION:-

NAME Type FUNCTION


X Integer x coordinate
Y Integer y coordinate
R Integer Store radius
D Integer Store distance
N Integer Temporary

INPUT & OUTPUT:-

A CLASS ISC SCORES DEFINES THE SCORES OF A


CANDIDATE IN 6 SUBJECTS & ANOTHER CLASS
BEST 4 DEFINES THE BEST FOUR SUBJECTS . THE
DETAILS OF
BOTH CLASSES ARE:
CLASS NAME - ISCSCORES
DATA MEMBERS-
INT N[6][2]- TO STORE MARKS OF SIX SUBJECT
AND CODE(NUMERIC)
MEMBER METHODS-
ISCSCORES( ) - CONSTRUCTOR TO ACCEPT THE
MARKS
INT POINT( ) - TO RETURN THE POINT IN EACH
SUBJECT ACCORDING
TO THE FOLLOWING-
MARKS >= 90 - 1
80-89 - 2
70-79 - 3
60-69 - 4
50-59 - 5
40-49 - 6
BELOW 40 7
CLASS NAME -BEST4
MEMBER METHODS-
VOID BESTSUBJECT( ) - TO DISPLAY THE TOTAL POINTS
& BEST SUBJECT USING THE CONCEPT OF
INHERITANCE.

Algorithm:-
STEP 1:Start
STEP 2:In ISCScores class, declare an array of 6 rows & 2
columns.
STEP 3:In the constructor ISCScores(),accept marks in
subjects & store in I column.
STEP 4:Make an integer returning function Point to accept a
no. x.
STEP 5:Check n[x][0] for the range in which it lies & return
proper grade.
STEP 6:Make a class Best4 extending ISCScores.
STEP 7:In constructor Best4(),call ISCScores().
STEP 8:Make a function BestSubjects().
STEP 9:Using bubble sort,arrange the array in descending
order.
STEP 10:Make a loop from 0 to 5.
STEP 11:For each element,call the function Point() & store
points in II column.
STEP 12:Print the total of I column as total marks.
STEP 13:Print the first 4 rows of II column.
STEP 14:These will be the best 4 points.
STEP 15:End.Step 10:Make the main() function.
STEP 11:Print the names of days in one row.
STEP 12:Print those elements of array that are not 0.
STEP 13:End
import java.util.*;
class ISCScores
{
Scanner ob=new Scanner(System.in);
int n[][]=new int[6][2];
ISCScores()throws Exception
{
for(int y=0;y<6;y++)
{
System.out.println("Enter marks:");
n[y][0]=ob.nextInt();
}
}
int Point(int x)
{
if(n[x][0]>=90)
return 1;
else if(n[x][0]>=80)
return 2;
else if(n[x][0]>=70)
return 3;
else if(n[x][0]>=60)
return 4;
else if(n[x][0]>=50)
return 5;
else if(n[x][0]>=40)
return 6;
else
return 7;
}
}
import java.util.*;
class Best4 extends ISCScores
{
Scanner ob=new Scanner(System.in);
public Best4()throws Exception
{
super();
}
void BestSubjects()
{
int a,b,c,s=0;
for(a=0;a<6;a++)
{
for(b=0;b<5-a;b++)
{
if(n[b][0]<n[b+1][0])
{
c=n[b+1][0];
n[b+1][0]=n[b][0];
n[b][0]=c;
}
}
}
for(a=0;a<6;a++)
{
s+=n[a][0];
n[a][1]=Point(a);
}
System.out.println("Total marks="+s);
System.out.println("Top 4 points are:");
for(a=0;a<4;a++)
System.out.println(n[a][1]);
}
}

VARIABLE DESCRIPTION:-

NAME Type FUNCTION


n[] Integer Array to store marks
Y Integer Loop variable
X Integer Counter
A Integer Loop Variable
B Integer Loop Variable
C Integer Temporary Variable
S Integer Store sum
INPUT & OUTPUT:-
CLASS D2 point DEFINES THE CO-
ORDINATES OF POINT IN A PLANE . WHILE
ANOTHER CLASS D3 point DEFINES CO-
ORDINATES IN A SPACE:
DETAILS OF BOTH CLASSES ARE GIVEN
BELOW:
CLASS NAME: D2point
DATA MEMBERS:
Double x , y to store the x and y co-ordinates.
MEMBER METHODS:
1.D2point()-constructor to assign zero to x and
y.
2.D2point(double nx , double ny)- constructor
to assign nx and ny to x and y respectively.
3.double Distance2d(D2point b)-to return the
distance between point b.
CLASS NAME: D3point
DATA MEMBERS:double z.
MEMBER METHODS:
1.D3point()-constructor to assign zero to z.
2.D3point(double nz)- constructor to assign nz
to z.
3.double Distance3d(D3point b)- to return the
distance between point b in space.
Algorithm:-
STEP 1-Start.
STEP 2-Create member methods of class D2point.Like
D2point(),D2point(double , double ) and double Distance2d()
: to store the distance between point b in a plane.
STEP 3-Create member methods of class D3point. .Like
D3point(),D3point(double , double ) and double Distance3d()
: to store the distance between point b in a space.
STEP 4-Create inheritance between both classes.
STEP 5-Specify the methods in the main method.
STEP 6-End.

class D2point
{
double x,y;
public D2point()
{
x=0;
y=0;
}
public D2point(double a,double b)
{
x=a;
y=b;
}
public double Distance2d(D2point b)
{
double s=0.000;
double result=0.000;
s=(Math.pow((b.x-x),2)+Math.pow((b.y-y),2));
result=Math.sqrt(s);
return result;
}
}

class D3point extends D2point


{
double z;
public D3point()
{
z=0;
}
public D3point(double a,double b,double c)
{
super(a,b);
z=c;
}
public double Distance3d(D3point b)
{
double s,result;
s=Math.pow((b.x-x),2)+Math.pow((b.y-y),2)+Math.pow((b.z-z),2);
result=Math.sqrt(s);
return result;
}
public void main(double a,double b,double c)
{
double s1,s2;
D2point b1=new D2point(a,b);
D3point b2=new D3point(a,b,c);
s1=Distance2d(b1);
s2=Distance3d(b2);
System.out.println(" Distance in plane "+s1);
System.out.println(" Distance in space "+s2);
}
}

VARIABLE DESCRIPTION:

NAME TYPE FUNCTION


x , y ,z double Instance variable to store the co-ordinates.
s , s1 , s2 Double To store the square of distance between the points.
Result Double To store the distance between the points.

INPUT & OUTPUT:-

PROGRAM
S
BASED
ON
PATTERNS
A PROGRAM TO PRINT FOLLOWING PATTERN:-
MADAM
A A
D D
A A
MADAM
Algorithm:-
STEP 1- Start
STEP 2- Enter the String
STEP 3- If counter=0 or it is equal to length-1 then print the
string
STEP 4- Print the character of the string and print spaces
until
length-2
STEP 5- Repeat Step4 until the length-1 of the string
STEP 6- If counter =length then go to Step3
STEP 7- End
class Pattern
{
public static void main(String s)
{
int l=s.length(),e=0,a,x;
for(a=0;a<l;a++)
{
if(e= =0||e= =l-1)
System.out.print(s);
else
{
System.out.print(s.charAt(a));
for(x=0;x<l-2;x++)
{
System.out.print(" ");
}
System.out.print(s.charAt(a));
}
e++;
System.out.println();
}
}
}

VARIABLE DESCRIPTION:-
NAME TYPE FUNCTION
S String To enter String
L Int To store length of string
E Int Counter variable
X Int To run loop to print spaces
A Int To run loop to print string

INPUT/OUTPUT:-

A PROGRAM TO PRINT THE FOLLOWING


PATTERN:-
A A
AB BA
ABC CBA
ABCD DCBA
ABCDEDCBA
Algorithm:-
STEP 1- Start.
STEP 2- Enter the character.
STEP 3- Find the length till where the pattern is to be
printed.
STEP 4- Run a loop until the length.
STEP 5- Run another loop until Step4 and print character.
STEP 6- Print spaces and reduce counter by 2.
STEP 7- Run another loop and print character until A.
STEP 8-Change line.
STEP 9-End.
class Series
{
public static void main(char c)
{
int a,b,f,d,sp,n;
char ch,ch1,ch2;
f=(int)c;
n=f-64;
ch1=ch2='A';
sp=(n-1)+(n-2);
for(a=1;a<=n;a++)
{
for(b=1;b<=a;b++)
{
System.out.print(ch1);
ch1++;
}
for(d=sp;d>=1;d--)
{
System.out.print(" ");
}
sp=sp-2;
for(ch=ch2;ch>='A';ch--)
{
System.out.print(ch);
}
ch2++;
if(ch2==c)
{
ch2--;
}
ch1='A';
System.out.println();
}
}
}

VARIABLE DESCRIPTION:-

NAME TYPE FUNCTION


C Char To enter a character
Ch Char To print character
ch1 Char To store A
ch2 Char Counter variable
A Int To run loop to print character
B Int Counter variable
F Int To store ASCII value
D Int To run loop to print spaces
Sp Int Counter variable
N Int To store length

INPUT/OUTPUT:-

A PROGRAM TO PRINT PASCALS TRIANGLE TILL


N NUMBERS:-
EXAMPLE:-
1
1 1
1 21
1 331
1 4641
Algorithm:-
STEP 1- Start
STEP 2- Take an array of n+1 locations
STEP 3- Run a loop till n
STEP 4- Run another loop till previous loop and print the
element of array with one space
STEP 5- Change the line
STEP 6- Run a loop to add the current and previous element
until 0
STEP 7- End
class Pascal
{
public static void main(int n)
{
int pas[]=new int[n+1];
pas[0]=1;
for(int i=0;i<n;i++)
{
for(int j=0;j<=i;j++)
{
System.out.print(pas[j]+" ");
}
System.out.println();
for(int j=i+1;j>0;j--)
{
pas[j]=pas[j]+pas[j-1];
}
}}}
VARIABLE DESCRIPTION:-

NAME TYPE FUNCTION

N Int to enter no. of lines

pas[] Int array to calculate nos.

I Int loop variable

J Int loop variable

INPUT/OUTPUT:-
A PROGRAM TO PRINT FOLLOWING PATTERN:-
a
aa
aaa
aaaa
aaa
aa
a
Algorithm:-
STEP 1- Start.
STEP 2- Enter a number and a character.
STEP 3- Run a loop till n.
STEP 4- Run another loop until n-1 and print spaces.
STEP 5- Run another loop until Step3 and print character
with space.
STEP 6- Change the line.
STEP 7- Run reverse loop from n-1 to 1.
STEP 8- Run another loop till n-1 and print spaces.
STEP 9- Run a loop till Step8 and print character with
spaces.
STEP 10- Change the line and repeat Step3 till n.
STEP 11- End.
class Pattern2
{
public static void main(char e,int n)
{
int c=n-1,b,d,a;
for(a=1;a<=n;a++)
{
for(b=1;b<=c;b++)
System.out.print(" ");
for(d=1;d<=a;d++)
System.out.print(e+" ");
c--;
System.out.println();
}
c=c+2;
for(a=n-1;a>=1;a--)
{
for(b=1;b<=c;b++)
System.out.print(" ");
for(d=1;d<=a;d++)
System.out.print(e+" ");
c++;
System.out.println();
}
}
}
VARIABLE DESCRIPTION:-

NAME TYPE FUNCTION


E Char to enter a character
N Int to enter a number
C int to store n-1
B Int loop variable
D Int loop variable
A Int loop variable

INPUT/OUTPUT:-

Define a class 'AP Series' with following


specification
Class Name : APSeries
Data Member :
a - To store first term
d - To store common difference
Member Function :
APSeries() - To initialize value of a and
d with 0
APSeries(double a,double d) - To initialize value
of a and d with parametric a and d.
nTHTerm(int n) - To return nTH term of
series
Sum(int n) - To return sum of series till
n terms
showSeries(int n) - To display n terms of
series with sum
Algorithm:-
STEP 1- Start.
STEP 2-We have initialize double type variable i.e. a
and d
STEP 3-We have made non parameterized constructor
by which we haveassign 0 to aandd.
STEP 4- We have made a parameterized constructor
.parameters are double a, double d. We have assign a
to this. a i.e. object class and d to this .d
STEP 5-We have made a return type function i.e.
nTHTerm. Parameter is int n.We have return the nth
term of A.P series in this function
STEP 6- We have made another return type function
i.e. Sum and int n is the parameter. In this function we
have return the sum of A.P series
STEP 7- We have made a function i.e. Show Series and
parameter is int n(Integer Type). In this we have print
the nth term of series as well as Sum of AP series.
STEP 8- We have made main function of this class and
print the A.P. series of required term.
STEP 9-End
import java.io.*;
class APSeries
{private double a,d;
APSeries()
{a = d = 0;
}
APSeries(double a,double d)
{
this.a = a;
this.d = d;
}
double nTHTerm(int n)
{
return (a+(n-1)*d);
}
double Sum(int n)
{
return (n*(a+nTHTerm(n))/2);
}
void showSeries(int n)
{
System.out.println("FIRST TERM OF THE SERIES IS "+a);
System.out.println("COMMON DIFFERENCE OF THE SERIES IS "+d);
System.out.println("Hence the series is ");
for(int i=1;i<=n;i++)
{
System.out.println(nTHTerm(i)+" ");
}
System.out.println("It's Sum will be : "+Sum(n));
System.out.println("------------------------*-------------------------*----------------------");
}
}
class testap
{
public static void main(String args[]) throws Exception
{
APSeries a = new APSeries(1,2);
a.showSeries(5);
}
}

VARIABLE DESCRIPTION:-
NAME TYPE FUNCTION
A int To store first term of AP Series.
D int To store the common difference
of AP Series.
Sum double To store sum of AP Series.
I int Loop variable.
INPUT & OUTPUT:-

Potrebbero piacerti anche