PROGRAMS
PROGRAMS
PROGRAMS
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();
}
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();
}
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();
}
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);
}
}
}
void input()
{
System.out.println("Enter "+ n +" array elements for 1st array");
for(int i=0;i<n;i++)
a[i]=sc.nextInt();
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();
}
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();
}
}
TRANSPOSE ARRAY
import java.util.Scanner;
int [][]tp;
int row;
int col;
row=r;
col=c;
og=new int[row][col];
tp=new int[col][row];
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
{
og[i][j]=sc.nextInt();
for(int j=0;j<col;j++)
tp[j][i]=og[i][j];
int i,j;
for(i=0;i<row;i++)
for(j=0;j<col;j++)
System.out.print(og[i][j]+" ");
System.out.println();
for(i=0;i<col;i++)
for(j=0;j<row;j++)
System.out.print(tp[i][j]+" ");
}
System.out.println();
int r=sc.nextInt();
int c=sc.nextInt();
obj.input();
obj.transpose();
obj.display();
public EvenNosInDiagonal (int n)//parameterized constructor to initialize size=n and allocate space for the array
object
size=n;
a=new int[n][n];
for(int i=0;i<size;i++)
{
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;
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]);
for(int i=0;i<size;i++)
System.out.print(a[i][j]+" ");
System.out.println();
int n=sc.nextInt();
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.
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;
int lower;
int upper;
public void input( )//This method accepts total number of terms.Display “Invalid Input” if n <=0
lower=sc.nextInt();
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( ).
for(;lower<=upper;++lower)
if(isSpecial(lower))
System.out.print(lower+" ");
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.
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;
int lower;
int upper;
public void input( )//This method accepts total number of terms.Display “Invalid Input” if n <=0
lower=sc.nextInt();
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( ).
for(;lower<=upper;++lower)
if(isSpecial(lower))
System.out.print(lower+" ");
obj.input();
obj.display();
WORD LENGTH
import java.util.Scanner;
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;
for(i=0;i<s.length;i++)
System.out.print(s[i]+" ");
System.out.println();
System.out.println("enter a sentence");
String sen=sc.nextLine();
obj.sort(s);
class boundaryElementsAdd
{
int [][] a;
int row;
int col;
row=r;
col=c;
a=new int[row][col];
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
a[i][j]=sc.nextInt();
int s=0;
for(int i=0;i<col;i++)
for(int j=0;j<row;j++)
s+=a[i][j];
}
int r=sc.nextInt();
int c=sc.nextInt();
obj.input();
obj.calc();