forked from utkarsh-shekhar/basic-programs
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCompositeMagic.java
More file actions
65 lines (60 loc) · 1.38 KB
/
Copy pathCompositeMagic.java
File metadata and controls
65 lines (60 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/* A Composite Magic number is a number which is both a composite number and a magic number */
import java.util.*;
class CompositeMagic
{
int checkComposite(int n)
{
int c=0;
for(int i=1; i<=n; i++)
{
if(n%i==0)
c++;
}
if(c==2) return 0;
else return 1;
}
int Magic(int n)
{
int s=0;
while(n>9)
{
int t=n;
s=0;
while(t>0)
{
int d=t%10;
s+=d;
t=t/10;
}
n=s;
}
if(n==1) return 1;
else return 0;
}
void main()
{
int m,n;
Scanner sc=new Scanner(System.in);
System.out.println("enter lower boundary");
m=sc.nextInt();
System.out.println("enter upper boundary");
n=sc.nextInt();
if(m>=n)
System.out.println("INVALID INPUT");
else
{
int c=0;
for(int i=m; i<=n; i++)
{
int comp=checkComposite(i);
int mag=Magic(i);
if(comp==1 && mag==1)
{
System.out.print(i+ ",");
c++;
}
}
System.out.println("FREQUENCY OF COMPOSITE INTEGER IS: "+ c);
}
}
}