QuestionAsk Questions?

write a program to print the series of long integer number?

Write a program in java to find out whether the given number is prime or not

View Answers

December 24, 2010 at 11:54 AM

Hello Friend,

1)Fibonacci series:

import java.util.*; public class Fibonacci { public static void main(String[] args) throws Exception { Scanner input=new Scanner(System.in); System.out.print("Enter value of n: "); int num = input.nextInt(); int f1=0,f2=0,f3=1; for(int i=1;i<=num;i++){ System.out.println(f3); f1=f2; f2=f3; f3=f1+f2; } } }

2)Check Prime Number

import java.util.*; class CheckPrimeNumber { static boolean isPrime(int number){ boolean isPrime=false; int i=(int) Math.ceil(Math.sqrt(number)); while (i>1){ if ((number!=i) && (number%i==0)){ isPrime=false; break; } else if (!isPrime) isPrime=true; --i; } return isPrime; } public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.println("Enter Number:"); int num=input.nextInt(); if(isPrime(num)){ System.out.println("Number is Prime!"); } else{ System.out.println("Number is not prime!"); } } }

Thanks

Related Tutorials/Questions & Answers: