0% found this document useful (0 votes)
50 views2 pages

Lab Task 4 Arrays

The document contains 4 code snippets that perform operations on arrays: 1) Take user input to initialize an integer array, calculate the average and percentage of numbers above the average. 2) Print numbers from an integer array that are in consecutive order of 3. 3) Take user input to initialize a 2D array, calculate and print the sum of each row. 4) Take a number from user, generate that many Fibonacci numbers and store in an array.

Uploaded by

Anindita Mishi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views2 pages

Lab Task 4 Arrays

The document contains 4 code snippets that perform operations on arrays: 1) Take user input to initialize an integer array, calculate the average and percentage of numbers above the average. 2) Print numbers from an integer array that are in consecutive order of 3. 3) Take user input to initialize a 2D array, calculate and print the sum of each row. 4) Take a number from user, generate that many Fibonacci numbers and store in an array.

Uploaded by

Anindita Mishi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Declare an integer array of size 6, initialize it with user input, calculate and

print the average.


Now calculate the percentage of numbers that are above that average.

import java.io.*;
import java.util.*;

public class Solution {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);


int [] arr = new int [6];
int i;
double avg, sum = 0, count = 0;
for (i = 0; i<6; i++)
{
arr[i] = sc.nextInt();
sum = sum + arr[i];
}

avg = sum/6;
for (i = 0; i<6; i++)
{
if (arr[i]>avg)
{
count++;
}
}
System.out.printf("%.2f", ((count*100)/6));
System.out.println("%");

}
}

Take an integer array and print only the numbers that are in consecutive orders
of 3.

import java.io.*;
import java.util.*;

public class Solution {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
int i, n;
n = sc.nextInt();
int[] arr = new int [n+2];
for (i = 0; i<n; i++)
{
arr[i] = sc.nextInt();

}
for (i = 0; i<n; i++)
{
if (arr[i]==arr[i+1]&&arr[i+1]==arr[i+2])
{
System.out.print(arr[i] + " ");
}
}
}
}

Take a 3X3 array and initialize it with user given values.

This study source was downloaded by 100000855934132 from CourseHero.com on 10-29-2022 10:46:56 GMT -05:00

https://www.coursehero.com/file/91419307/Lab-task-4-arraystxt/
Calculate and print the sum for each row.

import java.io.*;
import java.util.*;

public class Solution {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
int[][] arr = new int[3][3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
arr[i][j] = sc.nextInt();
}
}
for(int i = 0; i < 3; i++){
int sumRow = 0;
for(int j = 0; j < 3; j++){
sumRow = sumRow + arr[i][j];
}
System.out.println(sumRow);
}
}
}

Take an integer from user, generate that many fibonacci numbers and store in an
array. Display the array.

import java.io.*;
import java.util.*;

public class Solution {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);


int n;
n = sc.nextInt();
int[] arr = new int [n];

arr[0] = 0;
arr[1] = 1;

for (int i = 2; i < n; i++){


arr[i] = arr[i-1]+arr[i-2];
}

for(int i = 0; i < n; i++){


System.out.printf("%d ", arr[i]);
}
System.out.println("");

}
}

This study source was downloaded by 100000855934132 from CourseHero.com on 10-29-2022 10:46:56 GMT -05:00

https://www.coursehero.com/file/91419307/Lab-task-4-arraystxt/
Powered by TCPDF (www.tcpdf.org)

You might also like