0% found this document useful (0 votes)
3 views8 pages

Java Programs for Number Theory Concepts

The document contains Java programs for various mathematical computations, including checking for perfect numbers, prime numbers, HCF, LCM, Fibonacci series, automorphic numbers, amicable numbers, and printing prime numbers within a range. Each program is encapsulated in a class with a method to accept input and perform the respective calculation. The examples provided illustrate the functionality of each program with specific cases.

Uploaded by

plutonium.204
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views8 pages

Java Programs for Number Theory Concepts

The document contains Java programs for various mathematical computations, including checking for perfect numbers, prime numbers, HCF, LCM, Fibonacci series, automorphic numbers, amicable numbers, and printing prime numbers within a range. Each program is encapsulated in a class with a method to accept input and perform the respective calculation. The examples provided illustrate the functionality of each program with specific cases.

Uploaded by

plutonium.204
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Number Programs(2nd set )

16. Pgm to check no. is perfect no.(sum of factors of no. excluding the no.) or

not eg 28(1+2+4+7+14=28)

public class p16

public void accept(int n)

int i,s=0;

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

if(n%i==0)

s=s+i;

if(s==n)

[Link](n+" is perfect");

else

[Link](n+" is not perfect");

17. Pgm to check prime no or not(eg 13)

public class p17


{

public void accept(int n)

{
int i,nf=0;

if(n==1)

[Link](n+” is neither prime nor composite”);

else

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

if(n%i==0)

nf++;

if(nf==2)

[Link](n+" is prime");

else

[Link](n+" is not prime");

[Link] to find hcf of two numbers

public class p18

{
public void accept(int a,int b) {

int i,s,h=0;

if(a<b)

s=a;

else
s=b;

for(i=1;i<=s;i++)

if(a%i==0&&b%i==0)

h=i;

[Link]("HCF="+h); }

[Link] to find lcm

public class p19

public void accept(int a,int b) {

int i,s,h=0,lcm;

if(a<b)
s=a;

else

s=b;

for(i=1;i<=s;i++)

if(a%i==0&&b%i==0)

h=i;

lcm=(a*b)/h;

[Link]("LCM="+lcm); }

[Link] series
th
0 1 1 2 3 5 8…upto 20 term

public class p20

public void main()

int a=0,b=1,c;

[Link](a+" "+b+" ");


for(int i=3;i<=20;i++)

c=a+b;

[Link](c+" ");

a=b;

b=c;

[Link] series (another type) 1 2 2 4 8 32 …upto n term

public class p21

public void accept(int n)

int a=1,b=2,c;
[Link](a+" "+b+" "); for(int i=3;i<=n;i++)

c=a*b;

[Link](c+" ");
a=b;

b=c;

[Link] to find sum and average of the fibonacci series 0+1+1+2+3+…+upto n

public class p22

public void accept(int n)

int a=0,b=1,c,s=a+b;

[Link](a+" "+b+" ");

for(int i=3;i<=n;i++)

c=a+b;

[Link](c+" ");

s=s+c;

a=b;

b=c;
}
double avg=(double)s/n;

[Link]("sum="+s);

[Link]("Average"+avg);

23. Automorphic no. or not (if no. is present in extreme right of square of the
number then we can call it an automorphic no(eg:25,625)

public class p23

public void accept(int n)

int m=n,k=1,sq,d;

while(m!=0)

m=m/10;

k=k*10;

sq=n*n;

d=sq%k;

if(n==d)

[Link](n+" is automorphic");

else
[Link](n+" is not automorphic");
}

[Link] to check whether no. is amicable or not(sum of perfect divisors


st nd
(excluding that no)of 1 no. is equal to 2 no. and vice versa)eg: 220 and 284

public class p24

public void accept(int a,int b)

int i,sa=0,sb=0;

for(i=1;i<a;i++)

if(a%i==0)

sa=sa+i;

for(i=1;i<b;i++)

if(b%i==0)

sb=sb+i;

if(sa==b && sb==a)

[Link](a+ “ and ”+b+ “are amicable nos");

else

[Link]("not amicable nos");

}
}
25. //To print all prime numbers between a range

public class P25

public void accept(int p,int q)

int n;

[Link](“prime nos “);

for(n=p;n<=q;n++)

int i,nf=0;

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

if(n%i==0)

nf++;

if(nf==2)

[Link](n);

You might also like