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

Program n10

Program in java

Uploaded by

debojit052007
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)
17 views4 pages

Program n10

Program in java

Uploaded by

debojit052007
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

Program no: 10

import java.util.*;

public class program_10

int r1,r2,i,j,uN[],sum=0,c=0,index=0;

Scanner s = new Scanner(System.in);

public void findUniqueNumbers() {

System.out.println("Enter the start of the range: ");

r1 = s.nextInt();

System.out.println("Enter the end of the range: ");

r2 = s.nextInt();

for (i=r1;i<=r2;i++) {

if(isUnique(i)) c++;

}//find the number of unique numbers

uN = new int[c];

for (i=r1;i<=r2;i++) {

if (isUnique(i))

uN[index++]=i;

int[][] arr = storeIn2DArray(uN);

printResult(arr);

public boolean isUnique(int num){

String strNum = String.valueOf(num);


for (int i=0;i<strNum.length();i++){

for (int j=i+1;j<strNum.length();j++) {

if (strNum.charAt(i)==strNum.charAt(j)) {

return false;//if the digits are not identical it is not an unique number

return true;//if the digits are identical it is an unique number

public int[][] storeIn2DArray(int[] uniqueNums) {

int n=(int)Math.ceil(Math.sqrt(uniqueNums.length));

int[][]arr=new int[n][n];

index=0;

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

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

if (index<uniqueNums.length) {

arr[j][i] = uniqueNums[index++];

//2-D array initailised as column major wise

return arr;

}
public void printResult(int[][] arr) {

System.out.println("2D Array: ");

for (int i=0;i<arr.length;i++) {

for (int j=0;j<arr.length;j++) {

if(arr[i][j]!=0)

System.out.print(arr[i][j] + " ");//print the 2-D array

System.out.println();

System.out.print("Sum of primary diagonal: ");

for (int i=0;i<arr.length;i++) {

sum += arr[i][i];//find and print the sum of primary diagonal of the 2-D array

} System.out.print(sum);

System.out.print("\nSum of secondary diagonal: ");sum=0;

for (int i = 0; i < arr.length; i++) {

sum += arr[i][arr.length - i - 1];//find and print the sum of secondary diagonal


of the 2-D array

} System.out.print(sum);

public void main() {

program_10 ob = new program_10();

ob.findUniqueNumbers();

}
Output:

You might also like