Mảng 1 chiều và tìm phần tử lớn nhất:
[codes=java]import java.util.*;
import java.io.*;
class bt3
{
public static void main (String[] args)
{
int a[],n=0,i;
Scanner nhap=new Scanner(System.in);
System.out.print(“Nhap vao so phan tu n”);
n=nhap.nextInt();
a=new int[n+1];
for( i=0; i
System.out.print(“Nhap phan tu thu a[“+i+”]: “);
a[i]= nhap.nextInt();
}
for( i=0; i
System.out.print(“\t”+a[i]);
}
int max=a[0];
for( i=0; i
if(a[i]>max)
max=a[i];
} System.out.print(“\nMax la “+max);
}
}[/codes]
Mảng 2 chiều:
[codes=java]import java.util.*;
import java.io.*;
import java.lang.*;
class bt4
{
public static void main (String[] args)
{
Scanner nhap=new Scanner(System.in);
int a[][],i,j;
System.out.print(“Nhap so dong”); int dong=nhap.nextInt();
System.out.print(“Nhap so cot”); int cot=nhap.nextInt();
a=new int [dong][cot];
for(i=0; i
for(i=0; i
for(j=0;j
System.out.print(“\n”);
}
int s=0;
System.out.print(“Nhap vao dong thu k can tinh tong:”);
int k=nhap.nextInt();
for(i=0; i
System.out.print(“Tong dong la: “+s);
}
}[/codes]
Last modified on December 9th, 2020 at 12:57 am
Nam Le
lequocnam
0 responds