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
Post a Comment