Page 411 - CA_Blue( J )_Class10
P. 411
• Print the square of each element of the array. [2022]
Ans. import java.util.Scanner;
public class Array
{
public static void main()
{
double ar[] = new double[20];
double p = 1.0;
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 20; i++)
{
System.out.print("Enter the number ar[" + i + "]:");
ar[i] = sc.nextDouble();
}
for (int i = 0; i < 20; i++)
{
p = p * ar[i];
}
System.out.println("The product of all the elements is: "+p);
System.out.println("Square of each element of the array is: ");
for (int i = 0; i < 20; i++)
{
System.out.println(ar[i]*ar[i]);
}
}
}
16. Write a program to input 15 integer elements in an array and sort them in ascending order using the bubble sort technique.
[2019]
Ans. import java.util.*;
class bubblesorting
{
public static void main()
{
Scanner sc= new Scanner(System.in);
int a[] = new int[15];
int i,j,temp;
for(i = 0; i < 15; i++)
{
System.out.println("Enter numbers:");
a[i] = sc.nextInt();
}
for(i = 0; i < 15; i++)
{
for( j = 0; j < 14 - i; j++)
{
if(a[j] > a[j + 1])
{
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
System.out.println("Sorted elements:");
for(i = 0; i < 15; i++)
409
Arrays 409

