Perfect Square Upper, Lower
Perfect Square Upper, Lower
Write a program to accept 10 numbers in an SDA and display the Perfect squares.
import java.util.*;
class Square
{
public void print()
{
Scanner sc= new Scanner(System.in);
int a[]= new int[10];
//storing elements
for(int i=0;i<10;i++)
{
System.out.println("Enter a number:");
a[i]= sc.nextInt();
}
//printing perfect squares
int sq=0, f=0;
for(int i=0;i<10;i++)
{
sq=(int)Math.sqrt(a[i]);
if(sq*sq==a[i])
{
System.out.println(a[i]);
f++;
}
}
if(f==0)
System.out.println("No perfect squares in the array.");
}
}
Question 2:
Create a matrix B[][] of order N. Display the Upper Diagonal of the matrix.
Example:
N=4
Original Matrix Upper Diagonal
12 45 78 21 12 45 78 21
67 56 89 34 67 56 89
98 76 43 84 98 76
66 64 92 55 66
import java.util.*;
class UpperDiagonal
{
public void print()
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter order of the matrix:");
int N=sc.nextInt();
int B[][]= new int[N][N];
//storing elements
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
{
System.out.println("Enter a number:");
B[i][j]= sc.nextInt();
}
}
//printing original Matrix(optional)
System.out.println("ORIGINAL MATRIX:");
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
{
System.out.print(B[i][j]+"\t");
}
System.out.println();
}
//printing upper diagonal
System.out.println("UPPER DIAGONAL:");
for(int i=0;i<N;i++)
{
for(int j=0;j<N-i;j++)
{
System.out.print(B[i][j]+"\t");
}
System.out.println();
}
}}
Question 3:
Create a matrix B[][] of order N. Display the Lower Diagonal of the matrix.
Example:
N=4
Original Matrix Lower Diagonal
12 45 78 21 21
67 56 89 34 89 34
98 76 43 84 76 43 84
66 64 92 55 66 64 92 55
import java.util.*;
class LowerDiagonal
{
public void print()
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter order of the matrix:");
int N=sc.nextInt();
int B[][]= new int[N][N];
//storing elements
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
{
System.out.println("Enter a number:");
B[i][j]= sc.nextInt();
}
}
//printing original Matrix(optional)
System.out.println("ORIGINAL MATRIX:");
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
{
System.out.print(B[i][j]+"\t");
}
System.out.println();
}
//printing upper diagonal
System.out.println("LOWER DIAGONAL:");
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
{
if(j>=N-1-i)
System.out.print(B[i][j]+"\t");
else
System.out.print(" "+"\t");
}
System.out.println();
}
}}