Arrays Hands-Ons [Java > Arrays] | Accenture TFA Training Pre-Learning Modules
Disclaimer: The primary purpose of providing this solution is to assist and support anyone who are unable to complete these courses due to a technical issue or a lack of expertise. This website's information or data are solely for the purpose of knowledge and education.
Make an effort to understand these solutions and apply them to your Hands-On difficulties. (It is not advisable that copy and paste these solutions).
This page contains all the Hands-Ons, which is asked on the topic "Java > Arrays" for the TFA Training module.
All Question of the Hands-On Present Below for Ease Use Ctrl + F with the question name to find the Question. All the Best!
1. Alternate Number Difference
Path: JAVA/Arrays/Alternate Numbers Difference/Alternate Numbers Difference/Main.java
import java.util.Scanner;
public class Main{ public static void main (String[] args) { Scanner sc = new Scanner(System.in);
int arr_size = sc.nextInt(); if(arr_size <= 3){ System.out.println("Invalid array size"); System.exit(0); } int[] arr_int = new int[arr_size]; int big_diff = 0; int small_index = 0; for(int i = 0; i < arr_size; i++) arr_int[i] = sc.nextInt(); //if(arr_size % 2 == 0){ for(int j = 0; j < arr_size - 2; j++){ if(Math.abs(arr_int[j] - arr_int[j+2]) > big_diff){ big_diff = Math.abs(arr_int[j] - arr_int[j+2]); if(arr_int[j] <= arr_int[j+2]) small_index = j; else small_index = j + 2; } } //} // else{ // for(int k = 0; k <= arr_size - 2; k++){ // if(Math.abs(arr_int[k] - arr_int[k+2]) > big_diff){ // big_diff = Math.abs(arr_int[k] - arr_int[k+2]); // if(arr_int[k] <= arr_int[k+2]) // small_index = k; // else // small_index = k + 2; // } // } // } System.out.println(small_index); } }
2. Array Compatibility
Path: JAVA/Arrays/Array Compatiblilty/CompatibleArrays.java
import java.util.*;
public class CompatibleArrays
{ public static void main (String[] args) { Scanner sc=new Scanner(System.in); //1st array System.out.println("Enter the size for First array:"); int n1=sc.nextInt(); int flag=0; if(n1>0) { int[] arr1=new int[n1]; System.out.println("Enter the elements for First array:"); for(int i=0;i<n1;i++) { arr1[i]=sc.nextInt(); } //2nd array System.out.println("Enter the size for Second array:"); int n2=sc.nextInt(); if(n2>0) { int[] arr2=new int[n2]; System.out.println("Enter the elements for Second array:"); for(int i=0;i<n2;i++) { arr2[i]=sc.nextInt(); } if(n1==n2) { for(int i=0; i<n1;i++) { if(arr1[i]>=arr2[i]) { flag++; } } if(flag==n1) { System.out.println("Arrays are Compatible"); } else { System.out.println("Arrays are not Compatible"); } } else { System.out.println("Arrays are Not Compatible"); } } else { System.out.println("Invalid array size"); } } else { System.out.println("Invalid array size"); } } }
3. Array Square
Path: JAVA/Arrays/Array square/Array square/Main.java
import java.util.*;
public class Main {
public static void main (String[] args) { int i=0; Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int arr[]=new int[n]; for(i=0;i<n;i++) { arr[i]=sc.nextInt(); } int arr2[]=new int[n]; for(i=0;i<n;i++) { for(int j=0;j<n;j++) { if(i!=j) { arr2[i]+=arr[j]*arr[j]; } } } for(i=0;i<n;i++) { System.out.println(arr2[i]); } } }
4. Average and Grade Calculation
Path: JAVA/Arrays/Average and Grade Calculation/Student.java
public class Student
{ private int id; private String name; private int[] marks; private float average; private char grade; public void setId(int id) { this.id=id; } public int getId() { return this.id; } public void setName(String name) { this.name=name; } public String getName() { return this.name; } public void setMarks(int[] marks) { this.marks=marks; } public int[] getMarks() { return this.marks; } public void calculateAvg() { float sum=0; for(int i=0;i<this.marks.length;i++) { sum+=this.marks[i]; } average=(float)(sum/(this.marks.length)); setAverage(average); } public void findGrade() {int flag=0; for(int i=0;i<this.marks.length;i++) { if(this.marks[i]<50) { flag++; } } if(flag>0) { grade='F'; } else { if(this.average>=80 && this.average<=100) { grade='O'; } else if(this.average>=50 && this.average<=79) { grade='A'; } else { grade='F'; } } setGrade(grade); } public void setAverage(float average) { this.average=average; } public void setGrade(char grade) { this.grade=grade; } public float getAverage() { return this.average; } public char getGrade() { return this.grade; } }
Path: JAVA/Arrays/Average and Grade Calculation/StudentMain.java
import java.util.*;
public class StudentMain { public static void main (String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter the id:"); int id=sc.nextInt(); sc.nextLine(); System.out.println("Enter the name:"); String name=sc.nextLine(); System.out.println("Enter the number of subjects:"); int n=sc.nextInt(); int marks[]=new int[n]; for(int i=0;i<n;i++) { System.out.println("Enter mark for subject "+(i+1)+":"); marks[i] = sc.nextInt(); } Student obj=new Student(); obj.setName(name); obj.setId(id); obj.setMarks(marks); obj.calculateAvg(); obj.findGrade(); System.out.println("Id:"+obj.getId()); System.out.println("Name:"+obj.getName()); System.out.println("Average:"+obj.getAverage()); System.out.println("Grade:"+obj.getGrade()); } }
5. Country wise Population
Path: JAVA/Arrays/Country wise population/Main.java
import java.util.*;
public class Main{
public static void main(String[] args){
int size;
Scanner sc = new Scanner(System.in);
size=sc.nextInt();
sc.nextLine();
String[] country_name=new String[size];
int pop[]=new int[size];
for(int i=0;i<size;i++)
country_name[i]=sc.nextLine();
for(int i=0;i<size;i++)
{
pop[i]=sc.nextInt();
sc.nextLine();
}
int temp[]=Arrays.copyOf(pop,size);
Arrays.sort(pop);
for(int i=0;i<size;i++)
for(int j=0;j<size;j++)
if(temp[j]==pop[i])
System.out.println(country_name[j]);
for(int i:pop)
System.out.println(i);
}
}
6. Cumulative sum in an array
Path: JAVA/Arrays/Cumulative sum in an array/CumulativeSum.java
import java.util.*; public class CumulativeSum{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); System.out.println("Enter the number of elements"); int n=sc.nextInt(); if(n>0 && n<21){ int[] a=new int[n]; System.out.println("Enter the elements"); for(int i=0;i<n;i++) a[i]=sc.nextInt(); int sum=0; for(int i=0;i<n;i++){ sum+=a[i]; System.out.print(sum+" "); } } else{ System.out.println("Invalid Range"); } } }7. Display Date
Path: JAVA/Arrays/Display Date/Main.java
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); Main obj = new Main(); int num=0,no=0; int yr; System.out.println("Enter 3 integers for the day of the month, month and year"); num = sc.nextInt(); no = sc.nextInt(); yr = sc.nextInt(); String suff = obj.extension(num); String mont = obj.month(no); System.out.println(suff+" "+mont+" "+yr); } String extension(int num) { String[] suffix = {"th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"}; int n = num%100; return String.valueOf(num)+suffix[(n > 3 && n < 21) ? 0 : (n%10)]; } String month(int no) { String[] mon = {"January","February","March","April","May","June","July","August","September","October","November","December"}; return mon[no-1]; } }
8. Display array Values in reverse order
Path: JAVA/Arrays/Display array values in reverse order/Main.java
import java.util.Scanner; public class Main { public static void main(String[] args) { int n, res,i,j=0; Scanner s = new Scanner(System.in); System.out.print("Enter the array size \n"); n = s.nextInt(); int array[] = new int[n]; int rev[] = new int[n]; System.out.println("Enter the values"); for( i=0; i < n; i++) { array[i] = s.nextInt(); } for( i=n;i>0 ; i--,j++) { rev[j] = array[i-1]; System.out.println(rev[j]); } } }
9. Display array values
Path: JAVA/Arrays/Display array values/Main.java
import java.util.Scanner; public class Main { public static void main(String args[]) { Scanner in = new Scanner(System.in); System.out.println("Enter the array size"); int n = in.nextInt(); int arr[] = new int[n]; System.out.println("Enter the values"); for (int i = 0; i < n; i++) { arr[i] = in.nextInt(); } long sum = 0; for (int i = 0; i < n; i++) { System.out.println(+ arr[i]); } } }
10. Duplicate an element
Path: JAVA/Arrays/Duplicate an element/DuplicateElement.java
import java.util.*; public class DuplicateElement{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); System.out.println("Enter the size of an array:"); int n=sc.nextInt(); if(n>0){ int[] a=new int[n+1]; System.out.println("Enter the array elements:"); for(int i=0;i<n;i++){ a[i]=sc.nextInt(); } System.out.println("Enter the position of the element to be replicated:"); int k=sc.nextInt(); if(k<n){ a[n]=a[k]; for(int i=0;i<=n;i++){ System.out.println(a[i]); } } else{ System.out.println("Position is greater than the size of an array"); } } else{ System.out.println("Invalid array size"); } } }
11. Find Average Age
Path: JAVA/Arrays/Find Average Age/Test.java
import java.util.*; import java.text.DecimalFormat; public class Test { double calculateAverage(int[] age) { int len=age.length; double sum=0.0; for(int i=0;i<len;i++) { sum+=age[i]; } double avg=sum/len; return avg; } public static void main (String[] args) { Scanner sc =new Scanner(System.in); Test obj=new Test(); System.out.println("Enter total no.of employees:"); int n=sc.nextInt(); int flag=0; if(n>1) { int[] age=new int[n]; System.out.println("Enter the age for "+n+" employees:"); for(int i=0;i<n;i++) { int temp=sc.nextInt(); if(temp>=28 && temp<=40) { age[i]=temp; } else { System.out.println("Invalid age encountered!"); flag++; break; } } if(flag==0) { DecimalFormat df=new DecimalFormat("####.00"); System.out.println("The average age is "+df.format(obj.calculateAverage(age))); } } else { System.out.println("Please enter a valid employee count"); } } }
12. Find_Display the position of a number
Path: JAVA/Arrays/Find _ Display the position of a number/Main.java
import java.util.Scanner; public class Main { public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.println("Enter the array size"); int n=sc.nextInt(); System.out.println("Enter the values"); int[] arr=new int[50]; int i; for(i=0;i<n;i++) { arr[i]=sc.nextInt(); } System.out.println("Enter the number to find"); int f=sc.nextInt(); int flag=0; for(i=0;i<n;i++) { if(f==arr[i]) { flag=1; break; } } if(flag==1) { i=i+1; System.out.println(i); } else System.out.println(0); //Fill the code here } }
13. Find the player Details
Path: JAVA/Arrays/Find the Player Details/Main.java
import java.util.Scanner; public class Main { public static void main(String[] args) { Player[] pObj=new Player[4]; pObj[0]=new Player(146,"Athul",16,9876543210l); pObj[1]=new Player(135,"Rakshana",16,9956231045l); pObj[2]=new Player(168,"Christy",16,9856471230l); pObj[3]=new Player(194,"Athulya",16,9768541230l); Scanner sc=new Scanner(System.in); // Invoke the method findPlayerDetailsById in the PlayerUtility class and display results System.out.println("Enter the id to be searched"); int n=sc.nextInt(); PlayerUtility p=new PlayerUtility(); Player r=p.findPlayerDetailsById(pObj, n); if(r!=null){ System.out.println("Name:"+r.getPlayerName()); System.out.println("Phone number:"+r.getPhoneNumber()); } else{ System.out.println("No player found"); } } }Path: JAVA/Arrays/Find the Player Details/Player.java
//Don't change the code public class Player { private int playerId; private String playerName; private int age; private long phoneNumber; public int getPlayerId() { return playerId; } public void setPlayerId(int playerId) { this.playerId = playerId; } public String getPlayerName() { return playerName; } public void setPlayerName(String playerName) { this.playerName = playerName; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public long getPhoneNumber() { return phoneNumber; } public void setPhoneNumber(long phoneNumber) { this.phoneNumber = phoneNumber; } public Player(int playerId, String playerName, int age, long phoneNumber) { super(); this.playerId = playerId; this.playerName = playerName; this.age = age; this.phoneNumber = phoneNumber; } }Path: JAVA/Arrays/Find the Player Details/PlayerUtility.java
public class PlayerUtility { public Player findPlayerDetailsById(Player[] arr, int playerIdToSearch){ //Fill the code here for(int i=0;i<arr.length;i++){ if(arr[i].getPlayerId()==playerIdToSearch){ return arr[i]; } } return null; } }14. Find the highest mark
Path: JAVA/Arrays/Find the highest mark/Highestmark.java
import java.util.Scanner; public class Highestmark{ static int findMax(int arr[], int low, int high) { int max = arr[low]; for(int i=low; i<=high; i++) { if(arr[i] > max) max = arr[i]; } return max; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int arr[]= new int[6]; for(int j=0;j<6;j++) { arr[j] = sc.nextInt(); if(arr[j]<0) { System.out.println("Invalid Mark"); return; } } System.out.println("Highest mark is "+findMax(arr,0,5)); } }15. Generate number using odd digits
Path: JAVA/Arrays/Generate number using odd digits/Generate number using odd digits/Main.java
import java.util.Scanner; public class Main { public static void main (String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter the size of an array:"); int n=sc.nextInt(); if(n>0) { int arr[]=new int[n]; System.out.println("Enter the array elements:"); for(int i=0;i<n;i++) { arr[i]=sc.nextInt(); } String output=""; for(int j=0;j<n;j++) { if(arr[j]>=0&&arr[j]<10) { if(arr[j]%2!=0) { output=output.concat(String.valueOf(arr[j])); } } } if(output.equals("")) System.out.println("Single digit odd number is not found in the array"); else System.out.println(output); } else { System.out.println("Invalid array size"); } } }16. Highest Mark in Each Semester
Path: JAVA/Arrays/Highest Mark in Each Semester/HighestMarkPerSem.java
import java.util.Scanner; public class HighestMarkPerSem{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); System.out.println("Enter no of semester:"); int n=sc.nextInt(); int[] a=new int[n]; int flag=0; for(int i=0;i<n;i++){ System.out.println("Enter no of subjects in "+(i+1)+" semester:"); a[i]=sc.nextInt(); } int[] max=new int[n]; for(int i=0;i<n;i++){ int mx=0; int[] b=new int[a[i]]; System.out.println("Marks obtained in semester "+(i+1)+":"); for(int j=0;j<a[i];j++){ b[j]=sc.nextInt(); if(b[j]<0 || b[j]>100){ flag=1; break; } if(mx<b[j]) mx=b[j]; } if(flag==1){ break; } max[i]=mx; } if(flag==0) for(int i=0;i<n;i++){ System.out.println("Maximum mark in "+(i+1)+" semester:"+max[i]); } else{ System.out.println("You have entered invalid mark."); } } }17. Interchange the elements of an array
Path: JAVA/Arrays/Interchange the elements of an array/InterchangeArray.java
import java.util.Scanner; public class InterchangeArray{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int Size1,Size2, i ; System.out.println("Enter the number of elements in the first array :"); Size1 = sc.nextInt(); if(Size1<=0) { System.out.println("Invalid range"); return; } int [] a = new int[Size1]; System.out.println("Enter the elements in the first array"); for(i=0;i<Size1;i++) { a[i] = sc.nextInt(); if(a[i]<=0) { System.out.println("Invalid range"); return; } } System.out.println("Enter the number of elements in the second array :"); Size2 = sc.nextInt(); if(Size2<=0) { System.out.println("Invalid Range"); return; } if(Size1 != Size2) { System.out.println("Unable to swap size differs"); return; } int[] b = new int[Size2]; System.out.println("Enter the elements in the second array"); for(i=0;i<Size2;i++) { b[i] = sc.nextInt(); if(b[i]<=0) { System.out.println("Invalid range"); return; } } for(i=0;i<Size1;i++) { a[i] = a[i] + b[i]; b[i] = a[i] - b[i]; a[i] = a[i] - b[i]; } System.out.println("The first array after swapping is :"); printAttay(a,Size1); System.out.println("\nThe second array after swapping is :"); printAttay(b,Size2); } public static void printAttay(int[] arr, int size) { for(int x : arr) System.out.print(x+" "); } }19. Make a number
Path: JAVA/Arrays/Make a number/EvenNumber.java
import java.util.*; import java.lang.*; class EvenNumber{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); System.out.println("Enter the size of an array:"); int size=sc.nextInt(); if(size>0){ int a[] =new int[size]; ArrayList<Integer> a1 = new ArrayList<Integer>(); System.out.println("Enter the array elements:"); for(int i=0;i<size;i++) { a[i]=sc.nextInt(); if(a[i]%2==0 && a[i]>=2 && a[i]<=8) a1.add(a[i]); } if(a1.size()!=0) { for(int i=0;i<a1.size();i++){ System.out.print(a1.get(i)); } } else{ System.out.println("Single digit even number is not found in the array"); } } else{ System.out.println("Invalid array size"); } } }20. Marathon Data Analysis
Path: JAVA/Arrays/Marathon Data Analysis (2D Array)/Main.java
import java.util.*; import java.text.DecimalFormat; public class Main{ static double[][] arr2D = new double[4][7]; static String[] names = {"David","Charles","Antony","Leo"}; static double sum=0,sum1=0,sum2=0,sum3=0; static double avg=0,avg1=0,avg2=0,avg3=0,avg4=0,avg5=0,avg6=0; public static void main(String[] args){ Scanner sc = new Scanner(System.in); Main obj = new Main(); for(int i=0;i<4;i++) { for(int j=0;j<7;j++) { arr2D[i][j]=sc.nextDouble(); } } System.out.println("Total Distance covered by each participant:"); obj.totalDis(); obj.avgDis(); obj.display(); } void avgDis() { int flag1=0,j; for(int i=0;i<7;i++) { for(j=0;j<4;j++) { if(flag1==0) avg+=arr2D[j][i]; if(flag1==1) avg1+=arr2D[j][i]; if(flag1==2) avg2+=arr2D[j][i]; if(flag1==3) avg3+=arr2D[j][i]; if(flag1==4) avg4+=arr2D[j][i]; if(flag1==5) avg5+=arr2D[j][i]; if(flag1==6) avg6+=arr2D[j][i]; } flag1++; } } void totalDis() { int flag=0,j; for(int i=0;i<4;i++) { for(j=0;j<7;j++) { if(flag==0) sum=sum+arr2D[i][j]; if(flag==1) sum1=sum1+arr2D[i][j]; if(flag==2) sum2=sum2+arr2D[i][j]; if(flag==3) sum3=sum3+arr2D[i][j]; } flag++; } } void display() { int i=0; System.out.println(names[i++]+" "+(new DecimalFormat("0.00").format(sum))+" kms"); System.out.println(names[i++]+" "+(new DecimalFormat("0.00").format(sum1))+" kms"); System.out.println(names[i++]+" "+(new DecimalFormat("0.00").format(sum2))+" kms"); System.out.println(names[i]+" "+(new DecimalFormat("0.00").format(sum3))+" kms"); System.out.println("Average distance covered on each day:"); int j=4; i=0; while(i!=7) { System.out.println("Day1:"+(new DecimalFormat("0.00").format(avg/j))+" kms"); i++; System.out.println("Day2:"+(new DecimalFormat("0.00").format(avg1/j))+" kms"); i++; System.out.println("Day3:"+(new DecimalFormat("0.00").format(avg2/j))+" kms"); i++; System.out.println("Day4:"+(new DecimalFormat("0.00").format(avg3/j))+" kms"); i++; System.out.println("Day5:"+(new DecimalFormat("0.00").format(avg4/j))+" kms"); i++; System.out.println("Day6:"+(new DecimalFormat("0.00").format(avg5/j))+" kms"); i++; System.out.println("Day7:"+(new DecimalFormat("0.00").format(avg6/j))+" kms"); i++; } } }21. Mark Comparison
Path: JAVA/Arrays/Mark Comparison/Mark Comparison/Main.java
import java.util.*; public class Main { public static void main (String[] args){ Scanner sc=new Scanner(System.in); int f=sc.nextInt(); int flag=0; int fa[]=new int[20]; for(int i=1;i<=f;i++) { fa[i]=sc.nextInt(); if(fa[i]<0) { System.out.println("No Negative Elements"); System.exit(0); } } int s=sc.nextInt(); int []sa=new int[20]; if(f!=s){ System.out.println("Invalid array size"); } else { for(int i=1;i<=s;i++){ sa[i]=sc.nextInt(); if(sa[i]<0){ System.out.println("No Negative Elements"); System.exit(0); } for(int k=1;k<=f;k++){ for(int j=1;j<=s;j++){ if(fa[k]==sa[j]){ flag=1; System.out.println("("+k+","+j+")"); } } } } if(flag==0){ System.out.println("No matching scores"); } } } }22. Maximum Difference
Path: JAVA/Arrays/Maximum Difference/Main.java
import java.util.*; public class Main{ public static void main (String[] args) { Scanner sc = new Scanner(System.in); int num=sc.nextInt(); sc.nextLine(); if(num<=0){ System.out.println("Invalid"); return; } int input[]=new int[num]; for(int i=0;i<num;i++){ input[i]=sc.nextInt(); sc.nextLine(); } for(int i:input) if(i<0){ System.out.println("Invalid"); return; } int diff[][]=new int[num][num]; for(int i=0;i<num;i++){ for(int j=0;j<num;j++){ diff[i][j]=Math.abs(input[i]-input[j]); } } int max=Integer.MIN_VALUE; for(int i=0;i<num;i++){ for(int j=0;j<num;j++){ if(diff[i][j]>max) max=diff[i][j]; } } for(int i=0;i<num;i++){ for(int j=0;j<num;j++){ if(diff[i][j]==max){ System.out.println("("+(i)+","+(j)+")"); return; } } } } }23. Maximum repeated number
Path: JAVA/Arrays/Maximum repeated number/Main.java
import java.util.Scanner; public class Main{ public static void main (String[] args) { int max=0,count=0,tmpcount=0,size; Scanner sc=new Scanner(System.in); size=sc.nextInt(); sc.nextLine(); if(size<=0){ System.exit(0); } int[] a=new int[size]; for(int i=0;i<size;i++){ a[i]=sc.nextInt(); sc.nextLine(); } for(int i=0;i<size;i++){ for(int j=0;j<size;j++){ if(a[i]==a[j]){ tmpcount++; } } if(tmpcount>=count){ if(a[i]<max&&tmpcount==count){ count=tmpcount; }else{ max=a[i]; count=tmpcount; } } tmpcount=0; } System.out.println(max); } }24. Next Greatest number
Path: JAVA/Arrays/Next Greatest number/Next Greatest number/Main.java
import java.util.Scanner; import java.util.Arrays; public class Main{ static void swap(char ar[], int i, int j){ char temp = ar[i]; ar[i] = ar[j]; ar[j] = temp; } static void findNextGreatest(char ar[], int n){ int i; for(i = n -1; i > 0; i--){ if(ar[i] > ar[i-1]) break; } if(i == 0){ for(int j = 0; j < ar.length; j++) System.out.print(ar[j]); } else{ int x = ar[i - 1], min = i; for(int k = i + 1; k < n; k++){ if(ar[k] > x && ar[k] < ar[min]) min = k; } swap(ar, i-1, min); Arrays.sort(ar, i, n); for(int l = 0; l < ar.length; l++) System.out.print(ar[l]); } } public static void main (String[] args) { Scanner sc = new Scanner(System.in); String input = sc.next(); char arr[] = input.toCharArray(); findNextGreatest(arr, arr.length); } }25. Numerology
Path: JAVA/Arrays/Numerology/Numerology.java
import java.util.*; public class Numerology { char[][] num_val={{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H','I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}, {1, 2, 3, 4, 5, 8, 3, 5, 1, 1, 2, 3, 4, 5, 7, 8, 1, 2, 3, 4, 6, 6, 6, 5, 1, 7}}; public char numerological_value(char chr) { for(int i=0;i<26;i++) { if(chr==(num_val[0][i])) { return num_val[1][i]; } } return 0; } public static void main (String[] args) { Scanner sc=new Scanner(System.in); Numerology obj=new Numerology(); System.out.println("Enter your name:"); String name=sc.nextLine(); int len=name.length(); int flag=0, val=0; for(int i=0; i<len;i++) { char r=name.charAt(i); int temp=(obj.numerological_value(r)); if(temp==0) { System.out.println("Invalid name"); flag++; break; } else { val+=(int)temp; } } if(flag==0) { System.out.println("Your numerology no is:"+val); } } }26. Pass and Fail count
Path: JAVA/Arrays/Pass and Fail Count/Pass and Fail Count/Count.java
import java.util.*; public class Count { public static void main (String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter the no of subjects:"); int no_sub=sc.nextInt(); if(no_sub>0 && no_sub<=20) { int marks[]=new int[no_sub]; for(int i=0;i<no_sub;i++) { marks[i]=sc.nextInt(); } int pass=0,fail=0; for(int i=0;i<no_sub;i++) { if(marks[i]<50) { fail++; } else { pass++; } } if(fail==0) { System.out.println("Ram passed in all subjects"); } else if(pass==0) { System.out.println("Ram failed in all subjects"); } else { System.out.println("Ram passed in "+pass+" subjects and failed in "+fail+" subjects"); } } else { System.out.println("Invalid input range"); } } }27. Retail Shop
Path: JAVA/Arrays/Retail Shop/Shop.java
import java.util.*; public class Shop { private String shopName; private String shopAddress; private String[] products; public Shop(String shopName, String shopAddress, String[] products) { this.shopName=shopName; this.shopAddress=shopAddress; this.products=products; } public boolean checkProductAvailability(String productname) {int len=products.length; for(int i=0;i<len;i++) { if(productname.equalsIgnoreCase(products[i])) { return true; } } return false; } /* public void setShopName(String shopName) { this.shopName=shopName; }*/ public String getShopName() { return this.shopName; } /*public void setShopAddress(String shopAddress) { this.shopAddress=shopAddress; }*/ public String getShopAddress() { return this.shopAddress; } /*public void setProducts(String[] products) { this.products=products; }*/ public String[] getProducts() { return this.products; } public static void main (String[] args) { Scanner sc=new Scanner(System.in); //Shop obj=new Shop(); System.out.println("Enter the shopname:"); String shop_name=sc.nextLine(); System.out.println("Enter the address:"); String shop_address=sc.nextLine(); System.out.println("Enter number of products:"); int no_products=sc.nextInt(); sc.nextLine(); String[] products=new String[no_products]; for(int i=0;i<no_products;i++) { products[i]=sc.nextLine(); } Shop obj=new Shop(shop_name, shop_address, products); //obj.setShopName(shop_name); //obj.setShopAddress(shop_address); //obj.setProducts(products); System.out.println("Enter the product to be searched:"); String srch=sc.nextLine(); if(obj.checkProductAvailability(srch)) { System.out.println("Product is available at "+obj.getShopName()+", "+obj.getShopAddress()); } else { System.out.println("Product is not available at "+obj.getShopName()+", "+obj.getShopAddress()); } } }28. Search a Course
Path: JAVA/Arrays/Search a Course/Course.java
import java.util.*; public class Course { public static void main (String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter no of course:"); int no_crs=sc.nextInt(); if(no_crs>0) { System.out.println("Enter course names:"); String crs[]=new String[no_crs]; for(int i=0;i<no_crs;i++) { crs[i]=sc.next(); } System.out.println("Enter the course to be searched:"); String srch=sc.next(); int flag=0; for(int i=0;i<no_crs;i++) { if(srch.equals(crs[i])) { flag++; } } if(flag!=0) { System.out.println(srch+" course is available"); } else { System.out.println(srch+" course is not available"); } } else { System.out.println("Invalid Range"); } } }29. Sort the first and second half of an array
Path: JAVA/Arrays/Sort the first and second half of an array/ArraySort.java
import java.util.*; public class ArraySort { public static void main (String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter the size of an array:"); int n=sc.nextInt(); if(n>0) { int[] arr=new int[n]; System.out.println("Enter the elements:"); for(int i=0;i<n;i++) { arr[i]=sc.nextInt(); } int[] arr1=Arrays.copyOfRange(arr, 0, (n+1)/2); int[] arr2=Arrays.copyOfRange(arr, (n+1)/2, n ); Arrays.sort(arr1); //Arrays.sort(arr2, Collections.reverseOrder()); for(int i=0;i<n/2;i++) { for(int j=0;j<(n/2)-1;j++) { if(arr2[j]<arr2[j+1]) { int temp=arr2[j]; arr2[j]=arr2[j+1]; arr2[j+1]=temp; } } } int len2=arr1.length+arr2.length; int[] res=new int[len2]; System.arraycopy(arr1, 0, res, 0, (n+1)/2 ); System.arraycopy(arr2, 0, res, (n+1)/2, n/2 ); for(int i : res) { System.out.println(i); } } else { System.out.println("Array size should be greater than 0"); } } }30. Sort the values in descending order
Path: JAVA/Arrays/Sort the values in descending order/Main.java
import java.util.Scanner; public class Main { public static void main(String[] args) { int n, temp; Scanner s = new Scanner(System.in); System.out.print("Enter the array size \n"); n = s.nextInt(); int a[] = new int[n]; System.out.println("Enter the values"); for (int i = 0; i < n; i++) { a[i] = s.nextInt(); } for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (a[i] < a[j]) { temp = a[i]; a[i] = a[j]; a[j] = temp; } } } for (int i = 0; i < n - 1; i++) { System.out.print(a[i] + "\n"); } System.out.print(a[n - 1]); } }31. Sort the values
Path: JAVA/Arrays/Sort the values/Main.java
import java.util.Scanner; public class Main { public static void main(String[] args) { int n, temp; Scanner s = new Scanner(System.in); System.out.print("Enter the array size \n"); n = s.nextInt(); int a[] = new int[n]; System.out.println("Enter the values"); for (int i = 0; i < n; i++) { a[i] = s.nextInt(); } for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (a[i] > a[j]) { temp = a[i]; a[i] = a[j]; a[j] = temp; } } } for (int i = 0; i < n - 1; i++) { System.out.print(a[i]); System.out.println("\n"); } System.out.print(a[n - 1]); } }32. Sum of factorial of positive and single digit numbers in an array
Path: JAVA/Arrays/Sum of factorial of positive and single digit numbers in an array/Factorial.java
import java.util.*; public class Factorial{ public static void main (String[] args) { int size; System.out.println("Enter the size of an array:"); Scanner sc=new Scanner(System.in); size=sc.nextInt(); if(size<=0){ System.out.println("Invalid array size"); return ; } System.out.println("Enter the elements:"); int array[]=new int[size]; for(int i=0;i<size;i++) array[i]=sc.nextInt(); boolean isValid=false; for(int i=0;i<size;i++){ if(array[i]>0&&array[i]<10){ int sum=1; isValid=true; while(array[i]>0){ sum=sum*array[i]; array[i]=array[i]-1; } array[i]=sum; }else array[i]=0; } int sum=0; for(int i:array) sum+=i; if(isValid) System.out.println(sum); else System.out.println("No positive and single digit numbers found in an array"); } }33. Sum of the corresponding elements in 2 arrays
Path: JAVA/Arrays/Sum of the corresponding elements in 2 arrays/ArrayAccumulator.java
import java.util.*; class ArrayAccumulator{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); System.out.println("Enter the size of first array:"); int n=sc.nextInt(); if(n<=0){ System.out.println("Invalid array size"); } else{ int[] a=new int[n]; System.out.println("Enter elements for first array:"); for(int i=0;i<n;i++){ a[i]=sc.nextInt(); } System.out.println("Enter the size of second array:"); int n1=sc.nextInt(); if(n1<=0){ System.out.println("Invalid array size"); } else{ int[] a1=new int[n1]; System.out.println("Enter elements for second array:"); for(int i=0;i<n1;i++){ a1[i]=sc.nextInt(); } int a2; if(n==n1){ for(int i=0;i<n;i++) System.out.println(a[i]+a1[i]); } else if(n>n1){ for(int i=0;i<n1;i++) System.out.println(a[i]+a1[i]); for(int i=n1;i<n;i++) System.out.println(a[i]); } else if(n<n1){ for(int i=0;i<n;i++) System.out.println(a[i]+a1[i]); for(int i=n;i<n1;i++) System.out.println(a1[i]); } } } } }34. Sum of the maximum and the minimum element
Path: JAVA/Arrays/Sum of the maximum and the minimum element/Sum.java
import java.util.*; public class Sum { int max(int[] arr) { int max=0; for(int i=0;i<arr.length;i++) { if(arr[i]>max) { max=arr[i]; } } return max; } int min(int[] arr) { int min=arr[0]; for(int i=0;i<arr.length;i++) { if(arr[i]<min) { min=arr[i]; } } return min; } public static void main (String[] args) { Scanner sc=new Scanner(System.in); Sum obj=new Sum(); System.out.println("Enter the size of an array:"); int n=sc.nextInt(); if(n>0) { int[] arr=new int[n]; System.out.println("Enter the elements:"); for(int i=0;i<n;i++) { arr[i]=sc.nextInt(); } System.out.println(obj.max(arr)+obj.min(arr)); } else { System.out.println("Invalid Array Size"); } } }35. Zig zag Array
Path: JAVA/Arrays/Zig zag Array/Zig zag Array/Main.java
import java.util.*; public class Main { void zigzag(int[] arr) { boolean flag=true; int temp=0; for(int i=0;i<(arr.length-1);i++) { if(flag) { if(arr[i]>arr[i+1]) { temp = arr[i]; arr[i]=arr[i+1]; arr[i+1]=temp; } } else { if(arr[i]<arr[i+1]) { temp=arr[i]; arr[i]=arr[i+1]; arr[i+1]=temp; } } flag= !flag; } for(int i=0;i<arr.length;i++) { System.out.println(arr[i]); } } public static void main (String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int arr[]=new int[n]; int len=arr.length; for(int i=0;i<len;i++) { arr[i]=sc.nextInt(); } Main obj=new Main(); obj.zigzag(arr); } }If you have any queries, please feel free to ask on the comment section.If you want MCQs and Hands-On solutions for any courses, Please feel free to ask on the comment section too.Please share and support our page!
Post a Comment