0% found this document useful (0 votes)
7 views

Iterative Constructs Programs - Copy

The document contains Java programs that determine various types of numbers, including Niven, Neon, Armstrong, Automorphic, GCD, Spy, Perfect, Prime, Abundant, Pronic, and Twin Prime numbers. Each program prompts the user for input, performs calculations, and outputs whether the input number meets the criteria for the specified number type. The code examples demonstrate basic programming concepts such as loops, conditionals, and user input handling.

Uploaded by

afan24168
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)
7 views

Iterative Constructs Programs - Copy

The document contains Java programs that determine various types of numbers, including Niven, Neon, Armstrong, Automorphic, GCD, Spy, Perfect, Prime, Abundant, Pronic, and Twin Prime numbers. Each program prompts the user for input, performs calculations, and outputs whether the input number meets the criteria for the specified number type. The code examples demonstrate basic programming concepts such as loops, conditionals, and user input handling.

Uploaded by

afan24168
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
You are on page 1/ 11

NIVEN NUMBER

import java.util.*;
public class Niven
{
public static void main(String []args)
{
int n,s=0,d;
Scanner in = new Scanner(System.in);
System.out.println("Enter the number:");
n = in.nextInt();
int n1=n;
while(n!=0)
{
d=n%10;
s=s+d;
n=n/10;
}
if(n1%s==0)
System.out.println("Niven Number");
else
System.out.println("Not a Niven number");
}
}
NEON NUMBER

import java.util.*;
public class Neon
{
public static void main(String []args)
{
int n,n1,s=0,d;
Scanner in = new Scanner(System.in);
System.out.println("Enter the number:");
n = in.nextInt();
n1=n;
n=n*n;
while(n!=0)
{
d=n%10;
s=s+d;
n=n/10;
}
if(n1==s)
System.out.println("Neon Number");
else
System.out.println("Not a Neon number");
}
}
ARMSTRONG NUMBER
import java.util.*;
class Armstrong
{
public static void main(String []args)
{
int n,n1,d,s=0;
Scanner in = new Scanner(System.in);
System.out.println("Enter a number:");
n = in.nextInt();
n1=n;
int l=(Integer.toString(n).length());
while(n!=0)
{
d=n%10;
s=s+(int)Math.pow(d,l);
n=n/10;
}
System.out.println(s);
if(s==n1)
System.out.println("Armstrong number");
else
System.out.println("Not an armstrong number");
}
}
AUTOMORPHIC NUMBER

import java.util.*;
class automorphic
{
public static void main(String []args)
{
int n,n1,sq,c=0,d;
Scanner in = new Scanner(System.in);
System.out.println("Enter a number:");
n = in.nextInt();
sq=n*n;
n1=n;
while(n!=0)
{
d= n%10;
c++;
n=n/10;
}
if(sq%(int)Math.pow(10,c) == n1)
System.out.println("Automorphic number");
else
System.out.println("Not an Automorphic number");

}
}
GCD

import java.util.*;
class GCD
{
public static void main(String []args)
{
int a,b,p,i,gcd=0;
Scanner in = new Scanner(System.in);
System.out.println("Enter two numbers:");
a = in.nextInt();
b = in.nextInt();
p=a*b;
for(i=1;i<p;i++)
{
if(a%i==0 && b%i==0)
gcd=i;
}

System.out.println("GCD of 2 numbers="+gcd);
}
}
SPY NUMBER
import java.util.*;
public class Spy
{
public static void main(String []args)
{
int n,s=0,p=1,d;
Scanner in = new Scanner(System.in);
System.out.println("Enter the number:");
n = in.nextInt();
while(n!=0)
{
d=n%10;
s=s+d;
p=p*d;
n=n/10;
}
if(s==p)
System.out.println("Spy Number");
else
System.out.println("Not a Spy number");
}
}
PERFECT NUMBER
import java.util.*;
class Perfect
{
public static void main(String []args)
{
int n,i,c=0;
Scanner in = new Scanner(System.in);
System.out.println("Enter a number:");
n = in.nextInt();
for(i=1;i<n;i++)
{
if(n%i==0)
c=c+i;
}
if(c==n)
System.out.println("Perfect number");
else
System.out.println("Not a perfect number");
}
}
PRIME NUMBER
import java.util.*;
class Prime
{
public static void main(String []args)
{
int n,i,c=0;
Scanner in = new Scanner(System.in);
System.out.println("Enter a number:");
n = in.nextInt();
for(i=1;i<=n;i++)
{
if(n%i==0)
c++;
}
if(c==2)
System.out.println("Prime number");
else
System.out.println("Not a prime number");
}
}
ABUNDANT NUMBER
import java.util.*;
class Abundant
{
public static void main(String []args)
{
int n,i,c=0;
Scanner in = new Scanner(System.in);
System.out.println("Enter a number:");
n = in.nextInt();
for(i=1;i<n;i++)
{
if(n%i==0)
c=c+i;
}

if(c>n)
System.out.println("Abundant number");
else
System.out.println("Not a Abundant number");
}
}
PRONIC NUMBER

import java.util.*;
class Pronic
{
public static void main(String []args)
{
Scanner in = new Scanner(System.in);
int n, i,k=0;
System.out.println("Enter a number:");
n=in.nextInt();
for(i=1;i<=n;i++)
{
if(i*(i+1)==n)
{
System.out.println(i +"*"+ (i+1) +"=" +n);
k=1;
break;
}
}
if(k==1)
System.out.println(n+ " is a Pronic number");
else
System.out.println(n+ " is not a Pronic number");
}
}
TWIN PRIME NUMBER
import java.util.*;
class TwinPrime
{
public static void main(String []args)
{
int a,b,i,c1=0,c2=0;
Scanner in = new Scanner(System.in);
System.out.println("Enter two number:");
a = in.nextInt();
b= in.nextInt();
int x = Math.abs(a-b);
if(x==2)
{
for(i=1;i<=a;i++)
{
if(a%i==0)
c1++;
}
for(i=1;i<=b;i++)
{
if(b%i==0)
c2++;
}
if(c1==2 && c2==2)
System.out.println("Twin Prime number");
else
System.out.println("Not a twin prime number");
}
else
System.out.println("Not a Twin Prime number"); } }

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy