Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package Java_core;
- import java.io.*;
- /**
- *
- * @author Tri
- */
- public class Array {
- static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
- public static void main(String[] args) throws IOException {
- int a[]= new int[100];
- System.out.print("Enter n: ");
- int n = Integer.parseInt(in.readLine());
- inputArray(a, n);
- showArray(a, n);
- showEven(a, n);
- showMain(a, n);
- prmSum(a, n);
- max(a, n);
- sort(a, n);
- minmod30(a, n);
- showArray(a, n);
- show3(a, n);
- //Nhap mang so nguyen n phan tu
- //Hien cac phan tu chan
- //Co bao nhieu phan tu chinh phuong
- //Tinh tong cac phan tu la so nguyen to
- //Tim gia tri lon nhat trong mang
- //Tim gia tri nho nhat chia het cho 3
- //Sap xep mang tang dan
- //Sap xep mang giam dan cac phan tu tan cung la 3
- }
- //FUNCTIONS
- static void inputArray(int a[], int n) throws IOException
- {
- System.out.println("Input the array a["+n+"]");
- for(int i=0; i<n; i++)
- {
- System.out.print("a["+ i +"]=");
- a[i]= Integer.parseInt(in.readLine());
- }
- System.out.println("");
- }
- static void showArray(int a[], int n)
- {
- System.out.print("The full array: ");
- for(int i=0; i < n; i++)
- {
- System.out.print(+a[i]+" ");
- }
- System.out.println("");
- }
- static void showEven(int a[], int n)
- {
- System.out.print("Even value(s) in the array: ");
- for(int i=0; i<n; i++)
- {
- if(a[i]%2==0) System.out.print(+a[i]+" ");
- }
- System.out.println("");
- }
- static void showMain(int a[], int n)
- {
- int cnt=0;
- // System.out.print("Main number(s) in the array: ");
- for(int i=0; i<n; i++)
- {
- if(a[i]%Math.sqrt(a[i])==0) cnt++;
- }
- System.out.println("There are "+cnt+" main number(s) in the array");
- }
- static boolean isPrm(int n)
- {
- boolean prm=true;
- if(n<2)return false;
- else
- {
- for(int i=2; i<n; i++)
- {
- if(n%i==0) return false;
- }
- return true;
- }
- }
- static void prmSum(int a[], int n)
- {
- System.out.print("The sum off all prime numbers: ");
- int sum = 0;
- for(int i=0; i<n; i++)
- {
- if(isPrm(a[i])==true) sum+=a[i];
- }
- System.out.println(+sum);
- }
- static void max(int a[], int n)
- {
- System.out.print("The greatest value in the array: ");
- int max=a[0];
- for(int i=0; i<n; i++)
- {
- if(a[i+1]>a[i]) max=a[i+1];
- }
- System.out.println(+max);
- }
- static void minmod30(int a[], int n)
- {
- System.out.print("The smallest value that mod 3 equal to 0: ");
- for(int i=0; i<n; i++)
- {
- if(a[i]%3==0)
- {
- System.out.println(+a[i]);
- break;
- }
- }
- System.out.println("");
- }
- static void sort(int a[], int n)
- {
- for(int i=0; i<n; i++)
- {
- for(int j=i+1; j<n; j++)
- {
- if(a[i] > a[j])
- {
- int temp = a[i];
- a[i] = a[j];
- a[j] = temp;
- }
- }
- }
- }
- static void show3(int a[], int n)
- {
- System.out.print("The integers end with 3 in the array sorted descendingly: ");
- for(int i=n-1; i>=0; i--)
- {
- }
- System.out.println("");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement