Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
4 views27 pages

PRG12

The document contains multiple Java programs that demonstrate various programming concepts such as searching algorithms, pattern printing, input validation, and number classification. Each program includes a description of its purpose, variable descriptions, and sample output. The programs cover topics like binary search, tax calculation, duck numbers, spy numbers, happy numbers, and string manipulation.

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)
4 views27 pages

PRG12

The document contains multiple Java programs that demonstrate various programming concepts such as searching algorithms, pattern printing, input validation, and number classification. Each program includes a description of its purpose, variable descriptions, and sample output. The programs cover topics like binary search, tax calculation, duck numbers, spy numbers, happy numbers, and string manipulation.

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
You are on page 1/ 27

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

You might also like