Professional Documents
Culture Documents
Arrays
Arrays
java
Details
Activity
/* - Arrays: PracticeExercise 2 Answers 1-4
1. Initialize a character array with vowels.
2. Initialize an Integer array with 1 to 12 month numbers.
3. Initialize an Integer array with first five even numbers.
4. Initialize a double data type array with average marks 89.5, 90.2, 56.0,
67.9, 88.3.
*/
class Ch7_Pr_Ex2_Ans1_4
{
/*
4. Initialize a double data type array with average marks 89.5, 90.2,
56.0, 67.9, 88.3.
Ans. double avgmarks [] = {89.5, 90.2, 56.0, 67.9, 88.3};
*/
}
Ans2.java
Details
Activity
/* Arrays: Practice Exercise 4 Answer 2-
Write a program to read values as parameters into arrays.
2. Read marks of a student in five subjects, display the sum of the marks.
Output: depends on the input
Input: {90,78,98,86,90}
Sum is: 442
*/
class Ch7_Pr_Ex4_Ans2
{
void read(int marks[]) //parameter is marks
{
int len;
len = marks.length; //if user gives 5 elements, length will be 5
int sum=0;
for(int ctr = 0; ctr < len; ctr ++)
sum += marks[ctr];
System.out.println("Sum is:\t" + sum );
}//end of accept method
}//end of class
Ans3.java
Details
Activity
/* Arrays: Practice Exercise 4 Answer 3
Write a program to read values as parameters into arrays.
3. Read N from the user and store all the natural numbers from N to 1 and
display the array.
Output: if input is 5
Numbers in reverse order:
5 4 3 2 1
*/
class Ch7_Pr_Ex4_Ans3
{
public void main(int N)
{
int nat[]=new int [N];// declaring an array of size N
for(int j=0, i=N; i > 0; i--,j++)// j stores array subscript, i
stores N to 1
nat[j]=i;
// printing
System.out.println("Numbers in reverse order:");
for(int j=0 ; j < N; j++)
System.out.print (nat[j] + " ");
}// end of method
}// end of class
Ans4.java
Details
Activity
/* Arrays: Practice Exercise 4 Answer 4
4. Read five integers in an array and display 1st, 3rd and 5th elements
(odd positions).
Write a program to read values as parameters into arrays.
Input: {5,21,45,67,2}
Output:
Ans1.java
Details
Activity
/* - Arrays: Practice Exercise 5: Answer 1
Use terminal window to read the values.
1. Read an array to store the marks of five students and display the marks.
Output:
Program to store marks of five students in an array and display:
Enter 5 students marks:
100
92
83
84
95
The entered marks are:
100 92 83 84 95
*/
import java.util.Scanner;
class Ch7_Pr_Ex5_Ans1
{
public static void main(String args[])
{
int num[]=new int [5];// declaring an array
Scanner sc= new Scanner(System.in);
System.out.println("Program to store marks of five students in
an array and display:");
System.out.println("Enter 5 students marks:");
for(int j=0; j< 5; j++)
num[j]=sc.nextInt();//Reading 5 elements into the num array
// printing
System.out.println("The entered marks are:");
for(int j=0 ; j < 5; j++)
System.out.print (num[j] + "\t");
}// end of method
}// end of class
Ans2.java
Details
Activity
/*Arrays: Practice Exercise 5: Answer 2
Use terminal window to read the values.
2. Read an array to store the marks in five subjects from user, and display
the average marks.
Output:
Enter Marks in 5 subjects:
97
98
95
96
100
Ans3.java
Details
Activity
/*- Arrays: Practice Exercise 5: Answer 3
Use terminal window to read the values.
3. Write a program to read N from user and store all the natural numbers
from N to 1 in an integer
array.
Output:
Program to store all the natural numbers from N to 1:
Enter a positive number: 5
*/
import java.util.Scanner;
class Ch7_Pr_Ex5_Ans3
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
System.out.println("Program to store all the natural numbers
from N to 1:")
System.out.print("Enter a positive number: ");
int N=sc.nextInt();
int num[]=new int [N];// declaring an array with N elements
for(int i=0,j=N; j>0; j--,i++)//storing natural numbers in reverse
order in num array
num[i]=j;
// printing
System.out.println("Natural numbers in reverse order:");
for(int j=0 ; j < N; j++)
System.out.print (num[j] + " ");
}// end of method
}// end of class
Ans4.java
Details
Activity
/*- Arrays: Practice Exercise 5: Answer 4
Use terminal window to read the values.
4. Write a program to read five integers and display 2nd and 4th elements
(even positions).
Output:
Enter 5 numbers:
10
25
32
47
58
The even position elements are:
25 47
*/
import java.util.Scanner;
class Ch7_Pr_Ex5_Ans4
{
public static void main(String args[])
{
int num[]=new int [5];// declaring an array
System.out.println("Enter 5 numbers:");
Scanner sc= new Scanner(System.in);
for(int j=0; j< 5; j++)
{
num[j]=sc.nextInt();//reading 5 elements in num array.
}//end of for loop
// printing
System.out.println("The even position elements are:");
for(int j=1 ; j < 5; j+=2)// incrementing by two to display even
elements - the array starts with 0 subscript
System.out.print (num[j] + "\t");
}// end of method
}// end of class
.java
Details
Activity
/*- Arrays: Practice Exercise 5: Answer 5
Use terminal window to read the values.
Ans6.java
Details
Activity
/*- Arrays: Practice Exercise 5: Answer 6
Use terminal window to read the values.
6. Read five integers in an array and display 1st, 3rd and 5th elements
(odd positions).
Output:
Enter 5 numbers:
10
25
32
47
58
The odd position elements are:
10 32 58
*/
import java.util.Scanner;
class Ch7_Pr_Ex5_Ans6
{
Ans1.java
Details
Activity
/*- Arrays: Practice Exercise 12 Ans 1
1. Store even numbers in a 5 × 5 matrix.
Output:
The 5X5 array with even numbers is:
2 4 6 8 10
12 14 16 18 20
22 24 26 28 30
32 34 36 38 40
42 44 46 48 50
*/
class Ch7_Pr_Ex12_Ans1
{
void store()
{
int even[][]=new int[5][5];
int num=2;
for(int i=0;i<5;i++)//loop for rows
{
for(int j=0;j<5;j++)// loop for columns in each row
{
even[i][j]=num;
num+=2;
}//end of inner for loop
}//end of outer for loop
System.out.println("The 5X5 array with even numbers is: ");
// displaying the array
for(int i=0;i<5;i++)//loop for rows
{
for(int j=0;j<5;j++)// loop for columns in each row
{
System.out.print(even[i][j]+"\t"); //printing all elements
of a row
}//end of inner for loop
System.out.println();//new line for the next row
}//end of outer for loop
}//end of method
}//end of class
_Ans2.java
Details
Activity
/*Class 10 - Chapter 7 - Arrays: Practice Exercise 12 Ans 2
2. Store first ten Fibonacci numbers in a 2 × 5 array.
Output:
Ch7_Pr_Ex12_Ans3.java
Details
Activity
/*Class 10 - Chapter 7 - Arrays: Practice Exercise 12 Ans 3
3. Store first twelve palindrome numbers in a 3 × 4 array
Output:
0 1 2 3
4 5 6 7
8 9 11 22
*/
class Ch7_Pr_Ex12_Ans3
{
boolean palindrome(int n)
{
int rev=0, n1=n; //storing the value of n in n1
while(n>0)
{
int d=n%10;
rev=rev*10+d;
n=n/10;
}//end of loop
if( n1==rev)
return true;
else
return false;
}//end of method
void store()
{
int palin[][]=new int[3][4];
int k=0;
for(int i=0;i<3;i++)//loop for rows
{
for(int j=0;j<4;)// loop for columns in each row
{
if(palindrome(k))
{
palin[i][j]=k;
j++;
k++;
}//end of inner for loop
}//end of outer for loop
System.out.println("The first twelve palindromic numbers are:\n");
// displaying the array
for(int i=0;i<3;i++)//loop for rows
{
for(int j=0;j<4;j++)// loop for columns in each row
{
System.out.print(palin[i][j]+"\t"); //printing all elements
of a row
}//end of inner for loop
System.out.println();//new line for the next row
}//end of outer for loop
}//end of method
}//end of class