Array-promo

Question

Display Elements which are smith and emirp from the array.

Further sort the Smith and emirp elements displayed above individually and display the complete sorted smith and emirp elements in separate line with proper message.

Only using main method.

				
					(AnĀ Emirp Number (prime spelled backwards) is a prime number that results in a different prime when its decimal digits are reversed.)
(A Smith Number is a composite number whose sum of digits is equal to the sum of digits in its prime factorization)
				
			
				
					Enter the size of array:
10
Enter elements in array:
101
3
22
4
666
12
31
13
113
311
Emirp Numbers Unsorted:
101 3 31 13 113 311 
Smith Number Unsorted:
22 4 666 
Emirp Numbers Sorted:
3 13 31 101 113 311 
Smith Number sorted:
4 22 666 
				
			

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

				
					import java.util.Scanner;
public class array
{

    public static void main()
    {
        int arr[],emirpArray[],SmithArray[],tempArr[];
        int n,i=0,count=0,flag=0,flag1=0,rev=0,temp=0,k=0,sumd=0,sump=0,j=0,temp1=0,l=0,m=0;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the size of array:");
        n=sc.nextInt();
        arr=new int[n];
        emirpArray=new int[n];
        SmithArray=new int[n];
        System.out.println("Enter elements in array:");
        for(i=0;i< n;i++)
        {
            arr[i]=sc.nextInt();
        }
        //Emirp Number Checking
        for(i=0;i< n;i++)
        {
            if(arr[i]>1)
            {
                for(j=2;j< arr[i];j++)
                {
                    if(arr[i]%j==0)
                    {
                        flag=1;
                        break;
                    }
                }
            }
            else
            {
                flag=1;   
            }
            temp=arr[i];
            while(temp>0)
            {
                rev=rev*10+temp%10;
                temp=temp/10;
            }
            if(rev>1)
            {
                for(j=2;j< rev;j++)
                {
                    if(rev%j==0)
                    {
                        flag1=1;
                        break;
                    }
                }
            }
            else
            {
                flag1=1;   
            } 
            if(flag==0&&flag1==0)
            {

                emirpArray[k]=arr[i]; 
                k++;
            }
            //Emirp number checking Ends here
            flag=0;
            flag1=0;
            rev=0;
            sump=0;
            sumd=0;
            //Smith number checking Starts Here
            temp=arr[i];
            while(temp>0)
            {
                sumd=sumd+(temp%10);
                temp=temp/10;
            }
            temp=arr[i];
            for(j=2;j<=arr[i]/2;j++)
            {
                while(temp%j==0)
                {

                    flag=0;
                    //checking whether number is prime number or not
                    for(m=2;m< j;m++)
                    {

                        if(m%j==0)
                        {
                            flag=1;
                            break;
                        }
                    } 
                    //if number is prime then only it is added to sump
                    if(flag==0)
                    {
                        temp1=m;
                        //Extracting digits if i >9 and then adding extracted digits to sump
                        while(temp1>0)
                        {
                            sump=sump+(temp1%10);   
                            temp1=temp1/10;   
                        }
                        temp=temp/m;

                    }

                }
            }
            
            if(sumd==sump && arr[i]>0)
            {
                SmithArray[l]=arr[i];
                l++;
            }
            //Smith number checking ends Here

        }
        //Creating emirpArray 
        tempArr=new int[k];
        for(i=0;i< k;i++)
        {
            tempArr[i]=emirpArray[i];
        }
        emirpArray=tempArr;
        tempArr=new int[l];
        //Creating smithArray
        for(i=0;i< l;i++)
        {
            tempArr[i]=SmithArray[i];
        }
        SmithArray=tempArr;
        
        System.out.println("Emirp Numbers Unsorted:");
        //Printing Emirp Number array
        for(i=0;i< k;i++)
        {
            System.out.print(emirpArray[i]+" ");
        }
        
        System.out.println();
        System.out.println("Smith Number Unsorted:");
        //Printing Smith Number array
        for(i=0;i< l;i++)
        {
            System.out.print(SmithArray[i]+" ");
        }
        
        //sorting emirp number array
       for(i=0;i< k;i++)
       {
         for(j=0;j< k;j++)
         {
             if(emirpArray[i]< emirpArray[j])
             {
                 temp=emirpArray[i];
                 emirpArray[i]=emirpArray[j];
                 emirpArray[j]=temp;
                }
            }
        }
        System.out.println();
        System.out.println("Emirp Numbers Sorted:");
        
        for(i=0;i< k;i++)
        {
            System.out.print(emirpArray[i]+" ");
        }
        //sorting smith number array
        for(i=0;i< l;i++)
       {
         for(j=0;j< l;j++)
         {
             if(SmithArray[i]< SmithArray[j])
             {
                 temp=SmithArray[i];
                 SmithArray[i]=SmithArray[j];
                 SmithArray[j]=temp;
                }
            }
        }
        System.out.println();
        System.out.println("Smith Number sorted:");
        
        for(i=0;i< l;i++)
        {
            System.out.print(SmithArray[i]+" ");
        }
    }
}

				
			

Coding Store

10-minute Stress-Buster Games

Leave a Reply

Your email address will not be published. Required fields are marked *