You are on page 1of 22

PROGRAM 1

//A program to replace 'e' with '*'



import java .io.*;
public class Replace
{
public static void main()throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader (read);

int a,p;
String st;
char chr;

System.out.println("Enter a string");
st=in.readLine();
p=st.length();

for(a=0;a<p;a++)
{
chr=st.charAt(a);
{
if(chr=='e')
chr='*';
}
System.out.print(chr);
}
System.out.println();
}
}




PROGRAM 2
//To display a right angled triangle made by odd numbers

public class pattern
{
public static void main()
{
int i,j;

for (i=1;i<=9;i=i+2)
{
for(j=i;j>=1;j=j-2)
{
System.out.print(j);
System.out.print(" ");
}

System.out.println();
}
}
}



PROGRAM 3
// To find the volume of a cube,a sphere,and a cuboid using OVERLOADING
import java.io.*;
public class Compute
{
double vc=0,vs=0,vcd=0;
void volume(int s)
{
vc=s*s*s*s;
System.out.println("The volume of a cube="+vc);
}
void volume(float r)
{
vs=4/3*22/7*r*r*r;
System.out.println("The volume of a sphere="+vs);
}
void volume(int l,int b,int h)
{
vcd=l*b*h;
System.out.println("The volume of a cuboid="+vcd);
}
public static void main()throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);

int s,l,b,h; float r;

System.out.println("ENTER THE VALUE OF SIDE OF A CUBE");
s=Integer.parseInt(in.readLine());

System.out.println("RADIUS OF SPHERE");
r=Float.parseFloat(in.readLine());

System.out.println("LENGTH,BREADTH AND HEIGHT OF A CUBOID.PLEASE
GIVE EACH VALUE IN EACH LINE");
l=Integer.parseInt(in.readLine());
b=Integer.parseInt(in.readLine());
h=Integer.parseInt(in.readLine());
Compute ob=new Compute();
ob.volume(s);
ob.volume(r);
ob.volume(l,b,h);
}
}
PROGRAM 4
// To find the factorial of an assigned number by RECURSIVE FUNCTION

public class recursive
{
int fact(int n)
{
if(n==0)
{
return(1);
}
else
{
return(n*fact(n-1));
}
}
public static void main()
{
int k=5,f;
recursive ob=new recursive();
f=ob.fact(k);
System.out.println("Factorial of the no."+ k+" is "+f);
}
}
PROGRAM 5
//A program to display an inverted triangle made by natural numbers upto 15

public class patt
{
public static void main()
{
int c=15,i,j;

for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
System.out.print(c--);
System.out.print(" ");
}
System.out.println();
}
}
}



PROGRAM 6
//To pass the elements of an array to a function
import java.io.*;
public class sorting
{
void sort(int m[])
{
int i,j,t;

for(i=0;i<9;i++)
{
for(j=i+1;j<10;j++)
{
if(m[i]>m[j])
{
t=m[i];
m[i]=m[j];
m[j]=t;
}
}
}
System.out.println("The numbers are arranged in ascending order are :");

for(i=0;i<10;i++)
System.out.println(m[i]);
}

public static void main()throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
sorting ob=new sorting();

int i;
int n[]=new int[10];

for(i=0;i<10;i++)
{
System.out.print("Enter your marks in the cell "+(i+1)+":");
n[i]=Integer.parseInt(in.readLine());
}
ob.sort(n);
}
}
PROGRAM 7
//To search a number by LINEAR SEARCH

import java.io.*;
public class search
{
public static void main() throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);

int i,k=0,sh;
int m[]=new int[10];

for(i=0;i<10;i++)
{
System.out.println("Enter numbers:");
m[i]=Integer.parseInt(in.readLine());
}

System.out.println("Enter number to be searched");
sh=Integer.parseInt(in.readLine());

for(i=0;i<10;i++)
{
if(m[i]==sh)
k=1;
}
if(k>0)
System.out.println("THE REQUIRED NUMBER "+sh+" IS PRESENT");
else
System.out.println("THE REQUIRED NUMBER "+sh+" IS NOT PRESENT");
}
}

VARIABLE DESCRIPTION
VARIABLE DATA TYPE DESCRIPTION
i Integer Loop variable
k Integer Counter variable
sh Integer Variable used to input a
number given by user
m[] Integer Array variable


PROGRAM 8
//To search the capital of state

import java.io.*;
public class capital_state
{
public static void main()throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);

int i,a=0,k=0;
String ch;
String c[]=new String[10];
String s[]=new String[10];

for(i=0;i<10;i++)
{
System.out.print("Enter states :");
s[i]=in.readLine();
System.out.print("Enter capitals:");
c[i]=in.readLine();
}

System.out.println("Enter the state whose capital to be searched");
ch=in.readLine();

for(i=0;i<10;i++)
{
if(s[i].equals(ch))
{
k=1;
a=i;
}
}
if(k==1)
System.out.println("The capital is "+c[a]+".");
else
System.out.println("The state "+ch+" is not found at any location.");
}
}

VARIABLE DESCRIPTION
VARIABLE DATA TYPE DESCRIPTION
i Integer Loop variable
a Integer Counter variable
k Integer To use the value of loop to
get output
s[] String Array variable
c[] String Array variable
ch String Array variable


