WAP to enter 10 elements in an array. Considering the prime index positions, find the average of all the Odious numbers. Logic: If binary equivalent of a number is having odd number of 1's present in it, then the number is considered as Odious number. Example: 11,16,19 etc. Demonstration: Binary equivalent of 11 is 1011, no of 1's present in it is 3, which is odd

 import java.util.*;

class array_1_pg

{

    void main()

    {

        Scanner ob=new Scanner(System.in);

        int a[]=new int[10],i,m,c,d,s=0,f=0;

        System.out.println("Enter the elements in the array:");

        for(i=0;i<10;i++)

        {

            a[i]=ob.nextInt();

        }

        for(i=0;i<10;i++)

        {

            if(i==2 || i==3 || i==5 || i==7)

            {

                m=a[i];//19

                c=0;

                while(m!=0)

                {

                    d=m%2;//1

                    if(d==1)

                    {

                        c++;//3

                    }

                    m/=2;//0

                }

                if(c%2!=0)

                {

                    s=s+a[i];//11+19

                    f++;//2

                }

            }

        }

        if(s>0 || f>0)

          System.out.println("Considering the prime index positions, average of Odious numbers is:"+(double)s/f);

        else

          System.out.println("No Odious numbers are present in the prime index positions");

        }

    }

Post a Comment

Please Select Embedded Mode To Show The Comment System.*

Previous Post Next Post

It looks like you're using an ad blocker. We rely on ads to keep our site free, please whitelist us!

Click here to continue

Please whitelist our site to continue enjoying our content.

Whitelist | Close