John found another manuscript of ancient mathematicians. According to this manuscript an integer k is a lucky number if k = a1 + a2 + ... + an, where: ai = 7p. p may be any positive integer. if i and j are distinct, ai != aj For example 7 is a lucky number: 7 = 71. 56 is a lucky number 56 = 72 + 71. John has an array of n integers. He wants to determine how many members of this array are lucky. He is not good at programming and needs your help. Write a program which takes an integer n and array consisting of n integers and determines quantity of lucky integers in this array.

 /* Lucky Number*/

import java.io.*;

import java.util.*;

import java.lang.*;

class Lucky

{

    public static void main(String args[])

    {

int i,n,sum=0;

int a[] = new int[20];

System.out.println("Enter number of elements : ");

Scanner s = new Scanner(System.in);

n = s.nextInt();

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

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

{

    a[i] = s.nextInt();

}

    LuckyNumber(n,a);

    }


    public static void LuckyNumber(int n,int a[])

    {

int i,j,c=0,sum=0;

int b[] = new int[20];

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

{

           sum = 0;

   for(j=1;j<8;j++)

   {

          sum += Math.pow(7,j);

if(a[i] == sum)

{

    b[c] = a[i];

    c++;

    break;

}

   }

}

  System.out.println("Number of Lucky Numbers are : "+c);

System.out.print("Lucky Numbers are : ");

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

{

      System.out.print("  "+b[i]);

}

    }

}

Comments

Popular posts from this blog

Design GUI to handle Choice Control event.

Write a Java program that reads an integer number (between 1 and 255) from the user and prints the binary representation of the number. The answer should be printed as a String. Note: The output displayed should contain 8 digits and should be padded with leading 0s(zeros), in case the returned String contains less than 8 characters. For example, if the user enters the value 16, then the output should be 00010000 and if the user enters the value 100, the output should be 01100100 (Hint : You may use String.format() method for the expected output)