Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.Arrays;
- public class BigProject1 {
- //method to print the array delimited by spaces
- public static int[] print(int[] arr) {
- for(int i=0; i<arr.length; i++) {
- System.out.print(arr[i]+ " ");
- }
- return arr;
- }
- //method to sort the array
- public static int[] sorted(int[] arr2) {
- for(int o=0; o<arr2.length; o++) {
- Arrays.sort(arr2);
- }
- return arr2;
- }
- //method to count the number of different elements in the array
- public static int different(int[] arr3) {
- if(arr3.length==0) {
- return 0;
- }
- int counter=1;
- for(int x=1; x<arr3.length; x++) {
- if(arr3[x]!=arr3[x-1]) {
- counter++;
- }
- }
- return counter;
- }
- /*public static int[] pals(int[] arr4){
- int count2=0;
- for(int i=0; i<arr4.length; i++) {
- int n= arr4[i];
- int rev=0;
- int tmp=0;
- while(n>0) {
- tmp=n%10;
- n=n/10;
- rev=rev*10+tmp;
- }
- if(arr4[i]==rev) {
- count2++;
- }
- else {
- System.out.print("");
- }
- }
- return arr4;
- }*/
- public static void main(String[] args) {
- /*prompts user for the length of their array, initializes an array with that
- * length, loops through all the elements the user inputs, and stores every
- * element in the array, then calling the print method to print the array
- */
- Scanner scan= new Scanner(System.in);
- System.out.print("Enter the length of the array: ");
- int len= scan.nextInt();
- int[] arr= new int[len];
- for(int i=0; i<arr.length; i++) {
- arr[i]=scan.nextInt();
- }
- print(arr);
- //printing the length of the array again for the first objective
- System.out.println("\nNumber of elements:"+arr.length);
- /*initializing a variable at position 0 of the array, loops through all the
- * elements of the array, finds the largest element and prints it
- */
- int max1=arr[0];
- for (int j=0; j<len; j++) {
- if (max1 < arr[j]) {
- max1 = arr[j];
- }
- }
- System.out.println("Largest number: " +max1);
- //calling the print and sort methods to print the sorted array
- print(sorted(arr));
- /*Initializing two variables,looping through all elements of the array using
- *if statements to check which value is the smallest and which is the
- *second smallest and printing the second smallest element
- */
- int min=Integer.MAX_VALUE;
- int min2=Integer.MAX_VALUE;
- for(int k=0; k<arr.length; k++) {
- if(arr[k]==min) {
- min2=min;
- }
- if(arr[k]<min) {
- min2=min;
- min=arr[k];
- }
- else if(arr[k]<min2) {
- min2=arr[k];
- }
- }
- //System.out.print("\nSmallest number:"+min);
- System.out.print("\nSecond smallest number: "+min2);
- //counting all even numbers and printing that number
- int count=0;
- for(int l=0; l<arr.length; l++) {
- if(arr[l]%2==0) {
- count++;
- }
- }
- System.out.println("\nNumber of evens: "+count);
- //counting all odd numbers and printing that number
- int count1=0;
- for(int m=0; m<arr.length; m++) {
- if(arr[m]%2!=0) {
- count1++;
- }
- }
- System.out.println("Number of odds: "+count1);
- /*initializing a variable with 0, looping through the elements in the array
- * setting the variable to be itself plus the element in each position of
- * array, printing the sum of all elements
- */
- int sum=0;
- for(int q=0; q<arr.length; q++) {
- sum=sum+arr[q];
- }
- System.out.println("Sum of all numbers: "+sum);
- /*reversing the numbers in the array and comparing the original number
- * with the reversed one, printing the number of numbers with equal
- * reversed and original forms
- */
- int[] palarr=new int[len];
- int index=0;
- int count2=0;
- for(int i=0; i<arr.length; i++) {
- int n= arr[i];
- int rev=0;
- int tmp=0;
- while(n>0) {
- tmp=n%10;
- n=n/10;
- rev=rev*10+tmp;
- }
- if(arr[i]==rev) {
- count2++;
- arr[i]=palarr[index];
- index++;
- }
- else {
- System.out.print("");
- }
- }
- print(palarr);
- System.out.println("\nNumber of palindromic numbers: "+count2);
- //calling the method that finds the number of different elements and printing
- System.out.println("Number of different elements: "+different(arr));
- /*int count3=1;
- for(int x=1; x<arr.length; x++) {
- if(arr[x]==arr[x-1]){
- count3++;
- //System.out.print("Frequency of: "+arr[x]+"is"+count3/arr.length*100+"%");
- System.out.println(count3);
- }
- }*/
- //printing average of all elements by taking the sum and dividing by the length
- System.out.println("Average of all numbers: "+sum/len);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement