0% found this document useful (0 votes)
13 views39 pages

Program 3

The document contains multiple Java programs demonstrating various programming concepts such as searching algorithms, pattern printing, tax calculation, number classification (Duck, Spy, Happy), string manipulation, and function overloading. Each program includes a brief description of its purpose, variable descriptions, and expected outputs. The programs cover basic input/output operations, control structures, and mathematical calculations.

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)
13 views39 pages

Program 3

The document contains multiple Java programs demonstrating various programming concepts such as searching algorithms, pattern printing, tax calculation, number classification (Duck, Spy, Happy), string manipulation, and function overloading. Each program includes a brief description of its purpose, variable descriptions, and expected outputs. The programs cover basic input/output operations, control structures, and mathematical calculations.

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

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


value using Binary search technique.
import java.util.*;

class citynames

public static void main(String args[])

String u[ ]=new String [5];

int r=0,t,y;

Scanner sc= new Scanner(System.in);

System.out.println("Enter the names of 5 cities");

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

u[t]=sc.next();

System.out.println("Enter the city name to be search");

String b=sc.next();

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

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

{
r=1;

break;

if(r==1)

System.out.println("City name found at position="+(y+1));

else

System.out.println("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 java.util.*;

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(System.in);

System.out.println("Enter the vlaues in ascending order");

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

e[l]=sc.nextInt();

System.out.println("Enter the elementto be search");

p=sc.nextInt();

j=0;

d=4;

while(l<=d)
{

mi=(l+d)/2;

if(e[mi]==p)

g=1;

System.out.println("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)

System.out.println("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++)

System.out.print(b+" ");

b++;

System.out.println( );
}

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++)

System.out.print(" ");

c--;

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

{
System.out.print(t);

System.out.println( );

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 person.If 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(System.in);

double itt=0,tii;

int ag;

char gen;

System.out.println("Enter age ");

ag=sc.nextInt();

System.out.println("Enter gender as m or f");


gen=sc.next().charAt(0);

System.out.println("Enter total income of the person");

tii=sc.nextDouble();

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;

System.out.println("INCOME TAX="+itt);

else

System.out.println("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 java.util.*;

class abc

public static void main()

Scanner sc= new Scanner(System.in);

int e,f,g;

System.out.println("Enter the three values");

e=sc.nextInt();

f=sc.nextInt();

g=sc.nextInt();

if(e>f && e>g)

if(f>g)

System.out.print(g+","+f+","+e);

else

System.out.print(e+","+f+","+e);

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

if(e>g)

System.out.print(g+","+e+","+f);

else System.out.print(e+","+g+","+f);

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

if(e>f) System.out.print(f+","+e+","+g);

else System.out.print(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(System.in);

int a,b=0,d;

System.out.println("Enter your number to be checked.");

d=sc.nextInt();

while(d!=0)

a=d%10;

d=d/10;

if(a==0)

b++;

if(b>0)

System.out.println("It is a duck number.");

else

System.out.println("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(System.in);

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

System.out.println("Enter the value");

h=sc.nextInt();

while(h!=0)

g=h%10;

h=x/10;

j=j+g;

x=x*g;

if(j==x)

System.out.print("Spy Number.");

else

System.out.print("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 java.util.*;

class Happy

public static void main(String args[])

int n;

Scanner sc=new Scanner(System.in);

System.out.println("Enter a positive integer");

n=sc.nextInt();

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)

System.out.println(a+"is a HAPPY number");

else

System.out.println(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 java.util.*;

class abcd

public static void main()

Scanner sc = new Scanner(System.in);

int ll;

char c,chh;

System.out.println("enter the stirng ");

String d= sc.nextLine();

String s1="AEIOUaeiou";

ll=d.length();

c=d.charAt(0);

c=d.charAt(ll-1);

if(s1.indexOf(c)>=0 && s1.indexOf(c)==-1)

System.out.println("String starting with vowel and ending with consonant");

Else

System.out.println("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 java.util.*;

class abcde

public static void main()

Scanner sc = new Scanner(System.in);

int ii,ll;

char c;

String s1,s3="";

System.out.print("Enter the string");

s1=sc.nextLine();

ll= s1.length();

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

c=s1.charAt(ii);

if(s3.indexOf(c)==-1)

s3=s3+c;

System.out.print(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 java.util.*;

class series

public static void main( )

Scanner sc= new Scanner(System.in);

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

System.out.println("enter the value of the x and N");

xi=sc.nextInt();

num=sc.nextInt();

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

fi=fi*ii;

di=(int) Math.pow(xi,ii)/fi;

si=si+di;

System.out.println("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 java.util.*;

class abce

public static void main()

Scanner sc= new Scanner(System.in);

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

System.out.println("Enter the limit");

num=sc.nextInt();

System.out.print(ai+","+bi+","+ci);

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

dd=ai+bi+ci;

System.out.print(","+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 java.util.*;

class funcDak

public int alter(int aa)

int bb=aa*2-3;

return bb;

public String alter(String ss)

int le=ss.length();

String num="";

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

char cc=ss.charAt(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=ob.alter(7);

System.out.println("The result of the first function is="+xi);

String s=ob.alter("apple");

System.out.println("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 java.util.*;

class while_loop_12

public static void main()

int rr,cc,ii,mm,nn;

Scanner sc= new Scanner(System.in);

System.out.println("Enter the number");

nn=sc.nextInt();

System.out.println("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)

System.out.println(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 java.util.*;

class abcu

public void area(int aa)

int ss;

ss=aa*aa;

System.out.println("the area of square is="+ss);

public void area(int le, int br)

int aar;

aar=le*br;

System.out.println("the area of rectangle is="+aar);


}

public void area (double rr)

double aar;

aar=3.14*rr*rr;

System.out.println("the area of circle is="+aar);

public static void main()

abcu ob= new abcu();

Scanner sc= new Scanner(System.in);

int cch;

System.out.println("Enter 1. for area of square\n2. for area of rectangle\n3.for


area of circle");

cch=sc.nextInt();

switch(cch)

case 1:

System.out.println("enter the side of square");

int ss=sc.nextInt();

ob.area(ss);

break;
case 2:

System.out.println("enter the lenght and breadth");

int le=sc.nextInt();

int br=sc.nextInt();

ob.area(le,br);

break;

case 3:

System.out.println("enter the radius");

double rr=sc.nextDouble();

ob.area(rr);

break;

default :

System.out.println("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.

You might also like