PROGRAM 9
//To arrange the numbers using Bubble sort

import java.io.*;
public class bubble_sort
{
public static void main()throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);

int i,j,t;
int m[]=new int[10];

for(i=0;i<10;i++)
{
System.out.print("Enter numbers:");
m[i]=Integer.parseInt(in.readLine());
}
for(i=0;i<9;i++)
{
for(j=0;j<(9-i);j++)
{
if(m[j]>m[j+1])
{
t=m[j];
m[j]=m[j+1];
m[j+1]=t;
}
}
}
System.out.println("The numbers arranged in ascending order are :");

for(i=0;i<10;i++)
System.out.println(m[i]);
}
}





VARIABLE DESCRIPTION
VARIABLE DATA TYPE DESCRIPTION
i Integer Loop variable
j Integer Loop variable
t Integer Used to store temporary
value
m[] Integer Array variable


PROGRAM 10
//To find out maximum and minimum number
import java.io.*;
public class max_min
{
public static void main()throws IOException
{
InputStreamReader read = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(read);

int i,j,min,max;
int m[]=new int[10];

for(i=0;i<10;i++)
{
System.out.print("Enter the number in the cell : ");
m[i]=Integer.parseInt(in.readLine());
}
max=m[0];min=m[0];

for(i=0;i<10;i++)
{
if(max<m[i])
max=m[i];
if(min>m[i])
min=m[i];
}
System.out.println("The greatest of the array element = " + max);
System.out.println("The smallest of the array element =" + min);
}
}








VARIABLE DESCRIPTION
VARIABLE DATA TYPE DESCRIPTION
i Integer Loop variable
j Integer Loop variable
min Integer To store minimum value
max Integer To store maximum value
m[] Integer Array variable


PROGRAM 11
// A program to display a rectangle
public class rectangle
{
public static void main()
{
int a,b,c,p=2;

for(a=1;a<=5;a++)
{
for(b=1;b<=a;b++)
System.out.print(a);
for(c=p;c<=5;c++)
System.out.print(c);
System.out.println();
p++;
}
}
}

VARIABLE DESCRIPTION
VARIABLE DATA TYPE DESCRIPTION
a Integer Loop variable
b Integer Loop variable
c Integer Loop variable
p Integer Counter variable


PROGRAM 12
//To display the string after changing the case of the string

import java.io.*;
public class welcome
{
public static void main() throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);

String name,name1="";
int a,i;
char chr,chr1=0;

System.out.println("Enter string");
name=in.readLine();

a=name.length();

for(i=0;i<a;i++)
{
chr=name.charAt(i);
if(chr>='A'&& chr<='Z')
{
chr1=Character.toLowerCase(chr);
name1=name1+chr1;
}
else if(chr>='a' && chr<='z')
{
chr1=Character.toUpperCase(chr);
name1=name1+chr1;
}
else
{
name1=name1+chr;
}
}
System.out.print(name1);
}
}


VARIABLE DESCRIPTION
VARIABLE DATA TYPE DESCRIPTION
name String To input a string
name1 String To store the result
chr Character To take out a character
chr1 Character To change the case of
character
a Integer To find out length
i Integer Loop variable

PROGRAM 13
//To count the number of upper case,lower case,special character and digits in the string

import java.io.*;
public class count
{
public static void main()throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);

String name;

int up=0,low=0,dig=0,spl=0,i,p;
char chr;

System.out.println("Enter string:");
name=in.readLine();

p=name.length();

for(i=0;i<p;i++)
{
chr=name.charAt(i);
if(chr>='A' && chr<='Z')
up++;
else if(chr>='a' && chr<='z')
low++;
else if(chr>='0' && chr<='9')
dig++;
else
spl++;
}

System.out.println("NO.OF UPPERCASE="+up);
System.out.println("NO.OF LOWERCASE="+low);
System.out.println("NO.OF SPECIAL CHARACTERS="+spl);
System.out.println("NO.OF DIGITS="+dig);
}
}

VARIABLE DESCRIPTION
VARIABLE DATA TYPE DESCRIPTION
name String To input string
up Integer To count uppercase
characters
low Integer To count lower case
characters
dig Integer To count digits
spl Integer To count special
characters
i Integer Loop variable
p Integer To count length
chr Character To pick up characters


PROGRAM 14
// To display piglatin word

import java.io.*;
public class piglatin
{
public static void main()throws IOException
{
int c=0,i,len;
String s="",s1="",t="ay",w="";
char ch=' ';

InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);

System.out.println("Enter string:");
s=in.readLine();
len=s.length();

System.out.println("The pig latin word:");

for(i=0;i<len;i++)
{
ch=s.charAt(i);
if((ch=='a')||(ch=='e')||(ch=='i')||(ch=='o')||(ch=='u')||(ch=='A')||
(ch=='E')||(ch=='I')||(ch=='O')||(ch=='U'))
{
w=s.substring(i,len);
s1=s.substring(0,i);
break;
}
}
System.out.println(w+s1+t);
}
}





VARIABLE DESCRIPTION
VARIABLE DATA TYPE DESCRIPTION
i Integer Loop variable
len Integer To find the length of
string
t String To store a word
w String Substring variable
s String To input a word
s1 String Substring variable
ch Character To pick up a character

You might also like