This Program generates Fibonnaci Series for a given number of times.
Output: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55
[sourcecode language='java']
public class FibonnaciSeries {
/*
* Generates the Fibonnaci Series
*/
public void generateSeries(int num) {
int f1, f2 = 0, f3 = 1;
System.out.println(”fib(0) = ” + f2);
for (int i = 1; i <= num; i++) {
System.out.println("fib(" + i + ") = " + f3);
f1 = f2;
f2 = f3;
f3 = f1 + f2;
}
}
public static void main(String[] args) {
System.out.println("*****Fibonnaci Series*****");
FibonnaciSeries fb = new FibonnaciSeries();
fb.generateSeries(10);
}
}
[/sourcecode]
Highest Prime Number
Given a number this program generates the highest prime number.
Output: If the input is 25, the highest prime number close to 25 is 23. It prints 23.
[sourcecode language='java']
public class PrimeNumber {
/*
* Given a number, finds the highest prime number.
*/
public static int highestPrime(int n) {
int value = 1;
for (int i = 2; i <= n; i++) {
// System.out.println("n: " + n + " i: " + i + " mod: " + n%i);
if (i == n) {
value = n;
break;
}
if (n % i == 0) {
n--;
}
}
return value;
}
public static void main(String[] args) {
int value = highestPrime(25);
System.out.println("Prime:" + value);
}
}
[/sourcecode]
Triangle Printing
Given a number, this program prints the numbers in the right angle manner.
Output:
1
2 3
4 5 6
7 8 9 10
11 12 13 14
[sourcecode language='java']
public class TrianglePrinting {
/*
* Prints the numbers in the right angle manner.
*/
public static void trianglePrinting(int n) {
int counter = 0, i = 1;
while (i <= n) {
counter++;
for (int j = 0; j < counter; j++) {
if (i > n)
break;
System.out.print(i + ” “);
i++;
}
System.out.println();
}
}
public static void main(String[] args) {
trianglePrinting(14);
}
}
[/sourcecode]
Bookmark Article
62 Responses to “Logical Programs in Java – Fibonacci Series, Prime Numbers”
rajnish Says: November 21st, 2008 at 10:44 pm
please send me the java program of “prime number of Fibonacci series”
let us take e.g.
0, 1,1,2,3,5,8,13,………..
den prime number series will be- 3,5,13…………
anil kumar dhatwalia Says: December 22nd, 2008 at 12:02 pm
amazing coding sir thanx
anil kumar dhatwalia Says: December 22nd, 2008 at 12:07 pm
please send me th java programe “1 3, 2 4. 5 7, 6 8, 9 11, 10 12;”
sritej Says: January 2nd, 2009 at 1:06 pm
sir, i want simple logic of prime number programs
shweta Says: January 30th, 2009 at 12:52 pm
what is fibonacci series
Kiran Says: February 3rd, 2009 at 12:19 pm
hi, i want a logic program like
1
121
1231
12341
ramya Says: February 10th, 2009 at 5:10 pm
please send array using java
shankar Says: February 20th, 2009 at 2:15 pm
wowwwwwwwwwwwwwwww
nightfox Says: February 27th, 2009 at 8:48 pm
FibonacciSeries class would never compile , because of the blunders in the code
Hope u can the see double ‘n’ s used while creating a new instance of the class,
It becomes a minimal etiquette to check the code (run it in u r machine )
as many of the newbie’s may take as reference and copy the same code and try to run it
hope u take this in the +ve manner
Chandra Says: February 28th, 2009 at 7:00 pm
This is the output generated by the Fibonacci Series Program.
It is 100% correct. Let me know what is the output u r getting.
Let’s consider v1= 5, v2 = 10, then v3 = 15 (i.e v1 + v2)
v4 = 25 (i.e. v3 + v4) there on…..
Fibonacci Series
*****Fibonacci Series*****
fib(0) = 0
fib(1) = 1
fib(2) = 1
fib(3) = 2
fib(4) = 3
fib(5) = 5
fib(6) = 8
fib(7) = 13
fib(8) = 21
fib(9) = 34
fib(10) = 55
nightfox Says: February 28th, 2009 at 8:42 pm
Copy the same code that you have pasted ,u will get
FibonnaciSeries cannot be resolved
to a type
nightfox Says: February 28th, 2009 at 8:47 pm
Copy the same code that you have pasted ,u will get
FibonnaciSeries cannot be resolved to a type error
because of the spelling mistakes as u r class name is
FibonacciSeries and u r instantiating a new object with
FibonnaciSeries fb = new FibonnaciSeries(); (which contains single n and double c’s)
So i wondered did u ever run in u r machine
Moon Says: March 1st, 2009 at 10:38 am
Thanks for the correction. The program is updated.
Raja Says: April 29th, 2009 at 8:18 am
the second program will also be modified to be divided by 3 because if you take 21 as an example if you divide it by 2 it will say its prime number thou its not.. its divided by 3 .. I think we also need to add a if dividing the number with three..
venkat Says: May 28th, 2009 at 2:00 pm
/* 1 3, 2 4, 5 7, 6 8, 9 11, 10 12 */
import java.io.*;
public class oddevenpair {
public static void main(String a[]) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print(”Enter Number : “);
int n = Integer.parseInt(br.readLine());
int odd[] = new int[2];
int eve[] = new int[2];
int o = -1, e = -1;
StringBuffer sb = new StringBuffer();
for(int i=1; i<=n; i++) {
if(i%2==0) {
e++;
eve[e] = i;
}
if(e==1) {
sb.append(eve[0] + ” ” + eve[1] + “, “);
e = -1;
}
if(i%2!=0) {
o++;
odd[o] = i;
}
if(o==1) {
sb.append(odd[0] + ” ” + odd[1] + “, “);
o = -1;
}
}
System.out.println(sb.delete(sb.length()-2, sb.length()-1).toString());
}
}
venkat Says: May 28th, 2009 at 2:16 pm
/*
1
121
1231
12341
*/
import java.io.*;
public class inseq{
public static void main(String a[]) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print(”Enter a Number : “);
int n = Integer.parseInt(br.readLine()); //Read the numeric input from console
for(int i=0; i
System.out.print(1);
}
System.out.println(”\n”);
}
}
}
kavitha Says: June 17th, 2009 at 8:28 pm
please send the programs
1.display the factor numbers
2.display the prime number
3.display the 1 to 25 prime numbers
Nadhiya Says: June 27th, 2009 at 12:05 pm
send me a fibonaci series program in prime number
Nadhiya Says: June 27th, 2009 at 12:06 pm
program using fibonacci series in prime number
rohit Says: July 7th, 2009 at 9:06 am
want a program in java for a pyramid of 5 like:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Saurabh Says: July 22nd, 2009 at 9:44 am
HI i can do all types of pyramid programs !can u print a diamond in java
1
1 1
1 1
1 1
1 1
1 1
1
Saurabh Says: July 22nd, 2009 at 9:48 am
/* aprogram to display factors*/
import java.io.*;
public class inseq{
public static void main(String a[]) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print(”Enter a Number : “);
int n = Integer.parseInt(br.readLine());
int i,
for(i=1;i<=n;i++)
{
if(n%i==0)
{
System.out.print(i);
}
}
}
}
loucil Says: July 23rd, 2009 at 3:09 pm
hi, i want a program like this
12345
1234
123
12
1
Cristina Says: July 27th, 2009 at 11:20 pm
Hi, ccan I ask how can I output this:
25
1
20
15
3
10
4
5
5
Please, I realy need it. Please help me! T.T
indresh Says: August 10th, 2009 at 5:16 am
input string=acasereeeeeeee
hi,can i ask how can I output this
9
e
Please, I realy need it. Please help me!
java_cub Says: August 12th, 2009 at 4:28 am
hi frns, can any one give the code to get the following output
1
2 3
4 5 6
7 8 9 10
11 12 13
14 15
16
Nels Says: September 13th, 2009 at 1:06 am
Please send me the java program for Infix to postfix and prefix expression..
saksham Says: September 14th, 2009 at 7:05 am
input string=acasereeeeeeee
>input string=acasereeeeeeee
>hi,can i ask how can I output this
>9
>e
>Please, I realy need it. Please help me!
see you mean to input a String
you need to input it.
//suppose s is String
int hn=0;
int c=0;
int i,j;
for(i=0;i
saksham Says: September 14th, 2009 at 7:15 am
//remaining part
char ch=s.charAt(i);
//search for next now
for(j=0;j
char chr=s.charAt(j);
if(ch==chr) c++;
}
c–;
if(h
//i forgot to take v in start.
}
}
System.out.println(s.charAt(v)); //remember v
System.out.println(h);
saksham Says: September 14th, 2009 at 7:20 am
“hi frns, can any one give the code to get the following output
1
2 3
4 5 6
7 8 9 10
11 12 13
14 15
16
”
int c=1;
int i,j;
for(i=1;i<=4;i++)
{
for(j=1;j=1;i–)
{
for(j=1;j<=i;j++)
{
System.out.print(c+" ");
c++;
}
System.out.println();
}
tushar bhakte Says: September 23rd, 2009 at 9:26 am
i want to always need for this
swapnali Says: October 2nd, 2009 at 11:47 am
hi,
i want to knw the logic for permutation of string… in lexicographic order
like for String abc its permutation shud be
abc
acb
bac
bca
cab
cba
swapnali Says: October 2nd, 2009 at 11:49 am
hi,
i want to knw the logic for permutation of string… in lexicographic order
like for String abc its permutation shud be
abc
acb
bac
bca
cab
cba
Subrat Says: October 20th, 2009 at 11:52 am
Any one can give me the calculate programming codes without useing conditions,loopig,swith case & Ternary Operator ?
ceejay Says: October 27th, 2009 at 12:12 pm
can u please help me for the code of this java problem:
i want to display this 2 3 4
1+2 +3 +4 =1+4+27+256=288
thanks
ceejay Says: October 27th, 2009 at 12:13 pm
can u please help me for the code of this java problem:
i want to display this factorial
1+2 +3 +4 =1+4+27+256=288
thanks
Rajdeep Says: November 2nd, 2009 at 2:33 pm
SOLUTION:
1
1 2
1 2 3
1 2 3 4
….
import javax.swing.*;
public class NumAngle2 {
public static void main(String[] args) {
System.out.println(”This program prints numbers in the right angle manner.”);
String input = JOptionPane.showInputDialog(”Enter number?”);
int num = Integer.parseInt(input);
int counter = 1;
for(int i = 0; i < num; i++) {
counter++;
for(int j = 1; j < counter; j++) {
System.out.print(j + " ");
}
System.out.println();
}
}
}
Rajarshi Banerjee Says: November 10th, 2009 at 5:35 am
Hey guys I am from India and I guarantee you that this Fibonacci number series program will work:-
import java.io.*;
class fibonacci
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int num,ans1=1,ans2=0,ans3;
System.out.println(”Enter number”);
num=Integer.parseInt(br.readLine());
num=num-2;
System.out.print(ans2);
System.out.print(” “);
System.out.print(ans1);
System.out.print(” “);
while(num!=0)
{
ans3=ans1+ans2;
System.out.print(ans3);
System.out.print(” “);
ans2=ans1;
ans1=ans3;
num=num-1;
}
}
}
Shweta Says: November 16th, 2009 at 2:15 am
hi friendz
can anyone give me the code for the prog:
to accept a string from the user and change the beginning letter of every word with a CAPITAL letter.
Rajdeep Says: November 19th, 2009 at 4:12 am
/* 1
1 2 1
1 2 3 1
1 2 3 4 1
*/
import javax.swing.*;
public class OneAngle {
public static void main(String[] args) {
String input = JOptionPane.showInputDialog(”Enter num?”);
int n = Integer.parseInt(input);
int count = 0;
for(int i = 1; i <= n; i++) {
count++;
for(int j = 1; j 1) System.out.print(1);
System.out.println();
}
}
}
sany Says: November 20th, 2009 at 1:20 am
can you help me how to figure out this java problem.. here is the output..
***
* *
* *
* *
* *
***
sany Says: November 20th, 2009 at 1:21 am
___***
__*___*
_*_____*
_*_____*
__*___*
___***
abhi Says: November 24th, 2009 at 10:48 pm
pls help, code required for the following
number to word
eg:- 1245
o/p:- one thousand two hundred forty five
length of the input max 9.
sammy Says: December 9th, 2009 at 11:50 pm
F SERIES
X=0,Y=1;
PRINTF(”X=%D Y=%D”,X,Y);
Z=X+Y;
PRINTF(”Z=%D”,Z);
X=Y;
Y=Z;
Alok Says: December 24th, 2009 at 1:23 am
Hi Shweta,
Ans to Q39:
The best way is to use String.split().this returns array of strings(words in statement).Pick every word or index in array and modify first char using char(0) and toUppercase and join all array elements again. Hope that makes sense
pritam saraf Says: December 28th, 2009 at 4:37 am
Q 22. Using switch case perform array operation
A. sorting of array no
B. searching a particular no (taken as an input)
C. Addition of total element in array.
Q.23. Write a program that accepts a shopping list of five items from the command line and stores them in a vector.
Q.24. Write a program, which will read a string and rewrite it in the alphabetical order. For example, the word STRING should be written as GNIRST
Q 25. Write a program, which will take three strings from the command line and display the number of characters in each string.
Q.26. Write a program to find out prime no from 1 to 100
Q.27. Write a program, which will read a text and count all occurrences of a particular letter.
Q.28. Write a program to produce the following form.
1
0 1
1 0 1
0 1 0 1
1 0 1 0 1 1
2 2
3 3 3
4 4 4 4
5 5 5 5 5 $ $ $ $ $
$ $ $ $
$ $ $
$ $
$
Q.29. Write a utility class to sort the employees according to their names.
Q.30. Write a program to calculate harmonic value. e.g 1+1/2+1/3……1/n
Q.31. Admission to a professional course is subject to the following conditions
a. Marks in mathematics >=60
b. Marks in physics >=50
c. Marks in chemistry >=40
d. Total in all three subjects >=120
OR
Total in mathematics and physics >=150
Given the marks in the three subjects, write a program to process the application to list eligible candidates.
Q.32. Write a class Student; store it in the package stud. Write a class Batch with information about subject, faculty, and timing. Store it in the package bat. Use the class Batch to set information in the Student class.
Q.33 Write a program to display Fibonacci series.e.g.. 1 1 2 3 5 8 13 21……n.
pritam saraf Says: December 28th, 2009 at 4:38 am
Q 22. Using switch case perform array operation
A. sorting of array no
B. searching a particular no (taken as an input)
C. Addition of total element in array.
Q.23. Write a program that accepts a shopping list of five items from the command line and stores them in a vector.
Q.24. Write a program, which will read a string and rewrite it in the alphabetical order. For example, the word STRING should be written as GNIRST
Q 25. Write a program, which will take three strings from the command line and display the number of characters in each string.
Q.26. Write a program to find out prime no from 1 to 100
Q.27. Write a program, which will read a text and count all occurrences of a particular letter.
Q.28. Write a program to produce the following form.
1
0 1
1 0 1
0 1 0 1
1 0 1 0 1
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
$ $ $ $ $
$ $ $ $
$ $ $
$ $
$
Q.29. Write a utility class to sort the employees according to their names.
Q.30. Write a program to calculate harmonic value. e.g 1+1/2+1/3……1/n
Q.31. Admission to a professional course is subject to the following conditions
a. Marks in mathematics >=60
b. Marks in physics >=50
c. Marks in chemistry >=40
d. Total in all three subjects >=120
OR
Total in mathematics and physics >=150
Given the marks in the three subjects, write a program to process the application to list eligible candidates.
Q.32. Write a class Student; store it in the package stud. Write a class Batch with information about subject, faculty, and timing. Store it in the package bat. Use the class Batch to set information in the Student class.
Q.33 Write a program to display Fibonacci series.e.g.. 1 1 2 3 5 8 13 21……n.
rani Says: January 2nd, 2010 at 2:59 am
PROGRAM TO PRINT A TRIANGLE AS FOLLOWS
1
12
123
1234
12345
rani Says: January 2nd, 2010 at 3:00 am
I NEED A JAVA CODE FOR 48TH QUESTION
psallm Says: January 13th, 2010 at 6:36 am
hi, yeah i need a fibonacii number series in loop.. can you help me…???
mehul viramgama Says: January 17th, 2010 at 10:43 am
hi rani ,
PROGRAM TO PRINT A TRIANGLE AS FOLLOWS
1
12
123
1234
12345
import java.io.*;
class ptn8
{
public static void main(String arg[])
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
System.out.print(j+" ");
}
System.out.println("");
}
}
}
lavanya Says: February 5th, 2010 at 1:10 pm
public class Fibonacci {
public static void main(String []args)
{
int f1=0;
int f2=0;
int f3=1;
System.out.println(”The Fibo series is ::”);
System.out.println(f2);
for(int i=0;i<10;i++)
{
System.out.println(f3);
f1=f2;
f2=f3;
f3=f1+f2;
}
}
}
lavanya Says: February 5th, 2010 at 1:13 pm
/* Prime nos generation as required by user::*/
import java.util.*;
import java.io.*;
public class prime_nos {
public static void main(String []args) throws IOException
{
Scanner scan = new Scanner(System.in);
int i,c,n;
System.out.println(”Enter the starting range ::”);
int a=scan.nextInt();
System.out.println(”Enter the ending range ::”);
int b=scan.nextInt();
System.out.println(”The prime nos are ::”);
for(n=a;n{
c=0;
for(i=1;i<=n;i++)
{
if(n%i==0)
c++;
}
if(c==2)
{
System.out.println(n);
}
}
}
}
Elangovan ARUMUGAM Says: February 6th, 2010 at 1:54 pm
package com.maths;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/**
* Description: By definition, the first two Fibonacci numbers are 0 and 1, and
* each remaining number is the sum of the previous two. Some sources omit the
* initial 0, instead beginning the sequence with two 1s.
*
* In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the
* recurrence relation Fn = Fn-1 + Fn-2 like F0= 0 and F1 = 1
*
* F-n = (-1)power(n+1) * Fn
*
* Fibonacci Series: … -8, -5, -3, 2, -1, 1, 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,
* 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765
*
* F(n+2) – F(n+1) – F(n) = 0 is similar to the defining equation of the golden
* ratio in the form X*X -X -1 = 0;
*
*
*
* @author elangovan.arumugam
*
*/
public class FibonacciTest {
/**
*
*/
public FibonacciTest() {
}
/**
* @param args
*/
public static void main(String[] args) {
InputStreamReader inStream = new InputStreamReader(System.in);
BufferedReader bufRead = new BufferedReader(inStream);
int inputNumber = 0;
List arrayList = null;
try {
int fib1, fib2, fib;
System.out.println(”*****************************************”);
System.out.println(” Welcome to the Fibonacci Number Program “);
System.out.println(”*****************************************”);
System.out.println(”Which Fibonacci Number do you want?”);
String inputNumberStr = bufRead.readLine();
inputNumber = Integer.parseInt(inputNumberStr);
// System.out.println(”Square Root of ” + inputNumber + ” : ”
// + Math.sqrt(inputNumber));
fib1 = 1;
fib2 = 0;
switch (inputNumber) {
case 0:
System.out.println(”Sorry, no result for f(0)”);
break;
case 1:
System.out.println(”Fib(1) = 0″);
break;
case 2:
System.out.println(”Fib(1) = 0″);
System.out.println(”Fib(2) = 1″);
break;
default:
arrayList = new ArrayList();
arrayList.add(”Fib(” + (1) + “) = ” + 0);
arrayList.add(”Fib(” + (2) + “) = ” + 1);
System.out.print(”0, 1″);
for (int i = 1; i <= inputNumber – 2; i++) {
// fib = 1 + 0
fib = fib1 + fib2;
// fib2 = 1
fib2 = fib1;
// fib1 = 1
fib1 = fib;
System.out.print(", ");
System.out.print(fib);
arrayList.add("Fib(" + (i + 2) + ") = " + fib);
}
}
System.out.print("\n");
System.out.println("Fibonacci Series :");
System.out.print(arrayList);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Thulasipathi naidu Says: February 8th, 2010 at 1:05 am
how to convert input number 2542 .then output is disply like
two thousan five hundred fourty two.
sagnik das Says: February 28th, 2010 at 4:29 am
1
12
123
1234
123
12
1
i know this program but i need something different
like this
5
5 4
5 4 3
5 4 3 2
5 4 3 2 1
5 4 3 2
5 4 3
5 4
5
can u give me the program for this
ravi Says: March 5th, 2010 at 2:21 am
write a program in java that accept a value n and calculate the sum of all odd number from n.
vikram Says: March 25th, 2010 at 8:14 pm
write a program for binary search
Surender Says: March 27th, 2010 at 1:06 am
1 3 2 4 5 7 6 8 9 11 10 12
answer to question 15th.
It is so simple
/*
out put such as below
1 3 2 4 5 7 6 8 9 11 10 12
*/
class PrintXY {
PrintXY(){
for(int i=1;i<=10;i++)
{
if(i%2!=0)
{
System.out.print(i+" "+(i+2)+" ");
}
else
{
System.out.print(" "+i+" "+(i=i+2)+" ");
}
}
}
public static void main(String[] args) {
new PrintXY();
}
}
laxmi Says: March 29th, 2010 at 3:19 am
plz send me a pragram that accepts a character array and print the * for each character and also count the repeatable character and mark it by *.
Input—{a,b,a,b,c,d,a,c,a,b,d,f}—
output—
a–****
b–***
c–**
d–**
f–*
alok Says: March 30th, 2010 at 1:56 am
these codes are useful but need some improvement
alok Says: March 30th, 2010 at 1:58 am
need some improvemen
No comments:
Post a Comment