Here we will discuss off campus coding question asked in Xoriant off campus recruitment. Xoriant coding question with solution.
Xoriant Question
For the question below we define a internal number as any number whose last digit is 6, and a external number as any number that is less than 56.
You have to take the input from user until he enters negative number. Then
You have to change the program so that any number in the array is replaced by -3. if it’s a internal number, with -7 if it’s a external number, and with -9 if it is both, a internal and external number.
Example:
input:
96 68 12 46 -1
Output:
-3 68 -7 -9
Explanation:
Input is checked for divisible by 6 if it’s truereturn -3, if the input is less than 56 return -7. if it satisfy both conditions then return -9.
Algorithm to solve
- To take the input from user untill user enters negative number, use a while loop
- As we are not sure about how many elements we are going to enter so use ArrayList, whose size increases automatically.
- Once your done with taking input from user just convery arraylist to array.
- Write nested if else statements to check for given conditions and write output accordingly.
- At the end modify array accordingly and print the array.
JAVA CODE
package arrayproblems; import java.util.*; class problem7 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); ArrayList array1=new ArrayList(); System.out.println("enter the elements of array"); boolean flag = true; while(flag) { int a=sc.nextInt(); if(a>0) { array1.add(a); } else { flag=false; } } System.out.println(array1); int[] array2=new int[array1.size()]; for(int i=0;i<array1.size();i++) { array2[i]=(int) array1.get(i); } for(int i=0;i<array2.length;i++) { if(array2[i]%10==6 && array2[i]<56) { array2[i]=-9; } else if(array2[i]%10==6){ array2[i]=-3; } else if(array2[i]<56) { array2[i]=-7; } } for(int i:array2) { System.out.print(i+ " "); } } }
Similar Questions
Complete Solution For Rob And Mars Problem In Java
Pingback: Taking N number of inputs from user - Is It Actually
Pingback: Rotate Array Elements towards Right Side - Is It Actually
Pingback: Maximum Work Hacker Earth Problem solved - Is It Actually
Pingback: SHL off campus coding Questions - Is It Actually