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=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);
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;
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);
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=' ';
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