0% found this document useful (0 votes)
10 views67 pages

Program Final

The document contains multiple Java programs demonstrating various programming concepts, including searching algorithms, pattern printing, tax calculation, number classification (Duck, Spy, Happy), string manipulation, and function overloading. Each program is accompanied by a description of variable types and purposes, as well as sample outputs. The programs cover a wide range of topics suitable for beginners learning Java programming.

Uploaded by

rakshitaashahi21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views67 pages

Program Final

The document contains multiple Java programs demonstrating various programming concepts, including searching algorithms, pattern printing, tax calculation, number classification (Duck, Spy, Happy), string manipulation, and function overloading. Each program is accompanied by a description of variable types and purposes, as well as sample outputs. The programs cover a wide range of topics suitable for beginners learning Java programming.

Uploaded by

rakshitaashahi21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PROGRAM-1

WAP to input an array of size 10 in ascending order and search for a


value using Binary search technique.
import [Link].*;

class citynames

public static void main(String args[])

String u[ ]=new String [5];

int r=0,t,y;

Scanner sc= new Scanner([Link]);

[Link]("Enter the names of 5 cities");

for(t=0;t<5;t++)

u[t]=[Link]();

[Link]("Enter the city name to be search");

String b=[Link]();

for(y=0;y<=4;y++)

if(u[y].equals(b))

{
r=1;

break;

if(r==1)

[Link]("City name found at position="+(y+1));

else

[Link]("no such city name is found");

OUTPUT-
VARIABLE DESCRIPTION TABLE

TYPE PURPOSE
NAME
u string Used to declare an array of
size 10.
t int Control Variable used in for
loop to input the names of
cities.
b int Used to store the word to be
search.
y string Control variable used in the
for loop to search the names
of cities .
PROGRAM 2

WAP to input an array of size 10 in ascending order and search for a


value using Binary search technique.

import [Link].*;

class binary_search

public static void main(String args[])

int e[ ]=new int[5];

int g=0,l,p,j,d,mi;

Scanner sc= new Scanner([Link]);

[Link]("Enter the vlaues in ascending order");

for(l=0;l<5;l++)

e[l]=[Link]();

[Link]("Enter the elementto be search");

p=[Link]();

j=0;

d=4;

while(l<=d)
{

mi=(l+d)/2;

if(e[mi]==p)

g=1;

[Link]("Element found at position="+(mi+1));

break;

else if(e[mi]<p)

l=mi+1;

else if(e[mi]>p)

d=mi-1;

if(l>d)

[Link]("No such Element name is found"); }

OUTPUT
VARIABLE DESCRIPTION TABLE

NAME TYPE PURPOSE


e int To store value in an array
l int To use as a counter in the for
loop
j int To store the lower limit
d int To store the upper limit.
mi int To store middle index
p int To store searching value
PROGRAM-3

WAP to print the following pattern –

23

456

7 8 9 10
11 12 13 14 15

class nested_loop

public static void main()

int k,l,b=1;

for(k=1;k<=5;k++ )

for(l=1;l<=k;l++)

[Link](b+" ");

b++;

[Link]( );
}

OUTPUT

VDT

NAME TYPE PURPOSE


k int To use as a control
variable in for loop anto
print the following
pattern.
l int To use as a control
variable in for loop anto
print the following
pattern.
b int Use to print the value
PROGRAM-4

Wap to print the following pattern:

AB

ABC

ABCD

ABCDE
class nested_loop_13

public static void main()

int c=10,h;

char l,t;

for(l='A' ;l <='E';l++)

for(h=1;h<=c;h++)

[Link](" ");

c--;

for(t='A';t<=h;t++)

