Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PROGRAMS

Download as pdf or txt
Download as pdf or txt
You are on page 1of 37

SELECTION SORT

import java.util.Scanner;
public class SelectionSort
{
int[ ] a;
int size;
public SelectionSort(int n)
{
size=n;
a=new int[n];
}
static Scanner sc=new Scanner(System.in);
public void input()
{
System.out.println("Enter "+size+" array elements");
for(int i=0;i<size;i++)
a[i]=sc.nextInt();
}

public void sort()


{
int i,j,max,pos;
for(i=0;i<=size-2;i++)
{
max=a[i];
pos=i;
for(j=i+1;j<=size-1;j++)
{
if(a[j]>max)
{
max=a[j];
pos=j;
}
}
if(pos!=i)
{
a[pos]=a[i];
a[i]=max;
}
}
}

public void display( )


{
for(int i=0;i<a.length;i++)
{
System.out.print(a[i]+" ");
}
System.out.println();
}

public static void main()


{
System.out.println("Enter size of array");
int n=sc.nextInt();
SelectionSort obj = new SelectionSort(n);
obj.input();
obj.sort();
System.out.println("sorted array is: ");
obj.display();
}
}

BINARY SEARCH
import java.util.Scanner;
public class BinarySearch
{
int[]arr ;
int size;
int num ;
public BinarySearch(int n)
{
size=n;
arr=new int[n];
}
static Scanner sc=new Scanner(System.in);
public void input()
{
System.out.println("Enter "+size+" array elements");
for(int i=0;i<size;i++)
{
arr[i]=sc.nextInt();
}
System.out.println("Enter number to be searched");
num=sc.nextInt();
}

public void sort( )


{
int i,j,temp=0;
boolean f=true;
for(i=1;i<size-1;i++)
{
for(j=0;j<=size-i-1;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
f=false;
}
}//end of inner for loop
if(f==true)
break;
else
f=true;
}
}

public boolean search( )


{
boolean check=false;
int high,low,mid;
low=0;
high=size-1;
while(low<=high)
{
mid=(low+high)/2;
if(num==arr[mid])
{
check=true;
break;
}
else
if(num>arr[mid])
low=mid+1;
else
high=mid-1;
}
return check;
}

public void display( )


{
System.out.println(search()?"Number is found":"Number is not found");
}

public static void main()


{
System.out.println("enter size of array");
int n=sc.nextInt();
BinarySearch obj = new BinarySearch(n);
obj.input();
obj.sort();
obj.display();
}
}

ARMSTRONG
import java.util.*;
public class Armstrong1
{
public boolean isArmstrong(int num)
{
int d,sum=0,copy,i;
copy=num;
sum=0;
while(copy>0)
{
d=copy%10;
sum=sum+(d*d*d);
copy=copy/10;
}
if(sum==num)
return true;
else
return false;
}
public void findAndDisplay()
{
System.out.println("All three digit Armstrong numbers");
for(int i=100;i<=999;i++)
{
if(isArmstrong(i))
System.out.println(i);
}
}
public static void main()
{
Armstrong1 obj=new Armstrong1();
obj.findAndDisplay();
}
}

BUBBLE SORT
import java.util.Scanner;
public class bubblesort
{
int[ ] a;
int size;
public bubblesort(int n)
{
size=n;
a=new int[n];
}
static Scanner sc=new Scanner(System.in);
public void input()
{
System.out.println("Enter "+size+" array elements");
for(int i=0;i<size;i++)
a[i]=sc.nextInt();
}

public void sort()


{
int n=a.length;
int i,j;
for (i=1;i<n;i++)
{
for (j=0;j<n-i;j++)
{
if(a[j]>a[j+1])
{
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}

public void display( )


{
for(int i=0;i<a.length;i++)
{
System.out.print(a[i]+" ");
}
System.out.println();
}

public static void main()


{
System.out.println("Enter size of array");
int n=sc.nextInt();
bubblesort obj = new bubblesort(n);
obj.input();
obj.sort();
System.out.println("sorted array is: ");
obj.display();
}
}

VAMPIRE NO.
import java.util.*;
public class VampireNo
{
static Scanner sc=new Scanner(System.in);
boolean checkComposite(int n)
{
int c=0;
for(int i=1;i<=n;i++)
{
if(n%i==0)
c++;
}
if(c>2)
return true;
else
return false;
}
int countdigits(int n)
{
return (int)Math.log10(n)+1;
}
int [] getdigits(int x,int y)
{
int l=0;
if(y==0)
l=countdigits(x);
else
l=2*countdigits(x);
int []a=new int[l];
for(int i=0;i<l;i++)
{
if(x>0)
{
a[i]=x%10;x/=10;
}
else
{
a[i]=y%10;y/=10;
}
}
return a;
}

boolean isVampireNo(int n)
{
if(!checkComposite(n))
return false;
if(countdigits(n)%2!=0)
return false;
int i,j;
out: for(i=1;i<=(int)Math.sqrt(n);i++)
{
if(n%i==0)
{
j=n/i;
int x=countdigits(n)/2;
if(countdigits(i)==x &&countdigits(j)==x &&(i%10!=0||j%10!=0))
{
int[]a=getdigits(n,0),b=getdigits(i,j);
Arrays.sort(a);Arrays.sort(b);
for(int z=0;z<a.length;z++)
if(a[z]!=b[z])
continue out;
return true;
}
}
}
return false;
}
public void main()
{

int m,n;
do{
System.out.print("Enter the value of M: ");
m=sc.nextInt();
System.out.print("Enter the value of N: ");
n=sc.nextInt();
}
while (m<1000 || n<=m || n>9999);
System.out.println("Vampire numbers between "+ m +" to "+ n +" are: ");
for (int i=m;i<=n;i++)
{
if(isVampireNo(i))
{
System.out.println(i);
}
}
}
}

SUM OF DIAGONALS
import java.util.Scanner;
public class SumOfDiagonals
{//start of class
int[][]a;//refers to the array object
int size;//refers to the array size(no of rows=no. of columns here)
public SumOfDiagonals (int n)//parameterized constructor to initialize size=n and allocate space
for the array object
{
size=n;
a=new int[n][n];
}
static Scanner sc=new Scanner(System.in);
public void input()//accepts array elements and stores in the array
{
for(int i=0;i<size;i++)
{
for (int j=0;j<size;j++)
{
System.out.println("Enter elements");
a[i][j]=sc.nextInt();
}
}
}

public void getSum()//this method calculates the sum of primary and secondary diagonal
elements separately
{
int sld=0,srd=0;
for(int i=0;i<size;i++)
{
sld+=a[i][i];
srd+=a[i][size-1-i];
}
System.out.println("the sum of primary diagonal "+sld);
System.out.println("the sum of secondary diagonal "+srd);
}

public void display()//displays the 2-D array in matrix format.


{
for(int i=0;i<size;i++)
{
for (int j=0;j<size;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}

public static void main()


{
System.out.println("Enter size of array");
int n=sc.nextInt();
SumOfDiagonals obj = new SumOfDiagonals(n);
obj.input();
obj.getSum();
System.out.println("2D array in matrix format is: ");
obj.display();

}
}

Total Vowel And Consonant


import java.util.*;
public class TotalVowelAndConsonant16
{
String sen;
static Scanner sc=new Scanner(System.in);
public void input()
{
System.out.println("enter a sentence");
Scanner sc=new Scanner(System.in);
sen=sc.nextLine();
}

public void extract()


{
int vo=0,co=0;
StringTokenizer st= new StringTokenizer(sen);
while(st.hasMoreTokens())
{
String word=st.nextToken();
String a="AEIOUaeiou";
for(int i=0;i<word.length();i++)
{
char ch=word.charAt(i);
if(Character.isLetter(ch))
{
if(a.indexOf(ch)!=-1)
vo++;
else
co++ ;
}
}
System.out.println("Word \t No. Of Vowel \t No. Of Consonant");
System.out.println(word + "\t\t" + vo + "\t\t" + co);
vo=co=0;
}
}
public void display()
{
System.out.println("Words with equal number of vowel and consonant are-");
int vo=0,co=0;
StringTokenizer st= new StringTokenizer(sen);
while(st.hasMoreTokens())
{
String word=st.nextToken();
String a="AEIOUaeiou";
for(int i=0;i<word.length();i++)
{
char ch=word.charAt(i);
if(Character.isLetter(ch))
{
if(a.indexOf(ch)!=-1)
vo++;
else
co++ ;
}
}
if(vo==co)
{
System.out.println(word);
}
}
}

public static void main()


{
TotalVowelAndConsonant16 obj= new TotalVowelAndConsonant16();
obj.input();
obj.extract();
obj.display();
}
}
TRANSPOSE ARRAY
import java.util.Scanner;
public class transposeArray
{//start of class
int [][] orig;
int [][]tp;
int row;
int col;
static Scanner sc=new Scanner(System.in);
public transposeArray(int r,int c)
{
row=r;
col=c;
orig=new int[row][col];
tp=new int[col][row];
}

public void input()


{//this method is used to take inputs of the elements
for(int i=0;i<row;i++)
{//outer loop starts
for(int j=0;j<col;j++)
{//inner loop starts
System.out.println("Enter value for input matrix");
orig[i][j]=sc.nextInt();
}//inner loop ends
}//outer loop ends
}

public void transpose()


{
for(int i=0;i<col;i++)
{//outer loop starts
for(int j=0;j<row;j++)
{ //inner loop starts
tp[i][j]=orig[j][i];
}//inner loop ends
}//outer loop ends
}

public void display()


{//this method displays the original array and transpose array
int i,j;
System.out.println("The original Matrix is");
for (i=0;i<row;i++)
{//outer loop starts
for(j=0;j<col;j++)
{//inner loop starts
System.out.print(orig[i][j]+" ");
}//inner loop ends
System.out.println();
}//outer loop ends
System.out.println("The Transposed Matrix is");
for (i=0;i<col;i++)
{//outer loop starts
for(j=0;j<row;j++)
{//inner loop starts
System.out.print(tp[i][j]+" ");
}//inner loop ends
System.out.println();
}//outer loop ends
}
public static void main()
{//this method calls other methods accordingly
System.out.println("Enter no. of rows");
int r=sc.nextInt();
System.out.println("Enter no. of columns");
int c=sc.nextInt();
transposeArray obj=new transposeArray(r,c);
obj.input();
obj.transpose();
obj.display();
}
}//end of class

COMMON UNCOMMON NUMBERS


import java.util.*;
public class CommonUncommon
{
int n,m;
int[]a,b,c;
static Scanner sc=new Scanner(System.in);
public CommonUncommon(int s,int t)
{
n=s;
a=new int[n];
m=t;
b=new int[m];
c=new int[Math.min(n,m)];
}

void input()
{
System.out.println("Enter "+ n +" array elements for 1st array");
for(int i=0;i<n;i++)
a[i]=sc.nextInt();

System.out.println("Enter "+ m +" array elements for 2nd array");


for(int i=0;i<m;i++)
b[i]=sc.nextInt();
}

boolean check(int []x,int z,int y)


{
for(int i=0;i<z;i++)
if(x[i]==y)return true;
return false;
}

int common()
{
int x=0;
for(int i=0;i<n;i++)
{
if(check(b,b.length,a[i])&&!check(c,x,a[i]))
c[x++]=a[i];
}
return x;
}

void display(int[]x,int y)
{

for(int i=0;i<y;i++)
System.out.print(x[i]+" ");
System.out.println();
}

void uncommon()
{
System.out.println("The 1st array:");
display(a,n);
System.out.println("The 2nd array:");
display(b,m);
int p=common();
System.out.println("Common values of both the arrays are:");
display(c,p);
System.out.println("Extra numbers in 1st Array which are not present in the 1st array are:");
for(int i=0;i<n;i++)
if(!check(c,p,a[i]))
System.out.print(a[i]+" ");
System.out.println();
System.out.println("Extra numbers in 1st Array which are not present in the 2nd array are:");
for(int i=0;i<m;i++)
if(!check(c,p,b[i]))
System.out.print(b[i]+" ");
System.out.println();
}

public static void main()


{
System.out.println("Enter size of 1st array");
int n=sc.nextInt();
System.out.println("Enter size of 2nd array");
int m=sc.nextInt();
if(n<=4 || m<=4)
{
System.out.println("invalid input");
System.exit(0);
}
CommonUncommon obj=new CommonUncommon(n,m);
obj.input();
obj.uncommon();
}
}

REVERSE ARRAY
import java.util.Scanner;
public class ArrayReverse7
{
int[] a;
int size;
static Scanner sc=new Scanner(System.in);
public ArrayReverse7 (int n )//parameterized constructor to initialize size=n and allocate space
for the array object.
{
size=n;
a=new int[size];
}
public void input( )// accepts array elements and stores in the array.
{
System.out.println("Enter "+size+" array elements");
for(int i=0;i<a.length;i++)
{
a[i]=sc.nextInt();
}
}
public void reverse( )//reverses the array by swapping 1st and last values ,2nd and 2nd last
values and so on.
{//start of method
for(int i=0;i<a.length/2;i++)
{//end of method
int t=a[i];
a[i]=a[a.length-i-1];
a[a.length-i-1]=t;
}//end of loop
}//end of method
public void display()
{
for(int i=0;i<a.length;i++)
System.out.print( a[i]+" ");
System.out.println();
}
public static void main()
{
System.out.println("enter size of array");
int n=sc.nextInt();
ArrayReverse7 obj = new ArrayReverse7 (n);
obj.input();
System.out.println("Original array is:-");
obj.display();
System.out.println("Reversed array elements are");
obj.reverse();
obj.display();
}
}

REVERSE EACH ROW


import java.util.Scanner;
public class ReverseEachRow11
{
int[][] a;
int N;
int M;
static Scanner sc=new Scanner(System.in);
public ReverseEachRow11(int row,int col)
{
N=row;
M=col;
a=new int[N][M];
}
public void input()
{
System.out.println("Enter "+(N*M)+" values for 1st matrix");
for(int i=0;i<N;i++)
{
for(int j=0;j<M;j++)
{
a[i][j]=sc.nextInt();
}
}
}
public void doReverse()
{
int l;
int h;
for(int i=0;i<N;i++)
{
for(l=0,h=M-1;l<h;l++,h--)
{
int temp=a[i][l];
a[i][l]=a[i][h];
a[i][h]=temp;
}
}
}
public void display()
{
for(int i=0;i<N;i++)
{
for (int j=0;j<M;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
public static void main()
{
System.out.println("Enter no. of rows");
int r=sc.nextInt();
System.out.println("Enter no. of columns");
int c=sc.nextInt();
ReverseEachRow11 obj=new ReverseEachRow11(r,c);
obj.input();
System.out.println("The original matrix is: ");
obj.display();
obj.doReverse();
System.out.println("The reversed matrix is: ");
obj.display();
}
}

MINIMUM MAXIMUM AVERAGE SUM


import java.util.Scanner;
public class MinMaxSumAvg13
{
int[][] a;
int N;
int M;
static Scanner sc=new Scanner(System.in);
public MinMaxSumAvg13(int row,int col)
{
N=row;
M=col;
a=new int[N][M];
}
public void input()
{
System.out.println("Enter "+(N*M)+" values for the matrix");
for(int i=0;i<N;i++)
{
for(int j=0;j<M;j++)
{
a[i][j]=sc.nextInt();
}
}
}

public void calc()


{
int sum=0,i,j,min,max;
double avg=0.0;
for(i=0;i<N;i++)
{
max=min=a[i][0];
for(j=0;j<M;j++)
{
sum+=a[i][j];
if(a[i][j]<min)
min=a[i][j];
if(a[i][j]>max)
max=a[i][j];
}
avg=sum/(double)M;
System.out.println("Row# " +(i+1));
System.out.println("Minimum: "+min+ "\t"+"Maximum: "+max+ "\t"+ "Sum: "+sum+ "\t" +
"Average: "+avg);
sum=0;//"Minimum: "+min+
}
}
public void display()
{
for(int i=0;i<N;i++)
{
for (int j=0;j<M;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
public static void main()
{
System.out.println("How many rows ?");
int r=sc.nextInt();
System.out.println("How many columns ?");
int c=sc.nextInt();
MinMaxSumAvg13 obj=new MinMaxSumAvg13(r,c);
obj.input();
System.out.println("The original matrix is: ");
obj.display();
obj.calc();
}
}

TRANSPOSE ARRAY
import java.util.Scanner;

public class Transpose

int [][] og;

int [][]tp;

int row;

int col;

static Scanner sc=new Scanner(System.in);

public Transpose(int r,int c)

row=r;

col=c;

og=new int[row][col];

tp=new int[col][row];

public void input()

System.out.println("Enter "+(row*col)+" values for input matrix");

for(int i=0;i<row;i++)

for(int j=0;j<col;j++)
{

og[i][j]=sc.nextInt();

public void transpose()

for (int i=0;i<row;i++)

for(int j=0;j<col;j++)

tp[j][i]=og[i][j];

public void display()

int i,j;

System.out.println("The original Matrix is");

for(i=0;i<row;i++)

for(j=0;j<col;j++)

System.out.print(og[i][j]+" ");

System.out.println();

System.out.println("The Transposed Matrix is");

for(i=0;i<col;i++)

for(j=0;j<row;j++)

System.out.print(tp[i][j]+" ");

}
System.out.println();

public static void main()

System.out.println("Enter no. of rows");

int r=sc.nextInt();

System.out.println("Enter no. of columns");

int c=sc.nextInt();

Transpose obj = new Transpose(r,c);

obj.input();

obj.transpose();

obj.display();

EVEN NOS IN ARRAY


import java.util.*;

public class EvenNosInDiagonal

int[][]a;//refers to the array object

int size;//refers to the array size(no of rows=no. of columns here)

public EvenNosInDiagonal (int n)//parameterized constructor to initialize size=n and allocate space for the array
object

size=n;

a=new int[n][n];

static Scanner sc=new Scanner(System.in);

public void input()//accepts array elements and stores in the array

for(int i=0;i<size;i++)

for (int j=0;j<size;j++)

{
System.out.println("Enter elements");

a[i][j]=sc.nextInt();

public void even()//this method calculates the sum of primary and secondary diagonal elements separately

int sld=0,srd=0;

for (int i=0;i<size;i++)

if(a[i][i]%2==0)

System.out.println(a[i][i]);

else if(a[i][size-1-i]%2==0)

System.out.println(a[i][size-1-i]);

public void display()//displays the 2-D array in matrix format.

for(int i=0;i<size;i++)

for (int j=0;j<size;j++)

System.out.print(a[i][j]+" ");

System.out.println();

public static void main()

System.out.println("Enter size of array");

int n=sc.nextInt();

EvenNosInDiagonal obj = new EvenNosInDiagonal(n);

obj.input();

obj.even();
System.out.println("2D array in matrix format is: ");

obj.display();

}//end of class

SPECIAL NUMBERS
/** Write a program in Java to input lower and upper limit and display all the special numbers within the limit .

A special number is a number whose sum of the odd digits is equal to the sum of the even digits.

Display error message if lower limit is not less than upper limit.

Class name:- SpecialNumbersInARange

Data Members are-

int lower- stores the lower limit

int upper-stores the upper limit

Instance methods are-

public void input( ) – This method accepts lower and upper limit value.

public void display( )- This method displays all the special numbers between lower and upper limits with the help of
isSpecial( ).

public boolean isSpecial(int num)- This method checks whether num is a Special number or not and returns true or
false accordingly.

Write main( ) to create object of the Class SpecialNumbersInARange and call the above methods accordingly */

import java.util.Scanner;

public class SpecialNumbersInARange3

static Scanner sc=new Scanner(System.in);

int lower;

int upper;

public void input( )//This method accepts total number of terms.Display “Invalid Input” if n <=0

System.out.println("Enter the lower range");

lower=sc.nextInt();

System.out.println("Enter the upper range");

upper=sc.nextInt();

if(lower>=upper)

System.out.println("invalid input");

System.exit(0);
}

public boolean isSpecial(int num)//This method checks whether num is a Special number or not and returns true
or false accordingly.

int digit,evensum=0,oddsum=0;

while(num>0)

digit=num%10;

if(digit%2==0)

evensum+=digit;

else

oddsum+=digit;

num/=10;

return oddsum==evensum;

//This method displays all the special numbers between lower and upper limits with the help of isSpecial( ).

public void display()//

System.out.println("The special numbers in the range are ");

for(;lower<=upper;++lower)

if(isSpecial(lower))

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

public static void main()

SpecialNumbersInARange3 obj=new SpecialNumbersInARange3();

obj.input();

obj.display();

}
PIGLATIN
/** Write a program in Java to input lower and upper limit and display all the special numbers within the limit .

A special number is a number whose sum of the odd digits is equal to the sum of the even digits.

Display error message if lower limit is not less than upper limit.

Class name:- SpecialNumbersInARange

Data Members are-

int lower- stores the lower limit

int upper-stores the upper limit

Instance methods are-

public void input( ) – This method accepts lower and upper limit value.

public void display( )- This method displays all the special numbers between lower and upper limits with the help of
isSpecial( ).

public boolean isSpecial(int num)- This method checks whether num is a Special number or not and returns true or
false accordingly.

Write main( ) to create object of the Class SpecialNumbersInARange and call the above methods accordingly */

import java.util.Scanner;

public class SpecialNumbersInARange3

static Scanner sc=new Scanner(System.in);

int lower;

int upper;

public void input( )//This method accepts total number of terms.Display “Invalid Input” if n <=0

System.out.println("Enter the lower range");

lower=sc.nextInt();

System.out.println("Enter the upper range");

upper=sc.nextInt();

if(lower>=upper)

System.out.println("invalid input");

System.exit(0);

public boolean isSpecial(int num)//This method checks whether num is a Special number or not and returns true
or false accordingly.
{

int digit,evensum=0,oddsum=0;

while(num>0)

digit=num%10;

if(digit%2==0)

evensum+=digit;

else

oddsum+=digit;

num/=10;

return oddsum==evensum;

//This method displays all the special numbers between lower and upper limits with the help of isSpecial( ).

public void display()//

System.out.println("The special numbers in the range are ");

for(;lower<=upper;++lower)

if(isSpecial(lower))

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

public static void main()

SpecialNumbersInARange3 obj=new SpecialNumbersInARange3();

obj.input();

obj.display();

WORD LENGTH
import java.util.Scanner;

public class WordLength

static Scanner sc=new Scanner(System.in);

void sort(String s[])


{

int n=s.length;

int i,j;

for (i=0;i<n;i++)

for (j=0;j<n-i-1;j++)

if(s[j].length()>s[j+1].length())

String temp=s[j];

s[j]=s[j+1];

s[j+1]=temp;

System.out.println("New sentence is: ");

for(i=0;i<s.length;i++)

System.out.print(s[i]+" ");

System.out.println();

public static void main()

WordLength obj= new WordLength();

System.out.println("enter a sentence");

String sen=sc.nextLine();

String []s =sen.split(" ");

obj.sort(s);

BOUNDARY ELEMENTS ADD


import java.util.Scanner;

class boundaryElementsAdd
{

int [][] a;

int row;

int col;

static Scanner sc=new Scanner(System.in);

public boundaryElementsAdd(int r,int c)

row=r;

col=c;

a=new int[row][col];

public void input()

{//this method is used to take inputs of the elements

for(int i=0;i<row;i++)

{//outer loop starts

for(int j=0;j<col;j++)

{//inner loop starts

System.out.println("Enter value for input matrix");

a[i][j]=sc.nextInt();

}//inner loop ends

}//outer loop ends

public void calc()

int s=0;

for(int i=0;i<col;i++)

{//outer loop starts

for(int j=0;j<row;j++)

{ //inner loop starts

if(i==0 || i==col-1 || j==0 || j==row-1)

s+=a[i][j];
}

}//inner loop ends

}//outer loop ends

System.out.println("The sum of the boundary elements are" + s);

public static void main()

{//this method calls other methods accordingly

System.out.println("Enter no. of rows");

int r=sc.nextInt();

System.out.println("Enter no. of columns");

int c=sc.nextInt();

boundaryElementsAdd obj=new boundaryElementsAdd(r,c);

obj.input();

obj.calc();

You might also like