Program on address calculation in Single Dimensional array
import java.util.*;
class sda
{
public static void main(String args[])
{
int item,n;
Scanner s = new Scanner (System.in);
System.out.print( " Enter the size of the array ");
n=s.nextInt();
//int[] a; // declaration
//a=new int[n];// creation
int[] a= new int[n]; // declaration and creation
for(int i=0;i<n;i++)
{
a[i]=s.nextInt();
}
System.out.print("\n Array elements : ");
for(int i=0;i<n;i++)
{
System.out.print( a[i] + " ");
}
for(int i=0;i<n;i++)
{
System.out.printf("\n Address = %h ---- Array = a[%d] ---- Data = %d",i*4,i,a[i]);
}
System.out.print("\n Enter the Array element which u want : ");
item=s.nextInt();
for(int i=0;i<n;i++)
{
if(item == a[i])
System.out.printf("\n Address = %h ---- Array = a[%d] ---- Data = %d",i*4,i,a[i]);
}
}
}
output:
import java.util.*;
class sda
{
public static void main(String args[])
{
int item,n;
Scanner s = new Scanner (System.in);
System.out.print( " Enter the size of the array ");
n=s.nextInt();
//int[] a; // declaration
//a=new int[n];// creation
int[] a= new int[n]; // declaration and creation
for(int i=0;i<n;i++)
{
a[i]=s.nextInt();
}
System.out.print("\n Array elements : ");
for(int i=0;i<n;i++)
{
System.out.print( a[i] + " ");
}
for(int i=0;i<n;i++)
{
System.out.printf("\n Address = %h ---- Array = a[%d] ---- Data = %d",i*4,i,a[i]);
}
System.out.print("\n Enter the Array element which u want : ");
item=s.nextInt();
for(int i=0;i<n;i++)
{
if(item == a[i])
System.out.printf("\n Address = %h ---- Array = a[%d] ---- Data = %d",i*4,i,a[i]);
}
}
}
output:
No comments:
Post a Comment