/* B4.2019 Syllabus Package program to find prime, fib and Armstrong.
Steps to follow:
Step 1: Create a folder “myPackage” in “C:\Java\Bin”.
Step 2: Type and Save CheckNumber.java in myPackage folder.
Step 3: Compile it as: C:\Java\Bin>javac myPackage\CheckNumber.java */
//Code for CheckNumber.java:
package myPackage;
public class CheckNumber
{
public static void prime(int num)
{
int pflag=0;
for(int i=2;i<=num/2;++i)
{
if(num%i==0)
pflag=1;
break;
}
if(pflag==0)
System.out.println("It is a Prime Number!");
else
System.out.println("It is not a Prime Number!");
}
public static void fibonacci(int num)
{
boolean flag=false;
if(num>0)
{
flag=isFibonacci(num);
}
if(flag)
System.out.println("It is a Fibonacci Number!");
else
System.out.println("It is not a Fibonacci Number!");
}
static boolean isPerfectSquare(int x)
{
int s = (int) Math.sqrt(x);
return (s*s == x);
}
static boolean isFibonacci(int x)
{
return isPerfectSquare(5*x*x + 4) || isPerfectSquare(5*x*x - 4);
}
public static void armstrong(int num)
{
int temp, n=0;
double remainder, result=0.0;
temp = num;
while (temp != 0)
{
temp =temp / 10;
++n;
}
temp = num;
while (temp != 0)
{
remainder = temp%10;
result = result + Math.pow(remainder, n);
temp = temp / 10;
}
if(result == num)
System.out.println("It is an Armstrong Number!");
else
System.out.println("It is not an Armstrong Number!");
}
}
//B4 continued... 2019syllabus Package Program
Steps continued…
Step 4: Type and Save B4.java in C:\Java\Bin folder.
Step 5: Compile main program as: C:\Java\Bin>javac B4.java
Step 6: Run only main program as: C:\Java\Bin>java B4
Code for B4.java:
import java.io.*;
import myPackage.*;
class B4
{
public static void main(String args[]) throws IOException
{
int num;
CheckNumber obj=new CheckNumber();
DataInputStream in=new DataInputStream(System.in);
System.out.println("Enter a number:");
num=Integer.parseInt(in.readLine());
obj.prime(num);
obj.fibonacci(num);
obj.armstrong(num);
}
}
Search This Blog
Sunday, 21 August 2022
B4. Package program to find prime, fib and Armstrong.
Labels:
Java_2019Syllabus
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment