[go: up one dir, main page]

0% found this document useful (0 votes)
73 views4 pages

Array Questions

The document contains 6 programming questions that involve taking integer inputs from the user and storing them in arrays. The questions include printing array elements, checking if a number is present in an array, counting positive/negative/even/odd numbers in an array, copying array elements into a new array in reverse order, finding the sum and product of all array elements, and finding the largest and smallest elements of an array.

Uploaded by

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

Array Questions

The document contains 6 programming questions that involve taking integer inputs from the user and storing them in arrays. The questions include printing array elements, checking if a number is present in an array, counting positive/negative/even/odd numbers in an array, copying array elements into a new array in reverse order, finding the sum and product of all array elements, and finding the largest and smallest elements of an array.

Uploaded by

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

1.

Take 10 integer inputs from user and store them in an array and print them on
screen.
Answer

import java.util.*;

class Ans{
public static void main(String[] args){
Scanner s = new Scanner(System.in);
int[] z = new int[10];
for(int i = 0;i<z.length;i++){
System.out.println("Print the value of z["+i+"]");
z[i] = s.nextInt();
}
for(int i = 0;i<z.length;i++){
System.out.println("The value of z["+i+"] is "+z[i]);
}
}
}

2. Take 10 integer inputs from user and store them in an array. Again ask user to give
a number. Now, tell user whether that number is present in array or not.

3. Take 20 integer inputs from user and print the following:


number of positive numbers
number of negative numbers
number of odd numbers
number of even numbers
number os 0.
Answer:

import java.util.*;
class Ans{
public static void main(String[] args){
Scanner s = new Scanner(System.in);
int[] z = new int[20];
int pos = 0;
int neg = 0;
int odd = 0;
int even = 0;
int zero = 0;
for(int i = 0;i<z.length;i++){
System.out.println("Print the value of z["+i+"]");
z[i] = s.nextInt();

if(z[i]>0){
pos++;
}
else if(z[i]<0){
neg++;
}
else{
zero++;
}
if(z[i]%2==0){
even++;
}
else{
odd++;
}
}
System.out.println("Positive : "+pos+"\nNegative : "+neg+"\nZero :
"+zero+"\nodd : "+odd+"\neven : "+even);
}
}
4. Take 10 integer inputs from user and store them in an array. Now, copy all the
elements in an another array but in reverse order.

5. import java.util.*;
6.
7. class Ans{
8. public static void main(String[] args){
9. Scanner s = new Scanner(System.in);
10. int[] a = new int[10];
11. int[] b = new int[10];
12. for(int i =0;i<a.length;i++){
13. System.out.println("Enter the value of a["+i+"]");
14. a[i] = s.nextInt();
15. }
16. int j = 0;
17. for(int i = b.length-1;i>=0;i--){
18. b[i] = a[j];
19. j++;
20. }
21. for(int i = 0; i< b.length; i++){
22. System.out.println("The value of b["+i+"] is "+b[i]);
23. }
24. }
25. }

5. Write a program to find the sum and product of all elements of an array.
6. Find largest and smallest elements of an array.

import java.util.*;

class Ans{
public static void main(String[] args){
Scanner s = new Scanner(System.in);
int[] a = new int[10];
for(int i =0;i<a.length;i++){
System.out.println("Enter the value of a["+i+"]");
a[i] = s.nextInt();
}

int largest = a[0];


int smallest = a[0];

for(int i = 0;i<a.length;i++){
if(a[i]>largest)
largest = a[i];
if(a[i]<smallest)
smallest = a[i];
}

System.out.println("Largest is "+largest+" and smallest is "+smallest);

}
}

You might also like