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

Array Programs

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

import java.util.

*;
class Odius
{
int arr[];
int n;
static Scanner sc = new Scanner(System.in);
public Odius()
{

}
public Odius(int nx)
{
size=s;
arr=new int[size];
}
public void getdata()
{
System.out.println("Enter numbers in array");
for(int i=0;i<size;i++)
{
arr[i]=sc.nextInt();
}
}
public void show_Odius()
{
int i,x,d,c;
for(i=0;i<size;i++)
{
x=arr[i];c=0;

while(x!=0)
{
d=x%2;
if(d==1)
c++;
x/=2;
}
if(c%2!=0)
System.out.println("Number "+arr[i]+" is Odius");
}

}
public static void main(String args[])
{
Odius obj=new Odius();
System.out.println("Enter the size");
int a=sc.nextInt();
Odius obj=new Odius(a);
obj.getdata();
obj.show_Odius();

}
}
import java.util.*;
class Prime_Factors
{
int arr[];
int s;
static Scanner sc = new Scanner(System.in);
public Prime_Factors(int lim)
{
s=lim;
arr=new int[size];
}
public void Fillarray()
{

System.out.println("Enter numbers in array");


for(int i=0;i<s;i++)
{
arr[i]=sc.nextInt();
}
}
public void show_Primefacts()
{
int i,x,j;
for(i=0;i<s;i++)
{
x=arr[i];j=2;
System.out.print("Prime factor of "+x+" : ");
while(x!=1)
{
if(x%j==0)
{
System.out.print(j+" ");
x=x/j;
}
else
j++;
}
System.out.println();
}

public static void main(String args[])


{
System.out.println("Enter the size");
int a=sc.nextInt();
Prime_Factors obj=new Prime_Factors(a);
obj.Fillarray();
obj.show_Primefacts();

}
}
import java.util.*;
class Sorter
{
int arr[]=new int[100];
static Scanner sc = new Scanner(System.in);
public Sorter()
{
for(int i=0;i<100;i++)
arr[i]=0;
}
public void readlist()
{
System.out.println("Enter numbers in array");
for(int i=0;i<100;i++)
{
arr[i]=sc.nextInt();
}
}
public int Indexofmin(int Startindex)
{
int min=arr[Startindex],p=Startindex;
for(int j=Startindex+1;j<100;j++)
{
if(arr[j]<min)
{
min=arr[j];
p=j;
}
}
return p;
}
public void selectionsort()
{
int i,pos,y;
for(i=0;i<99;i++)
{
pos=Indexofmin(i);
y=arr[pos];
arr[pos]=arr[i];
arr[i]=y;
}
}
public void displaylist()
{

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


{
System.out.println(arr[i]);
}
}
public static void main(String args[])
{
Sorter obj=new Sorter();
obj.readlist();
System.out.println("Original numbers are");
obj.displaylist();
obj.selectionsort();
System.out.println("Numbers is ascending order are");
obj.display();
}
}
DOUBLE DIMENSIONAL ARRAY

Q. Write a program to input data in double dimensional array and find


the largest and smallest number in array.

import java.util.*;
class D_display
{
int arr[][];
int r,c;
int max, min;
static Scanner sc=new Scanner(System.in);
public D_display(int rr,int cc)
{
r=rr;
c=cc;
max=0;
min=0;
arr=new int[r][c];
}
public void getdata()
{
int i,j;
System.out.println("Enter numbers in array");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
arr[i][j]=sc.nextInt();
}
}
}
public void arr_display()
{
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
System.out.print(arr[i][j]+"\t");
}
System.out.println();
}
}
public void find()
{
max=arr[0][0];
min=arr[0][0];
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(arr[i][j]>max)
max=arr[i][j];
if(arr[i][j]<min)
min=arr[i][j];
}
}
}
public void display()
{
System.out.println("largest number is : "+max);
System.out.println("smallest number is : "+min);
}
public static void main(String args[])
{
System.out.println("Eneter row and column of array");
int a=sc.nextInt();
int b=sc.nextInt();
D_display obj=new D_display(a,b);
obj.getdata();
System.out.println("The Matrix is");
obj.arr_display();
obj.find();
obj.display();
}
}
Q. Write a program to find the sum of all diagonal elements stored in
double dimensional array.
import java.util.*;
class D_diagonal
{
int arr[][];
int n;
int sum;
static Scanner sc=new Scanner(System.in);
public D_diagonal(int rr)
{
n=rr;
sum=0;
arr=new int[n][n];
}
public void getdata()
{
int i,j;
System.out.println("Enter numbers in array");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
arr[i][j]=sc.nextInt();
}
}
}
public void arr_display()
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
System.out.print(arr[i][j]+"\t");
}
System.out.println();
}
}
public void find()
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i==j||i+j==n-1)
sum=sum+arr[i][j];
}
}
}
public void display()
{
System.out.println("sum of diagonal elements is : "+sum);

}
public static void main(String args[])
{
System.out.println("Eneter size of array");
int a=sc.nextInt();
D_diagonal obj=new D_diagonal(a);
obj.getdata();
System.out.println("The Matrix is");
obj.arr_display();
obj.find();
obj.display();
}
}

You might also like