Sunteți pe pagina 1din 8

Array

A simple Program to demonstrate the functioning of 2D arrays :


class Arrays2D
{
static void test()
{
int m[][]={{1,5,6,9},{2,5,9,7},{15,5,78,9}};
for(int i=0;i<3;i++)
{
for(int j=0;j<4;j++)
{
System.out.print(m[i][j]+” “);
}
System.out.println();
}
}
}
Output of the above program :
WAP to find the HIGHEST and LOWEST
element from a 2D Array
import java.io.*;
class TwoDimeArray
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter the Dimensions of the Array :”);
int n=Integer.parseInt(br.readLine());
int m=Integer.parseInt(br.readLine());
int arr[][]=new int[n][m];
int i,j;
System.out.println(“Enter the Array Elements :”);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
arr[i][j]=Integer.parseInt(br.readLine());
}
}
int min=arr[0][0],max=arr[0][0];
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(arr[i][j]<min)
{
min=arr[i][j];
}

if(arr[i][j]>max)
{
max=arr[i][j];
}
}
}
System.out.println(“Lowest Element is “+min);
System.out.println(“Highest Element is “+max);
}
}

Special 2-Digit Number (ICSE 2014)


Sample: 59

5 + 9 = 14

5 * 9 = 45

14 + 45 = 59

import java.io.*;
class SpTwoDigit
{
public static void main(String[] a)throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter a 2-Digit number : “);
int n= Integer.parseInt(br.readLine());
int d,p,sum=0,pro=1,sp;
if(n>=10 && n<=99)
{
p=n;
while(n>0)
{
d=n%10;
n=n/10;
sum=sum+d;
pro=pro*d;
}
sp=sum+pro;
if(sp==p)
{
System.out.println(p+” is a Special 2-Digit number.”);
}
else
{
System.out.println(p+” is NOT a Special 2-Digit number.”);
}
}

else
{
System.out.println(n+” is an INVALID Input.”);
}
}
}
Bubble Sort
class Bubble
{
public static void main(String args[])
{
int a[]={90, 11, 15, 6, 56};
int temp=0, i,j;
for(i=0;i<5;i++)
{
for(j=0;j<4-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}

for(i=0;i<5;i++)
{
System.out.println(a[i]);
}
}
}
Selection Sort
class SelSort
{
public static void main(String a[])
{
int arr[]={56,11,48,9,1};
int i,j,max,temp;
for(i=4;i>=0;i–)
{
max=0;
for(j=0;j<=i;j++)
{
if(arr[j]>arr[max])
{
max=j;
}
}
temp=arr[max];
arr[max]=arr[i];
arr[i]=temp;
System.out.println(arr[i]);
}

for(i=0;i<5;i++)
{
System.out.print(arr[i]+” “);
}
}
}
Pattern 3
Input : JAVA

Output :

J A

J A V

J A V A

import java.io.*;

class Pattern3
{
public static void main(String a[])throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print(“Enter a Word : “);
String w = br.readLine();
for(int i=0;i<w.length();i++)
{
for(int j=0;j<=i;j++)
{
System.out.print(w.charAt(j)+” “);
}
System.out.println();
}
}
}

S-ar putea să vă placă și