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

Java Programs

Uploaded by

Irfan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Java Programs

Uploaded by

Irfan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Fibonacci Series in Java without using recursion

class Main{

public static void main(String args[])

int n1=0,n2=1,n3,i,count=10;

System.out.print(n1+" "+n2);//printing 0 and 1

for(i=2;i<count;++i)//loop starts from 2 because 0 and 1 are already printed

n3=n1+n2;

System.out.print(" "+n3);

n1=n2;

n2=n3;

}}

Fibonacci Series using recursion in java


class Main2{

static int n1=0,n2=1,n3=0;


static void printFibonacci(int count){

if(count>0){

n3 = n1 + n2;

n1 = n2;

n2 = n3;

System.out.print(" "+n3);

printFibonacci(count-1);

public static void main(String args[]){

int count=10;

System.out.print(n1+" "+n2);//printing 0 and 1

printFibonacci(count-2);//n-2 because 2 numbers are already printed

Prime Number Program in Java


Prime number is a number that is greater than 1 and divided by 1 or itself only. In other
words, prime numbers can't be divided by other numbers than itself or 1. For example 2,
3, 5, 7, 11, 13, 17.... are the prime numbers.
import java.io.*;

public class Main{

public static void main(String args[]) throws IOException {

InputStreamReader r=new InputStreamReader(System.in);

BufferedReader bufferedreader = new BufferedReader(r);


System.out.println("Enter a number");
int n = Integer.parseInt(bufferedreader.readLine());

int i,m=0,flag=0;

m=n/2;

if(n==0||n==1){

System.out.println(n+" is not prime number");

}else{

for(i=2;i<=m;i++){

if(n%i==0){

System.out.println(n+" is not prime number");

flag=1;

break;

if(flag==0) { System.out.println(n+" is prime number"); }

}//end of else
}

Prime Number Program using Method in Java


import java.io.*;

public class Main {

static void checkPrime(int n){

int i,m=0,flag=0;

m=n/2;

if(n==0||n==1){

System.out.println(n+" is not prime number");

}else{

for(i=2;i<=m;i++){

if(n%i==0){

System.out.println(n+" is not prime number");

flag=1;

break;

if(flag==0) { System.out.println(n+" is prime number"); }


}//end of else

public static void main(String args[]) throws IOException {

InputStreamReader r=new InputStreamReader(System.in);

BufferedReader bufferedreader = new BufferedReader(r);


System.out.println("Enter a number");
int n = Integer.parseInt(bufferedreader.readLine());

checkPrime(n);

Through Scanner

import java.util.Scanner;

import java.util.Scanner;

public class Main {

public static void main(String[] args) {


Scanner s = new Scanner(System.in);
System.out.print("Enter a number : ");
int n = s.nextInt();
if (isPrime(n)) {
System.out.println(n + " is a prime number");
} else {
System.out.println(n + " is not a prime number");
}
}

public static boolean isPrime(int n) {


if (n <= 1) {
return false;
}
for (int i = 2; i < Math.sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
}

Find prime numbers between two numbers

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Enter the first number : ");
int start = s.nextInt();
System.out.print("Enter the second number : ");
int end = s.nextInt();
System.out.println("List of prime numbers between " + start + " and " + end);
for (int i = start; i <= end; i++) {
if (isPrime(i)) {
System.out.println(i);
}
}
}
public static boolean isPrime(int n) {
if (n <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
}
Palindrome Program in Java
A palindrome number is a number that is same after reverse. For example 545, 151,
34543, 343, 171, 48984 are the palindrome numbers. It can also be a string like LOL,
MADAM etc.

Palindrome number algorithm

● Get the number to check for palindrome

● Hold the number in temporary variable

● Reverse the number

● Compare the temporary number with reversed number

● If both numbers are same, print "palindrome number"

● Else print "not palindrome number"

import java.util.*;

class Main

public static void main(String args[])

String original, reverse = ""; // Objects of String class

Scanner in = new Scanner(System.in);

System.out.println("Enter a string/number 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/number is a palindrome.");

else

System.out.println("Entered string/number isn't a palindrome.");

Print Pattern in Java


import java.util.*;
class Main {
public static void main(String args[])

{
//i for rows and j for columns
//row denotes the number of rows you want to print

Scanner s = new Scanner(System.in);


System.out.print("Enter the number of rows : ");
int row = s.nextInt();
int i, j;
//outer loop for rows
for(i=0; i<row; i++)
{
//inner loop for columns
for(j=0; j<=i; j++)
{
//prints stars
System.out.print("* ");
}
//throws the cursor in a new line after printing each line
System.out.println();
}
}
}

You might also like