Wipro Training Assignment Day 3
Wipro Training Assignment Day 3
Q1. Write a program to initialize an integer array and print the sum and
average of the array
package arrays;
import java.util.*;
Q2. Write a program to initialize an integer array and find the maximum
and minimum value of an array
package arrays;
import java.util.*;
Q3. Write a program to initialize an integer array with values and check if
a given number is present in the array or not. If the number is not found, it
will print -1 else it will print the index value of the given number in the
array
Ex1) Array elements are {1,4,34,56,7} and the search element is 90
O/P: -1
Ex2)Array elements are {1,4,34,56,7} and the search element is 56
O/P: 4
package arrays;
import java.util.*;
public class ARRAY_SEARCH
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int [] array1 = new int[] {1,4,34,6,7};
System.out.println("Enter the number to be searched : ");
int key = sc.nextInt();
int index=0;
int flag=0;
for(int i = 0; i<array1.length;i++)
{
if(array1[i]==key)
{
index= i;
flag = 1;
else
{
continue;
}
}
if(flag!=0)
{
System.out.println("Index of the searched element is
"+index);
}
else
{
System.out.println("-1");
}
}
}
Q4. Initialize an integer array with ascii values and print the corresponding
character values in a single row.
package arrays;
import java.util.*;
Q5. Write a program to find the largest 2 numbers and the smallest 2
numbers in the given array
package arrays;
import java.util.*;
for(int i = 0;i<arr.length;i++)
{
for(int j =i+1;j<arr.length;j++)
{
if(arr[j]<arr[i])
{
int temp = arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
package arrays;
import java.util.*;
public class REPEAT_ARRAY
{
Q8. Write a program to print the element of an array that has occurred the highest
number of times
Eg) Array -> 10,20,10,30,40,100,99
O/P:10
package arrays;
import java.util.*;
public class REPEAT_ARRAY
{
Q9. Write a program to print the sum of the elements of the array with the given below
condition. If the array has 6 and 7 in succeeding orders, ignore 6 and 7 and the numbers
between them for the calculation of sum.
Eg1) Array Elements - 10,3,6,1,2,7,9
O/P: 22
[i.e 10+3+9]
Eg2) Array Elements - 7,1,2,3,6
O/P:19
Eg3) Array Elements - 1,6,4,7,9
O/P:10
package arrays;
import java.util.*;
public class CLASS
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter size of array: ");
int size = in.nextInt();
int arr[] = new int[size];
System.out.print("Enter array elements: ");
boolean six = false, seven = false;
int id_six = -1, id_seven = arr.length;
int sum = 0;
System.out.println(sum);
Q10. Write a program to reverse the elements of a given 2*2 array. Four integer
numbers needs to be passed as Command Line arguments.
Example1:
C:\>java Sample 1 2 3
O/P Expected : Please enter 4 integer numbers
Example2:
C:\>java Sample 1 2 3 4
O/P Expected :
1 2 3 4
5 6 7 8
9 10 11 12
package arrays;
import java.util.*;
public class REVERSE_CLASS
{
public static void main(String[] args)
{
if (args.length != 4)
System.out.println("Please enter 4 integer numbers");
else {
int arr[][] = new int[2][2];
for (int i = 0, k = 0; i < 2; i++) {
for (int j = 0; j < 2; j++, k++) {
arr[i][j] = Integer.parseInt(args[k]);
}
}
// reversing rows;
for (int i = 0; i < 2; i++) {
int tmp = arr[0][i];
arr[0][i] = arr[1][i];
arr[1][i] = tmp;
}
//reversing columns
for (int i = 0; i < 2; i++) {
int tmp = arr[i][0];
arr[i][0] = arr[i][1];
arr[i][1] = tmp;
}
}
Q11. Write a program to find greatest number in a 3*3 array. The program is supposed
to receive 9 integer numbers as command line arguments.
Example1:
C:\>java Sample 1 2 3
Example2:
O/P Expected :
The given array is :
1 23 45
55 121 222
56 77 89
The biggest number in the given array is 222
class Solution {
public static void main(String[] args) {
if(args.length != 9)
System.out.println("Please enter 9 integer numbers");
else {
int arr[][] = new int[3][3];
int max = 0;
for(int i=0, k = 0; i<3; i++) {
for(int j=0; j<3; j++, k++) {
arr[i][j] = Integer.parseInt(args[k]);
}
}