{
[Link](t);

[Link]( );

VARIABLE DESCRIPTION TABLE

NAME TYPE PURPOSE


H int To use as a control
variable in for loop to
print the following
pattern
I int To use as a control
variable in for loop to
print the following
pattern.
T int To use as a control
variable in for loop to
print the following
pattern.
PROGRAM-5

WAP to input the age, gender (and Taxable Income of person male or
female) and Taxable Income of a [Link] the age more than 65 years
or the gender is female, display “wrong category”. If the age is less
than or equal to 65 years and the gender is male

class Tax

public static void main()

Scanner sc= new Scanner([Link]);

double itt=0,tii;

int ag;

char gen;

[Link]("Enter age ");

ag=[Link]();

[Link]("Enter gender as m or f");


gen=[Link]().charAt(0);

[Link]("Enter total income of the person");

tii=[Link]();

if(ag<=65 && gen=='m')

if(tii<=160000)

itt=0;

else if(tii>160000 && tii<=500000)

itt=(tii-160000)*0.1;

else if(tii>500000 && tii<=800000)

itt=((tii-500000)*0.2)+34000;

else if(tii>800000)

itt=((tii-800000)*0.3)+94000;

[Link]("INCOME TAX="+itt);

else

[Link]("Wrong category");

}
OUTPUT

VARIABLE DESCRIPTION TABLE

NAME TYPE PURPOSE


ag int To input age by the user.
gen int To input gender by the
user.
tii int To input total income by
the user
itt int To calculate total income
tax.
PROGRAM-6

WAP to input three number and print them in ascending order (using
else-if ladder)
import [Link].*;

class abc

public static void main()

Scanner sc= new Scanner([Link]);

int e,f,g;

[Link]("Enter the three values");

e=[Link]();

f=[Link]();

g=[Link]();

if(e>f && e>g)

if(f>g)

[Link](g+","+f+","+e);

else

[Link](e+","+f+","+e);

else if
(f>e && f>g)

if(e>g)

[Link](g+","+e+","+f);

else [Link](e+","+g+","+f);

else if(g>e && g>f)

if(e>f) [Link](f+","+e+","+g);

else [Link](e+","+f+","+g);

}}}

OUTPUT
VARIABLE DESCRIPTION TABLE

NAME TYPE PURPOSE


e int To input the value from
the user.
f int To input the value from
the user.
g int To input the value from
the user.
PROGRAM-7

WAP in Java to input a number and check whether it is a Duck


Number or not.
public class duck_number

public static void main(String args[])

Scanner sc=new Scanner([Link]);

int a,b=0,d;

[Link]("Enter your number to be checked.");

d=[Link]();

while(d!=0)

a=d%10;

d=d/10;

if(a==0)

b++;

if(b>0)

[Link]("It is a duck number.");

else

[Link]("It is not a duck number.");


}

OUTPUT-

VARIABLE DESCRIPTION TABLE-

NAME TYPE PURPOSE

a int To take input of user


b int To store the extract
digit
d int As a counter for how
many times a
condition is met.
PROGRAM-8

WAP to take a number from the user and check and print weather
the given number is Spy number or Not.
lass spy_num

public static void main( )

Scanner sc= new Scanner([Link]);

int g,j=0,x=1,h;

[Link]("Enter the value");

h=[Link]();

while(h!=0)

g=h%10;

h=x/10;

j=j+g;

x=x*g;

if(j==x)

[Link]("Spy Number.");

else

[Link]("Not a Spy Number.");


}

OUTPUT

VARIABLE DESCRIPTION TABLE

NAME TYPE PURPOSE


j int To find the sum of the
digits.
x int To find the product of the
digits
g int To remove the digits from
the inputed number.
h int To input the number from
the user.
PROGARM-9

WAP to input a number and print whether it is a HAPPY numberor


not.
import [Link].*;

class Happy

public static void main(String args[])

int n;

Scanner sc=new Scanner([Link]);

[Link]("Enter a positive integer");

n=[Link]();

int di,s=0,a=n;

while(n>9)

while(n>0)

di=n%10;

s=s+(di*di);

n=n/10;

}
n=s;

s=0; }

if(n==1)

[Link](a+"is a HAPPY number");

else

[Link](a+"is not a HAPPY number");

}}}

OUTPUT

VARIABLE DESCRIPTION TABLE

NAME TYPE PURPOSE


n int Stores the number to be
entered by the user.
di int Stores the digits of the
number.
s int Finds the sum of the
squares of the numbers.
PROGRAM-10

WAP to input a string and check whether a string is starting with a


