You are on page 1of 21

1 // Predict the output

2 import java.util.*;
3 class Main {
4 public static void main(String[] args)
5 {
6 Scanner al = new Scanner(System.in);
7 int r = al.nextInt();
8 int arr[][] = new int[r][];
9 for (int i=0; i<arr.length; i++)
10 arr[i] = new int[i+1];
11 int count = 0;
12 for (int i=0; i<arr.length; i++)
13 for(int j=0; j<arr[i].length; j++)
14 arr[i][j] = count++;
15 for (int i=0; i<arr.length; i++)
16 {
17 for (int j=0; j<arr[i].length; j++)
18 System.out.print(arr[i][j] + " ");
19 System.out.println();
20 }
21 }
22 }
Question 1
Write a java program to print the jagged array for the given below input.

Input Format:
Number of rows
Number of elements in each row.
Array elements.
Question 1
Sample Input: Sample Output:
3 6 7 8 10 36
5 81 80 2
3 5 6 82 10 11 23
6
6 7 8 10 36
81 80 2
5 6 82 10 11 23
1 import java.util.*;
2 public class Main
3 {
4 public static void main(String[] args)
5 {
6 Scanner sc = new Scanner(System.in);
7 int row = sc.nextInt();
8 int arr[][] = new int[row][];
9 for(int i = 0;i<row;i++)
10 {
11 arr[i] = new int[sc.nextInt()];
12 }
13 for (int i=0; i<arr.length; i++)
14 {
15 for(int j=0; j<arr[i].length; j++)
16 {
17 arr[i][j] = sc.nextInt();
18 }
19 }
20
21
22
1 for (int i=0; i<arr.length; i++)
2 {
3 for (int j=0; j<arr[i].length; j++)
4 {
5 System.out.print(arr[i][j] + " ");
6 }
7 System.out.println();
8 }
9 }
10 }
11
12
13
14
15
16
17
18
19
20
21
22
Question 2
Rakesh’s grandmother has the habit of giving some money to her grandson Rakesh
whenever she visit his home. She may visit his home ‘x’ months in a year and ‘ni’
number of times in every month. Consider ‘i’ to be the respective month and ‘nik’ to
be the number of times she had visited his house in ith month. Write a java program
to calculate to total amount given by his grandma to rakesh.
Question 2
Sample Input: Sample Output:
6 1090
2
1
1
5
6
3
15 10
100
50
10 10 10 10 10
15 20 30 50 10 10
200 30 500
1 import java.util.*;
2 public class Main
3 {
4 public static void main(String[] args)
5 {
6 Scanner sc = new Scanner(System.in);
7 int row = sc.nextInt();
8 int arr[][] = new int[row][];
9 int sum = 0;
10 for(int i = 0;i<row;i++)
11 {
12 arr[i] = new int[sc.nextInt()];
13 }
14 for (int i=0; i<arr.length; i++)
15 {
16 for(int j=0; j<arr[i].length; j++)
17 {
18 arr[i][j] = sc.nextInt();
19 }
20 }
21
22
1 for (int i=0; i<arr.length; i++)
2 {
3 for (int j=0; j<arr[i].length; j++)
4 {
5 sum = sum + arr[i][j];
6 }
7 }
8 System.out.println(sum);
9 }
10 }
11
12
13
14
15
16
17
18
19
20
21
22
Question 3
Write a java program to print the boundary elements in the given jagged array.
Question 3
Sample Input: Sample Output:
4 12 10
4 10 2
2 100
1 16
6
12 66 78 10
10 2
100
123456
1 import java.util.*;
2 public class Main
3 {
4 public static void main(String[] args)
5 {
6 Scanner sc = new Scanner(System.in);
7 int row = sc.nextInt();
8 int arr[][] = new int[row][];
9 int x=0;
10 for(int i = 0;i<row;i++)
11 {
12 arr[i] = new int[sc.nextInt()];
13 }
14 for (int i=0; i<arr.length; i++)
15 {
16 for(int j=0; j<arr[i].length; j++)
17 {
18 arr[i][j] = sc.nextInt();
19 }
20 }
21
22
1 for (int i=0; i<arr.length; i++)
2 {
3 System.out.print(arr[i][0] + " ");
4 x = arr[i].length - 1;
5 if(x!=0)
6 {
7 System.out.print(arr[i][x]);
8 }
9 System.out.println();
10 }
11 }
12 }
13
14
15
16
17
18
19
20
21
22
Question 4
Write a Java code to sort the given array using bubble sort.

Sample Input: Sample Output:


8 list[] = {1, 2, 3, 4, 5, 6, 10, 12}
list[] = {4, 3, 2, 10, 12, 1, 5, 6}
1 import java.util.Scanner;
2 class Main
3 {
4 public static void main(String args[])
5 {
6 Scanner sc = new Scanner(System.in);
7 int n = sc.nextInt();
8 int list[] = new int[n];
9 for(int index = 0;index < n; index++)
10 {
11 list[index] = sc.nextInt();
12 }
13 bubble_sort(n,list);
14 }
15
16
17
18
19
20
21
22
1 public static void bubble_sort(int n, int list[])
2 {
3 for(int i = 0; i <= n-2; i++)
4 {
5 for(int j = 0; j<= n-2-i; j++)
6 {
7 if(list[j] > list[j+1])
8 {
9 int temp = list[j];
10 list[j] = list[j+1];
11 list[j+1] = temp;
12 }
13 }
14 }
15 for(int i = 0; i < n; i++)
16 System.out.print(list[i]+" ");
17 }
18 }
19
20
21
22
Question 5
Write a java program to sort the first half of the array using insertion sort.

Sample Input: Sample Output:


6 267321
672321
1 import java.util.Scanner;
2 class Main
3 {
4 public static void main(String args[])
5 {
6 Scanner sc = new Scanner(System.in);
7 int n = sc.nextInt();
8 int list[] = new int[n];
9 for(int index = 0;index < n; index++)
10 {
11 list[index] = sc.nextInt();
12 }
13 insertion_sort(n,list);
14 }
15
16
17
18
19
20
21
22
1 public static void insertion_sort(int n,int list[])
2 {
3 for (int idx1 = 1; idx1 < n/2; idx1++)
4 {
5 int key = list[idx1];
6 int idx2 = idx1 - 1;
7 while((idx2 >= 0) && (list[idx2] > key))
8 {
9 list[idx2 + 1] = list[idx2];
10 idx2--;
11 }
12 list[idx2 + 1] = key;
13 }
14 for(int i = 0; i < n; i++)
15 {
16 System.out.print(list[i]+" ");
17 }
18
19 }
20 }
21
22
THANK YOU

You might also like