0% found this document useful (0 votes)
6 views7 pages

Level 3

Uploaded by

vkaruppasamy2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views7 pages

Level 3

Uploaded by

vkaruppasamy2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

public class arrays {

public static void main(String[] args) {


int[] a={1,2,3,4,1,3,4};
cop(a);
rev(a);
count(a);
int b[]= {6,4,5,6,7};
evenarray(b);
smallsumlarge(b);
ascending(b);
pat(5);
sim_pat(5);
rec(5);
rec2(5);
pattern(5);
int c[]= {1,2,3,4,5,6,7,8,9};
alternative(c);
int i[][]={{1, 2}, {3, 4}};
int p[][]= {{1, 1}, {1, 1}};
multiply(i,p);
System.out.println();
add(i,p);
System.out.println();
sub(i,p);
}
static void cop(int a[]){
int[] b=new int[a.length];
int size=0;
for(int c:a) {
b[size]=c;
System.out.print(b[size++]);
}
System.out.println();
}
static void rev(int a[]){
int size=a.length-1;
for(int c:a) {

System.out.print(a[size--]);
}
System.out.println();
}
static void count(int a[]){
for(int i=0;i<a.length;i++) {
int count=0;
for(int j=i+1;j<a.length;j++) {
if(a[i]==a[j]) {
count++;
}
}
if(count>0) {
System.out.print(a[i]);
}

}
System.out.println();
}
static void evenarray(int a[]) {
for(int i=0;i<a.length;i++) {
System.out.println((i%2==0)?"even="+a[i]+"\n":"odd="+a[i]+"\n");
}
}
static void smallsumlarge(int a[]) {
int small=Integer.MAX_VALUE;
int large=Integer.MAX_VALUE;
int total=0;
for(int c:a) {
if(c<small) {
small=c;
}
if(c>large) {
large=c;
}
total+=c;
}
System.out.println("min:"+small+"\nlarge: "+large+"\ntotal"+total);
}
static void ascending(int a[]) {
for(int i=0;i<a.length;i++) {
for(int j=i+1;j<a.length;j++) {
if(a[i]>a[j]) {
int first=a[i];
int second=a[j];
int third=first;
a[i]=second;
a[j]=third;
}

}
}
System.out.println("ascending order");

for(int c:a) {

System.out.print(c);
}
System.out.println();
for(int i=0;i<a.length;i++) {
for(int j=i+1;j<a.length;j++) {
if(a[i]<a[j]) {
int first=a[i];
int second=a[j];
int third=first;
a[i]=second;
a[j]=third;
}

}
}
System.out.println("descending order");
for(int c:a) {

System.out.print(c);
}
System.out.println();
}
static void pat(int a) {
for(int i=1;i<=a;i++) {
for(int sp=0;sp<a-i;sp++) {
System.out.print(" ");
}
for(int j=0;j<i;j++) {
System.out.print("* ");
}
System.out.println();
}
System.out.println();
System.out.println();
for(int i=a;i>0;i--) {
for(int sp=0;sp<a-i;sp++) {
System.out.print(" ");
}
for(int j=0;j<i;j++) {
System.out.print("* ");
}
System.out.println();
}
}
static void sim_pat(int a) {
for(int i=0;i<a;i++) {
for(int sp=0;sp<=i;sp++) {
System.out.print("* ");
}
System.out.println();
}
System.out.println();
}
//recursion lower triangle
static void rec(int a) {
int l=a;
if(a>0) {
for(int i=0;i<a;i++) {
System.out.print("* ");
}
System.out.println();
rec(a-1);
}
}
//recursion my logic upper triangle
static void rec2(int a) {
int n=0;
inner(n,a);
}

static void inner(int n,int a) {


if(n<a) {
for(int i=0;i<=n;i++) {
System.out.print("* ");
}
System.out.println();
inner(n+1,a);
}

}
//recursion chatgpt
static void star(int n) {
if(n==0) {
return;

}
System.out.print("* ");

star(n-1);

}
static void pattern(int a) {
if(a==0) {
return;

}
pattern(a-1);
star(a);
System.out.println();

}
static void alternative(int a[]) {
for(int i=0;i<a.length/2;i++) {
System.out.print(a[i]+" "+a[(a.length-1)-i]+" ");
}
System.out.println((a.length%2==0)?"":a[(a.length/2)]);
}
static void multiply(int a[][],int b[][]) {
int c[][]=new int[a.length][a[0].length];
for(int i=0;i<a.length;i++) {
for(int j=0;j<a.length;j++) {
for(int w=0;w<a.length;w++) {
c[i][j]+=a[i][w]*b[w][j];
}
}
}
for(int[] h:c) {
for(int k:h) {
System.out.print(k+" ");
}
System.out.println();
}

}
static void add(int a[][],int b[][]) {
int c[][]=new int[a.length][a[0].length];
for(int i=0;i<a.length;i++) {
for(int j=0;j<a.length;j++) {

c[i][j]=a[i][j]+b[i][j];
}
}

for(int[] h:c) {
for(int k:h) {
System.out.print(k+" ");
}
System.out.println();
}
}
static void sub(int a[][],int b[][]) {
int c[][]=new int[a.length][a[0].length];
for(int i=0;i<a.length;i++) {
for(int j=0;j<a.length;j++) {

c[i][j]=a[i][j]-b[i][j];
}
}

for(int[] h:c) {
for(int k:h) {
System.out.print(k+" ");
}
System.out.println();
}

}}
Output:

1234134
4314321
134
even=6
odd=4
even=5
odd=6
even=7
min:4
large: 2147483647
total28
ascending order
45667
descending order
76654
*
**
***
****
*****
*****
****
***
**
*
*
**
***
****
*****
*****
****
***
**
*
*
**
***
****
*****
*
**
***
****
*****
192837465
33
77

23
45

01
23

You might also like