Download Write a Java Program to Print Fibonacci Series? import java.io

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Write a Java Program to Print Fibonacci Series?
import java.io.*;
import java.util.Scanner;
public class Fibonacci
{
public static void main(String[]args)
{
int num,first=0,second=1,i,next;
// Creating an obj of Scanner Class To accept data form keyboard
Scanner s = new Scanner(System.in);
System.out.println("Enter a number");
num = s.nextInt();
for(i=0;i<num;i++)
{
if ( i <= 1 )
{
next = i;
}else{
next = first + second;
first = second;
second = next;
}
System.out.println(next+" ");
}
}
}
Write a java program to print n prime numbers?
import java.io.*;
import java.util.Scanner;
public class PrimeNumber
{
public static void main(String args[])
{
int n, status = 1, num = 3;
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of prime numbers you want");
n = in.nextInt();
if (n >= 1)
{
System.out.println("First "+n+" prime numbers are :-");
System.out.println(2);
}
for ( int i = 2 ; i <=n ; )
{
for ( int j = 2 ; j <= Math.sqrt(num) ; j++ )
{
if ( num%j == 0 )
{
status = 0;
break;
}
}
if ( status != 0 )
{
System.out.println(num);
i++;
}
status = 1;
num++;
}
}
}
Write a Java Program to Check whether the number is prime or not?
import java.io.*;
import java.util.Scanner;
public class PrimeCheck
{
public static void main(String args[])
{
int i, n, flag=0;
Scanner s = new Scanner(System.in);
System.out.println("Enter a Number: ");
n = s.nextInt();
for(i=2;i<=n/2;++i)
{
if(n%i==0)
{
flag=1;
break;
}
}
if (flag==0)
System.out.println(n+"is a prime number.");
else
System.out.println(n+"is not a prime number.");
}
}
Write a Java Program to check whether the string is palindrome or not?
import java.io.*;
import java.util.Scanner;
class Palindrome
{
public static void main(String args[])
{
String original, reverse = "";
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to check if it is a palindrome");
original = in.nextLine();
int length = original.length();
for ( int i = length - 1; i >= 0; i-- )
reverse = reverse + original.charAt(i);
if (original.equals(reverse))
System.out.println("Entered string is a palindrome.");
else
System.out.println("Entered string is not a palindrome.");
}
}