vowel and ending with consonant or not.
import [Link].*;

class abcd

public static void main()

Scanner sc = new Scanner([Link]);

int ll;

char c,chh;

[Link]("enter the stirng ");

String d= [Link]();

String s1="AEIOUaeiou";

ll=[Link]();

c=[Link](0);

c=[Link](ll-1);

if([Link](c)>=0 && [Link](c)==-1)

[Link]("String starting with vowel and ending with consonant");

Else

[Link]("NOT starting with vowel and ending with consonant.");

}
}

VARIABLE DESCRIPTION TABLE

NAME TYPE PURPOSE


d string To store the string
inputted by the user.
s string To store the vowels of
both cases.
ll int To store the vowels of
both cases.
c char To find whether the string
starts with a vowel or
not.
chh char To find whether the string
ends with a consonant or
not.
PROGRAM-11

WAP to input a string and remove duplicate characters from the


string.
import [Link].*;

class abcde

public static void main()

Scanner sc = new Scanner([Link]);

int ii,ll;

char c;

String s1,s3="";

[Link]("Enter the string");

s1=[Link]();

ll= [Link]();

for(ii=0;ii<=ll-1;ii++)

c=[Link](ii);

if([Link](c)==-1)

s3=s3+c;

[Link](s3);
}

OUTPUT

VDT

NAME TYPE PURPOSE


ii int To use as a counter in the
for loop.
ll int To find the length of the
string.
s1 String To input the string from
the user
c char To find out the duplicate
of the character.
PROGRAM-12

WAP to print the following series:- S=x1 /1! + x2 /2! + x 3 /3!..............


n terms.
import [Link].*;

class series

public static void main( )

Scanner sc= new Scanner([Link]);

int ii,di,si=0,fi=1,xi,num;

[Link]("enter the value of the x and N");

xi=[Link]();

num=[Link]();

for(ii=1;ii<=num;ii++)

fi=fi*ii;

di=(int) [Link](xi,ii)/fi;

si=si+di;

[Link]("The sum of the series is="+si);

}
OUTPUT-

VDT-

NAME TYPE PURPOSE


xi int To input the value from
the user.
ni int To input the value from
the user.
ii int To use as a counter in the
for loop.
di int To find the value
according to the series.
si int To add all values.
fi int To use as a variable in the
dividing part of the string.
PROGRAM-13

WAP to print the following series:- 1,4,5,10,19…………………..n terms.


import [Link].*;

class abce

public static void main()

Scanner sc= new Scanner([Link]);

int ii,ai=1,bi=4,ci=5,dd,num;

[Link]("Enter the limit");

num=[Link]();

[Link](ai+","+bi+","+ci);

for(ii=1;ii<=num-3;ii++)

dd=ai+bi+ci;

[Link](","+dd);

ai=bi;

bi=ci;

ci=dd;

}
OUTPUT

VARIABLE DESCRIPTION TABLE

NAME TYPE PURPOSE


num Int To input the number of
iteration inputted by the
user.
ii Int To use as a counter in the
for loop
ai Int Inputting the value 1.
bi Int Inputting the value 4.
ci Int Inputting the value 5.
dd Int To add all the values and
print them.
PROGRAM-14

WAP to overload a fuction named ‘alter’ as follows;

1)int alter(int a): to take an int value and double it then subtract 3 from it and
then return the final value.

2)String alter(String s):to take a string value and return it in reverse order.

3)char alter(char c):to take a character value and return the next alphabet.

import [Link].*;

class funcDak

public int alter(int aa)

int bb=aa*2-3;

return bb;

public String alter(String ss)

int le=[Link]();

String num="";

for(int xi=0;xi<le;xi++)

char cc=[Link](xi);

num=cc+num;
}

return num;

public char alter(char cc)

char mm=(char)(cc+1);

return mm;

public static void main()

funcDak ob= new funcDak();

int xi=[Link](7);

[Link]("The result of the first function is="+xi);

String s=[Link]("apple");

[Link]("The result of the second function is="+s);

}}

OUTPUT
VARIABLE DESCRIPTION TABLE

NAME TYPE PURPOSE


bb int To change the value of a
into desired value.
le int To store length of string s.
xi int To use a counter variable
in the for loop.
num string To store reverse value of
string s
cc char To extract the value of
string s
mm char To store altered value of
c.
PROGRAM-15

Write a program to input a no and print the frequency of each digit of


a no.
import [Link].*;

class while_loop_12

public static void main()

int rr,cc,ii,mm,nn;

Scanner sc= new Scanner([Link]);

[Link]("Enter the number");

nn=[Link]();

[Link]("Digit\tFrequency");

for(ii=0;ii<=9;ii++)

mm=nn;cc=0;

while(mm!=0)

rr=mm%10;

mm=mm/10;

if(rr==ii)

cc++;
}

if(cc>0)

[Link](ii+"\t"+cc);

OUTPUT

VARIABLE DESCRIPTION TABLE-

NAME TYPE PURPOSE


nn int To store the number
ii int Loop index variable
cc int Counter variable to count
digit
rr int To store the digit
PROGRAM-16

Write a program to overload the function area () to calculate the


following as per the specifications given below:

Member functions:

void area(int) : to calculate the area of square

void area(int, int) : to calculate the area of rectangle using the formula
length * breadth

void area(double ) : to calculate the area of circle


import [Link].*;

class abcu

public void area(int aa)

int ss;

ss=aa*aa;

[Link]("the area of square is="+ss);

public void area(int le, int br)

int aar;

aar=le*br;

[Link]("the area of rectangle is="+aar);


}

public void area (double rr)

double aar;

aar=3.14*rr*rr;

[Link]("the area of circle is="+aar);

public static void main()

abcu ob= new abcu();

Scanner sc= new Scanner([Link]);

int cch;

[Link]("Enter 1. for area of square\n2. for area of rectangle\[Link]


area of circle");

cch=[Link]();

switch(cch)

case 1:

[Link]("enter the side of square");

int ss=[Link]();

[Link](ss);

break;
case 2:

[Link]("enter the lenght and breadth");

int le=[Link]();

int br=[Link]();

[Link](le,br);

break;

case 3:

[Link]("enter the radius");

double rr=[Link]();

[Link](rr);

break;

default :

[Link]("invalid choice");

OUTPUT-
VDT-

NAME TYPE PURPOSE


cch int To store the user
choice
le int To store the length
br Int To store the width
rr double To store the radius
ss int To store the side of
square.
PROGRAM 17-

Create a class prime palindrome with following specifications

• class name Primepalindrome.

• member functions.

• boolean isprime(int n) return.

• true if number is prime else return false.

• boolean ispalindrome(int n) return true if n is palindrome else


return false.

• void display(int n1, int n2).

• print all the prime palindrome number between n1 and n2.

• write a main method to create object of a clss and call the above
methods.
import [Link].*;

class Prime_palindrom

public boolean is_prime (int xi)

int ii, cc=0;

for(ii=1;ii<=xi;ii++)

if(xi%ii==0)

cc++;

}
if(cc==2)

return true;

else

return false; }

public boolean is_palin(int xi)

int ss=0,rr,mm=xi;

while(xi!=0)

rr=xi%10;

xi=xi/10;

ss=10*ss+rr;

if(ss==mm)

return true;

else

return false;

public void show(int nn, int ni )

int ii;

boolean zz, kk;

for(ii=nn;ii<=ni;ii++)

zz=is_prime(ii);
kk=is_palin(ii);

if(zz==true && kk==true)

[Link](ii+",");

public static void main ()

Scanner sc= new Scanner([Link]);

Prime_palindrom ob =new Prime_palindrom( );

int aa,bb;

[Link]("enter the lower limit");

aa=[Link]();

[Link]("enter the upper limit");

bb=[Link]();

[Link](aa,bb);

OUTPUT
VARIABLE DESCRIPTION TABLE

NAME TYPE PURPOSE


n int To store the value
returned from the main
block.
ii int To use as a control in the
for loop.
cc int To count the number of
factors till the limit.
rr int To Extract the digit from
the number given by the
user.
ss int To store reverse of the
number inputted by the
user.
mm int As a duplicate of the
number given by the user
nn int To store the value
returned from the main
block.
ni int To store the value
returned from the main
block.
PROGRAM-18

WAP to define a function int fact(int n) used to find factorial on n. calculate


value of S in main function S= n!/(m!(n-m)!

import [Link].*;

class function_2

public int fact(int aa)

int ii,ff=1;

for(ii=1;ii<=aa;ii++)

ff=ff*ii;

return ff;

public static void main( )

Scanner sc= new Scanner([Link]);

int xi,yy,zz,num,mm;

double ss;

function_2 ob= new function_2();

[Link]("enter the value of num and m");


num=[Link]();

mm=[Link]();

xi=[Link](num);

yy=[Link](mm);

zz=[Link](num-mm);

ss=1.0*xi/(yy*zz);

[Link]("the value is ="+ss);

OUTPUT

VDT-

NAME TYPE PURPOSE


num int To store the value coming
from main block.
ii int To use as a counter in the
for loop.
ff int To calculate value and
return it in the main
block.
num int To input the value from
the user.
mm int To input the value from
the user.
PROGRAM-19

WAP to input a word and check for palindrome string.


import [Link].*;

class palindrome

public static void main()

Scanner sc = new Scanner( [Link]);

String ss,s2="";

int ii,ll;

char ch;

[Link]("Enter the String");

ss=[Link]();

ll=[Link]();

for(ii=0;ii<=ll-1;ii++)

ch=[Link](ii);

s2=ch+ss;

if([Link](ss))

[Link]("Palindrome String");

else
[Link]("Not a Palindrome String");

OUTPUT

VDT-

NAME TYPE PURPOSE


ss String To store the stirng value
s2 String To store the reverse
c char To store the character of
the string.
ii int Loop index variable
PROGRAM 20

WAP to input a string and print the following.

Sample input : SUNIL KUMAR SINGH

Sample output: [Link]


import [Link].*;

class ABC

public static void main()

Scanner sc = new Scanner( [Link]);

String ss,sr="",s3="";

int ii,ll;

char c;

[Link]("Enter the String");

ss=[Link]();

ll=[Link]();

for(ii=0;ii<=ll-1;ii++)

c=[Link](ii);

if(c!=' ')
sr=sr+c;

else

c=[Link](0);

s3=s3+c+".";

sr="";

[Link]("The result is="+s2+sr);

OUTPUT

VDT-

NAME TYPE PURPOSE


ss String To store the stirng value
sr String To store the word
C char To store the character of
the string.
s3 String Loop index variable
ll int To store length of string.
PROGRAM-21

WAP to input a matrix of size 4x4 and print the sum of boundary
elements.
import [Link].*;

class matrix_1

public static void main()

int ii,jj,ss=0;

int aa[][]=new int[4][4];

Scanner sc= new Scanner([Link]);

[Link]("enter the matrix ");

for(ii=0;ii<=3;ii++)

for(jj=0;jj<=3;jj++)

aa[ii][jj]=[Link]();

}}

[Link]("The original matrix is: ");

for(ii=0;ii<=3;ii++)

for(jj=0;jj<=3;jj++)
{

[Link](aa[ii][jj]+" ");

[Link]();

for(ii=0;ii<=3;ii++)

for(jj=0;jj<=3;jj++)

if(ii==0 || ii==3 || jj==0 || jj==3)

ss=ss+aa[ii][jj];

}}

[Link]("the sum of boundary elements="+ss);

}}

VDT-

NAME TYPE PURPOSE


Aa int To store the matrix
Ss int To store the sum
Ii int Loop index variable
Jj int Loop index variable

OUTPUT-
PROGRAM 22-

WAP to input a matrix of size 4x4 and print the sum of diagonals
elements of the matrix.
import [Link].*;

class matrix3

public static void main()

int ii,jj,ss=0,s2=0;

int aa[][]=new int[4][4];

Scanner sc= new Scanner([Link]);

[Link]("enter the matrix ");

for(ii=0;ii<=3;ii++)

for(jj=0;jj<=3;jj++)

aa[ii][jj]=[Link]();

for(ii=0;ii<=3;ii++)

for(jj=0;jj<=3;jj++)
{

if(ii==jj)

ss=ss+aa[ii][jj];

else if(ii+jj==3)

s2=s2+aa[ii][jj];

[Link]("\nthe matrix elements is");

for(ii=0;ii<=3;ii++)

for(jj=0;jj<=3;jj++)

[Link](aa[ii][jj]+" ");

[Link]();

[Link]("the sum of primary diagonal elements="+ss);

[Link]("the sum of secondary diagonal elements="+s2);

}
OUTPUT
VDT-
NAME TYPE PURPOSE
aa int To store the matrix
ss int To store the sum
ii int Loop index variable
jj int Loop index variable
s2 int To store the sum
PROGRAM 23-
WAP to input a matrix of size 4x4 and print the transpose of the matrix

import [Link].*;

class matrix3

public static void main()

int ii,jj,ss=0,s2=0;

int aa[][]=new int[4][4];

Scanner sc= new Scanner([Link]);

[Link]("enter the matrix ");

for(ii=0;ii<=3;ii++)

for(jj=0;jj<=3;jj++)

aa[ii][jj]=[Link]();

for(ii=0;ii<=3;ii++)

for(jj=0;jj<=3;jj++)

{
if(ii==jj)

ss=ss+aa[ii][jj];

else if(ii+jj==3)

s2=s2+aa[ii][jj];

[Link]("\nthe matrix elements is");

for(ii=0;ii<=3;ii++)

for(jj=0;jj<=3;jj++)

[Link](aa[ii][jj]+" ");

[Link]();

[Link]("the sum of primary diagonal elements="+ss);

[Link]("the sum of secondary diagonal elements="+s2);

}
OUTPUT-
VDT-

NAME TYPE PURPOSE


aa int To store the matrix
bb int To store the transpose
matrix
ii int Loop index variable
jj int Loop index variable
PROGRAM 24-

WAP to input an integer array of size 10 and sort then in ascending


order by using selection sort.
import [Link].*;

class selection_sort

public static void main()

Scanner sc= new Scanner([Link]);

int aa[]= new int[10];

int ii,pp,mi,jj,tt;

[Link]("enter the array element");

for(ii=0;ii<=9;ii++)

aa[ii]=[Link]();

[Link]("\nThe original array is:");

for(ii=0;ii<=9;ii++)

[Link](aa[ii]+",");

for(ii=0;ii<=9;ii++)
{

pp=ii;

mi=aa[ii];

for(jj=ii+1;jj<=9;jj++)

if(aa[jj]<mi)

mi=aa[jj];

pp=jj;

tt=aa[ii];

aa[ii]=aa[pp];

aa[pp]=tt;

[Link]("\nThe sorted array is:");

for(ii=0;ii<=9;ii++)

[Link](aa[ii]+",");

}
OUTPUT

VDT-

NAME TYPE PURPOSE


aa int To store the array
element
pp int To store position of min
value
ii int Loop index variable
jj int Loop index variable
mi int To store the min value
PROGRAM 25

WAP to input an int array of size 10 and sort then in descending order
by using bubble sort.
import [Link].*;

class bubble_sort

public static void main()

Scanner sc= new Scanner([Link]);

int aa[]= new int[10];

int ii,pp,mi,jj,tt;

[Link]("enter the array element");

for(ii=0;ii<=9;ii++)

aa[ii]=[Link]();

[Link]("\nThe original array is:");

for(ii=0;ii<=9;ii++)

[Link](aa[ii]+",");

for(ii=0;ii<=9;ii++)
{

for(jj=0;jj<=8-ii;jj++)

if(aa[jj]<aa[jj+1])

{ tt=aa[jj];

aa[jj]=aa[jj+1];

aa[jj+1]=tt;

[Link]("\nThe sorted array is:");

for(ii=0;ii<=9;ii++)

[Link](aa[ii]+",");

}
OUTPUT

VDT-

NAME TYPE PURPOSE


aa int To store the array
element
ii int Loop index variable
jj int Loop index variable

You might also like