SlideShare a Scribd company logo
www.javaassignmenthelp.net
Most Popular Java Problems and Solutions
If looking for Java problems and their solutions, check out this page and you will learn of the
most popular ones that you might want to learn about so that you can start memorizing and
applying them when needed.
List of Java Problems and Solutions
1. Create a Java program displaying Hello World on screen
Solution:
public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello World.");
}
}
www.javaassignmenthelp.net
2. Create a Java program displaying asterisk pattern.
Solution:
*****
*****
*****
*****
*****
public class JavaExercises
{
public static void main(String[] args)
{
printAsterisk();
}
static void printAsterisk(){
System.out.println("*****");
System.out.println("*****");
System.out.println("*****");
System.out.println("*****");
System.out.println("*****");
}
}
3. Create a Java program declaring 2 integer variables, 1 float variable as
well as 1 string variable, then assign 10, 12.5 as well as “Java
programming” respectively. After that, display the values on screen.
Solution:
public class JavaExercises
{
www.javaassignmenthelp.net
public static void main(String[] args)
{
accessVariables();
}
static void accessVariables(){
int x;
float y;
String s;
x = 10;
y = 12.5f;
s = "Java programming";
System.out.println(x);
System.out.println(y);
System.out.println(s);
}
}
4. Create a Java program with the use of the BufferedReader class in
prompting user to input her or his name. The output will be like this:
Hello Dara!
Solution:
import java.io.*;
public class JavaExercises
{
public static void main(String[] args)
{
printName();
}
static void printName(){
String pname=null;
try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter your name:");
pname=br.readLine();
}catch(IOException e){}
www.javaassignmenthelp.net
System.out.println("Hello "+pname);
}
}
5. Write an example method printing numbers one to ten.
Solution:
oneToTen()
*** Output ***
1
2
3
4
5
6
7
8
9
10
6. Write an example method printing positive odd numbers that is less
than 20.
Solution:
oddNumbers()
*** Output ***
1
3
www.javaassignmenthelp.net
5
7
9
11
13
15
17
19
7. Write an example method printing square numbers up to one hundred.
Solution:
squares()
*** Output ***
1
4
9
16
25
36
49
64
81
100
8. Write an example loop in printing out 4 random integers between one
and ten.
www.javaassignmenthelp.net
Solution:
random4()
*** Output ***
3
5
2
8
9. Write an example method printing out positive even numbers that is less
than n.
Solution:
1: even(20)
2: *** Output ***
3: 2
4: 4
5: 6
6: 8
7: 10
8: 12
9: 14
10: 16
11: 18
10.Write an example method printing out powers of two from 21
up to 2n.
Solution:
powers(8)
www.javaassignmenthelp.net
: *** Output ***
2
4
8
16
32
64
128
256
11. Create an example program that outputs “ Are we there yet?” Wait for
the input and if it say “yes”, then the outputs program is “good” and
then exit.
Solution:
"Are we there yet?"
No
"Are we there yet?"
Spoons
"Are we there yet?"
Yes
Good!
12. Create an example method using nested loops in producing a pattern.
Solution:
triangle()
*** Output ***
www.javaassignmenthelp.net
*
**
***
****
*****
13. Create an example method printing out 4x4 table square.
Solution:
tableSquare()
*** Output ***
A 4 x 4 table square
| 1 | 2 | 3 | 4 |
| 1 | 2 | 3 | 4 |
| 2 | 4 | 6 | 8 |
| 3 | 6 | 9 | 12 |
| 4 | 8 | 12 | 16 |
14. Write an example extending your answer to last question producing
method printing out n x n table square.
Solution:
tableSquares(6)
*** Output ***
A 6 x 6 table square
| 1 | 2 | 3 | 4 | 5 | 6 |
| 2 | 4 | 6 | 8 | 10 | 12 |
| 3 | 6 | 9 | 12 | 15 | 18 |
www.javaassignmenthelp.net
| 4 | 8 | 12 | 16 | 20 | 24 |
| 5 | 10 | 15 | 20 | 25 | 30 |
| 6 | 12 | 18 | 24 | 30 | 36 |
15. Write an example method printing out string array, 1 element for every
line
Solution:
String [] breakfast = {"Sausage", "Eggs", "Beans", "Bacon", "Tomatoes", "Mushrooms"};
printArray(breakfast)
*** Output ***
Sausage
Eggs
Beans
Bacon
Tomatoes
Mushrooms
16. Create an example method returning to the last element of the string
array.
Solution:
String [] breakfast = {"Sausage", "Eggs", "Beans", "Bacon", "Tomatoes", "Mushrooms"}
System.out.println(lastElement(breakfast));
*** Output ***
Mushrooms
www.javaassignmenthelp.net
17. Create an example method returning to the last, but 1 element of the
string array.
Solution:
String [] breakfast = {"Sausage", "Eggs", "Beans", "Bacon", "Tomatoes", "Mushrooms"}
System.out.println(lastButOne(breakfast));
*** Output ***
Tomatoes
18. Create an example method reversing elements of the Array.
Solution:
String [] breakfast = {"Sausage", "Eggs", "Beans", "Bacon", "Tomatoes", "Mushrooms"};
System.out.println(reverse(breakfast));
*** Output ***
: Mushrooms
: Tomatoes
: Bacon
: Beans
: Eggs
: Sausage
: Tails
19. Write a method testing to see if the array is palindromic. For example,
elements are same when it is reversed.
Solution:
www.javaassignmenthelp.net
String [] palindromic = {"Sausage", "Eggs", "Beans", "Beans", "Eggs", "Sausage"};
String [] breakfast = {"Sausage", "Eggs", "Beans", "Bacon", "Tomatoes", "Mushrooms"}
System.out.println(isPalindrome(palindromic));
System.out.println(isPalindrome(breakfast));
*** Output ***
True
False
20. Write a method printing out int array with the consecutive duplicated
eliminated.
Solution:
int [] nums = {1,1,3,3,3,2,2,2,1,1,1,1,4,4,4,4};
compress(nums)
*** Output ***
5: 1
: 3
: 2
: 1
: 4
: 1
: 1
21. Pack consecutive duplicated of the char array into the strings.
Solution:
www.javaassignmenthelp.net
char [] letters = {'a' 'a' 'a' 'a' 'b' 'c' 'c' 'a' 'a' 'd' 'e' 'e' 'e' 'e'};
pack(nums)
*** Output ***
: aaaa, b, cc, aa, d, eeee
22. Create a method returning the number of words in the string.
Solution:
String s = "I never saw a purple cow"
System.out.println(countWords(s));
*** Output ***
6
23. Create a method counting instances of letter “e” in the string.
Solution:
String s = "I never saw a purple cow"
System.out.println(countEs(s));
*** Output ***
3
24. Write a method returning number of the alphanumeric characters in
string.
Solution:
String s = "1984 by George Orwell."
www.javaassignmenthelp.net
System.out.println(countChars(s));
*** Output ***
18
25. Create a method returning the string, reversed.
Solution:
String s = "I never saw a purple cow"
System.out.println(reverse(s));
*** Output ***
woc elprup a was reven I
26. Create a method testing if the string is palindrome.
Solution:
String sentence = "I never saw a purple cow"
String palindrome = "rotavator"
System.out.println(isPalindrome(sentence));
System.out.println(isPalindrome(palindrome));
*** Output ***
False
True
27.Create improved palindrome that disregard punctuation, case and
spaces as well as recognize sentences like “ A man, a plan, a canal,
Panama!” as palindrome.
www.javaassignmenthelp.net
Solution:
String s = "I never saw a purple cow"
String p = "Rise to vote, Sir!"
System.out.println(isPalindrome(s));
System.out.println(isPalindrome(p));
*** Output ***
False
True
28. Write a method replacing vowels in the word with the asterisks.
Solution:
String s = "I never saw a purple cow"
star(s)
*** Output ***
* n*v*r s*w * p*rpl* c*w
29. Create method spelling out words. For example, This will become T-H-I-
S.
Solution:
String s = "I never saw a purple cow"
spellOut(s)
*** Output ***
I N-E-V-E-R S-A-W A P-U-R-P-L-E C-O-W
www.javaassignmenthelp.net
30.In simple substitution cipher, A=1, B=2, C=3 and others. Create a
method encoding sentence with the use of cipher.
Solution:
String s = "Hello World"
encode(s)
*** Output ***
8,5,12,12,15 23,15,18,12,4
31. Create decoder method for the substitution cipher.
Solution:
String s = "9 14,5,22,5,18 19,1,23 1 16,21,18,16,12,5 3,15,23"
decode(s)
*** Output ***
i never saw a purple cow
32. Determine if the given integer number is a prime.
Solution:
public class NNArithmetic {
NNArithmetic()
{
System.out.println(isPrime(7));
www.javaassignmenthelp.net
}
boolean isPrime(int a)
{
//Special cases
if(a==1) return false;
if(a==2) return true;
boolean prime = true;
int count = 2;
do
{
if (a%count==0)
{
prime = false;
}
count++;
} while(prime == true && count*count <=a);
return prime;
}
public static void main(String[] args)
www.javaassignmenthelp.net
{
new NNArithmetic();
}
}
33. Use the Euclid’s Algorithm in determining Greatest Common Divisor of
2 positive integer numbers.
Solution:
public class NNArithmetic {
NNArithmetic()
{
System.out.println(gcd (1071, 462));
System.out.println(gcd (6, 35));
System.out.println(gcd (36, 63));
}
int gcd(int a, int b)
{
if(b>a) //Make sure the first number is largest
{
int temp = a;
a = b;
www.javaassignmenthelp.net
b = temp;
}
while(b>0)
{
int remainder = a%b;
a = b;
b = remainder;
}
return a;
}
public static void main(String[] args)
{
new NNArithmetic();
}
}
34. Determine whether 2 positive integer numbers are co-prime. 2 numbers
are co-prime if the greatest common divisor equals to 1.
Solution:
public class NNArithmetic {
NNArithmetic()
www.javaassignmenthelp.net
{
System.out.println(isCoprime(35,64));
}
int gcd(int a, int b)
{
if(b>a) //Make sure the first number is largest
{
int temp = a;
a = b;
b = temp;
}
while(b>0)
{
int remainder = a%b;
a = b;
b = remainder;
}
return a;
}
www.javaassignmenthelp.net
boolean isCoprime(int a, int b)
{
return (gcd(a,b) ==1) ? true : false;
}
public static void main(String[] args)
{
new NNArithmetic();
}
}
35. Calculate Euler’s totient function phi(m). For instance, m = 10: r =
1,3,7,9; thus phi(m) = 4. Note the special case: phi(1) = 1.
Solution:
public class NNArithmetic {
NNArithmetic()
{
System.out.println(phi(10));
}
int gcd(int a, int b)
{
if(b>a) //Make sure the first number is largest
{
www.javaassignmenthelp.net
int temp = a;
a = b;
b = temp;
}
while(b>0)
{
int remainder = a%b;
a = b;
b = remainder;
}
return a;
}
boolean isCoprime(int a, int b)
{
return (gcd(a,b) ==1) ? true : false;
}
int phi(int m)
{
if (m==1) return 1; //Special Case
www.javaassignmenthelp.net
int phi = 0;
for(int i = 1; i<m; i++)
{
if (isCoprime(m,i)) phi++;
}
return phi;
}
public static void main(String[] args)
{
new NNArithmetic();
}
}
36. Create a method printing prime factors of given positive integer.
Solution:
public class NNArithmetic {
NNArithmetic()
{
printPrimeFactors(315);
printPrimeFactors(98);
}
void printPrimeFactors(int n)
www.javaassignmenthelp.net
{
for(int i = 2; i*i<=n; i++)
{
while(n%i == 0)
{
System.out.print(i + " ");
n = n/i;
}
}
if(n>1) System.out.print(n + "n");
else System.out.println("");
}
public static void main(String[] args)
{
new NNArithmetic();
}
}
37. With the given integers range by its lower limit and upper limit,
construct a list of the prime numbers within that range.
Solution:
public class NNArithmetic {
NNArithmetic()
www.javaassignmenthelp.net
{
printPrimesInRange(10,20);
}
boolean isPrime(int a)
{
//Special cases
if(a==1) return false;
if(a==2) return true;
boolean prime = true;
int count = 2;
do
{
if (a%count==0)
{
prime = false;
}
count++;
} while(prime == true && count*count <=a);
return prime;
}
www.javaassignmenthelp.net
void printPrimesInRange(int a , int b)
{
for (int i = a; i<=b; i++)
{
if (isPrime(i)) System.out.print(i + " ");
}
System.out.println("");
}
public static void main(String[] args)
{
new NNArithmetic();
}
}
38. Create predicate in finding 2 prime numbers summing up to given even
number.
Solution:
public class NNArithmetic {
NNArithmetic()
{
printGoldbachPair(28);
printGoldbachPair(1856);
}
www.javaassignmenthelp.net
boolean isPrime(int a)
{
//Special cases
if(a==1) return false;
if(a==2) return true;
boolean prime = true;
int count = 2;
do
{
if (a%count==0)
{
prime = false;
}
count++;
} while(prime == true && count*count <=a);
return prime;
}
int [] goldbachPair(int n)
{
int count = 2;
int [] pair = new int[2];
www.javaassignmenthelp.net
boolean foundPair = false;
while(foundPair == false && count <= n/2)
{
if(isPrime(count) && isPrime(n-count))
{
foundPair = true;
pair [0] = count;
pair [1] = (n-count);
}
count++;
}
return pair;
}
void printGoldbachPair(int n)
{
int [] pair = goldbachPair(n);
System.out.println(pair[0] + " + " + pair [1] + " = " + n);
}
public static void main(String[] args)
{
new NNArithmetic();
www.javaassignmenthelp.net
}
}
39. With the given integers by lower limit and upper limit, print list of even
numbers as well as their Goldbach composition. Find out how many
cases there are in range of 2..3000.
Solution:
public class NNArithmetic {
NNArithmetic()
{
printGoldbachList(9, 20);
printGoldbachList(1,2000,50);
}
boolean isPrime(int a)
{
//Special cases
if(a==1) return false;
if(a==2) return true;
boolean prime = true;
int count = 2;
do
www.javaassignmenthelp.net
{
if (a%count==0)
{
prime = false;
}
count++;
} while(prime == true && count*count <=a);
return prime;
}
int [] goldbachPair(int n)
{
int count = 2;
int [] pair = new int[2];
boolean foundPair = false;
while(foundPair == false && count <= n/2)
{
if(isPrime(count) && isPrime(n-count))
{
foundPair = true;
pair [0] = count;
pair [1] = (n-count);
}
count++;
www.javaassignmenthelp.net
}
return pair;
}
void printGoldbachPair(int n)
{
int [] pair = goldbachPair(n);
System.out.println(pair[0] + " + " + pair [1] + " = " + n);
}
void printGoldbachList(int a, int b)
{
if(a%2==1) a++; // make sure start on even number
for (int i = a ; i<=b ; i+=2)
{
printGoldbachPair(i);
}
}
void printGoldbachList(int a, int b, int min)
{
if(a%2==1) a++; // make sure start on even number
for (int i = a ; i<=b ; i+=2)
www.javaassignmenthelp.net
{
int [] pair = goldbachPair(i);
if(pair[0] > min && pair[1] > min) printGoldbachPair(i);
}
}
public static void main(String[] args)
{
new NNArithmetic();
}
}
40. Create a method printing out the string array, 1 element for every line.
Solution:
public class NNArrays {
NNArrays()
{
String [] breakfast = {"Sausage", "Eggs", "Beans", "Bacon", "Tomatoes", "Mushrooms"};
printArray(breakfast);
//Alternative Method
printArrayAlt(breakfast);
}
www.javaassignmenthelp.net
void printArray(String [] array)
{
for(int i = 0; i<array.length; i++)
{
System.out.println(array[i]);
}
}
//Alternative method using for each loop
void printArrayAlt(String [] array)
{
for(String s: array)
{
System.out.println(s);
}
}
public static void main(String[] args)
{
new NNArrays();
}
}
41. Write a method returning last element of the string array.
www.javaassignmenthelp.net
Solution:
public class NNArrays {
NNArrays()
{
String [] breakfast = {"Sausage", "Eggs", "Beans", "Bacon", "Tomatoes", "Mushrooms"};
System.out.println(lastElement(breakfast));
}
String lastElement(String [] array)
{
return array[array.length-1];
}
public static void main(String[] args)
{
new NNArrays();
}
}
42. Create a method returning last, but 1 element of the string array.
Solution:
public class NNArrays {
www.javaassignmenthelp.net
NNArrays()
{
String [] breakfast = {"Sausage", "Eggs", "Beans", "Bacon", "Tomatoes", "Mushrooms"};
//Added two extra arrays for testing
String [] coin = {"Heads", "Tails"};
String [] lonely = {"solo"};
System.out.println(lastButOne(breakfast));
System.out.println(lastButOneSafer(breakfast));
System.out.println(lastButOneSafer(coin));
System.out.println(lastButOneSafer(lonely));
}
String lastButOne(String [] array)
{
return array[array.length-2];
}
// The previous method will throw an
// arrayIndexOutOfBoundsException for arrays of length <2.
// This method will return an empty string if length <2
String lastButOneSafer(String [] array)
{
www.javaassignmenthelp.net
if (array.length<2)
{
return "";
}
else
{
return array[array.length-2];
}
}
public static void main(String[] args)
{
new NNArrays();
}
}
43. Create a method returning elements of the Array in a reverse order.
Solution:
public class NNArrays {
NNArrays()
{
String [] breakfast = {"Sausage", "Eggs", "Beans", "Bacon", "Tomatoes", "Mushrooms"};
//Added two extra arrays for testing
String [] coin = {"Heads", "Tails"};
www.javaassignmenthelp.net
String [] lonely = {"solo"};
printArray(reverse(breakfast));
printArray(reverse(coin));
printArray(reverse(lonely));
}
void printArray(String [] array)
{
for(int i = 0; i<array.length; i++)
{
System.out.println(array[i]);
}
}
String [] reverse(String [] array)
{
for(int i = 0; i<array.length/2;i++)
{
String temp = array[i];
array[i] = array[array.length-i-1];
array[array.length-i-1] = temp;
}
www.javaassignmenthelp.net
return array;
}
public static void main(String[] args)
{
new NNArrays();
}
}
44. Create a method testing to see if the array is a palindromic. For
instance, elements are same when it is being reversed.
Solution:
public class NNArrays {
NNArrays()
{
String [] breakfast = {"Sausage", "Eggs", "Beans", "Bacon", "Tomatoes", "Mushrooms"};
String [] lonely = {"solo"};
String [] palindromic = {"Sausage", "Eggs", "Beans",
"Beans", "Eggs", "Sausage"};
System.out.println(isPalindrome(palindromic));
System.out.println(isPalindrome(breakfast));
System.out.println(isPalindrome(lonely));
}
www.javaassignmenthelp.net
boolean isPalindrome(String [] array)
{
boolean isPal = true;
for (int i = 0; i<array.length/2; i++)
{
if (!array[i].equals(array[array.length -i -1]))
isPal = false;
}
return isPal;
}
// The following method doesn't work.
// Need to compare individual elements
boolean isPalindromeWrong(String [] array)
{
if (array.equals(reverse(array)))
{
return true;
}
else
{
www.javaassignmenthelp.net
return false;
}
}
String [] reverse(String [] array)
{
for(int i = 0; i<array.length/2;i++)
{
String temp = array[i];
array[i] = array[array.length-i-1];
array[array.length-i-1] = temp;
}
return array;
}
public static void main(String[] args)
{
new NNArrays();
}
}
45. Create a method printing out int array with the consecutives duplicates
eliminated.
Solution:
www.javaassignmenthelp.net
public class NNArrays {
NNArrays()
{
int [] nums = {1,1,3,3,3,2,2,2,1,1,1,1,4,4,4,4};
int [] num2 = {1,1};
int [] num1 = {1};
compress(nums);
compress(num2);
compress(num1);
}
void compress(int [] array)
{
System.out.println(array[0]);
for (int i = 1; i<array.length; i++)
{
if (array[i]!=array[i-1])
{
System.out.println(array[i]);
}
}
www.javaassignmenthelp.net
}
public static void main(String[] args)
{
new NNArrays();
}
}
46. Create a method printing 2D array of any sizes, then test it with the use
of 3 x 3 tic-tact-toe grid.
Solution:
public class NNStringArray {
NNStringArray()
{
tictactoe();
}
void tictactoe()
{
String [][] grid = new String[3][3];
for(int i = 0; i<grid.length; i++)
{
www.javaassignmenthelp.net
for (int j = 0; j<grid.length; j++)
{
grid[i][j] = "X";
}
}
printArray(grid);
}
void printArray(String [][] grid)
{
for(int i = 0; i<grid.length; i++)
{
for(int j = 0; j<grid.length; j++)
{
System.out.print(" | " + grid[i][j]);
}
System.out.println(" | ");
}
}
public static void main(String[] args) {
// TODO code application logic here
new NNStringArray();
www.javaassignmenthelp.net
}
}
47. Create a 8 x 8 array representing chess board. You should print out
array with the alternate “X” and “O”.
Solution:
public class NNStringArray {
NNStringArray()
{
chess();
}
void printArray(String [][] grid)
{
for(int i = 0; i<grid.length; i++)
{
for(int j = 0; j<grid.length; j++)
{
System.out.print(" | " + grid[i][j]);
}
System.out.println(" | ");
}
}
www.javaassignmenthelp.net
void chess()
{
String [][] grid = new String[8][8];
boolean fill = true;
for(int i = 0; i<grid.length; i++)
{
for (int j = 0; j<grid.length; j++)
{
grid[i][j] = fill ? "X" : "O";
fill = !fill;
}
fill = !fill;
}
printArray(grid);
}
public static void main(String[] args) {
// TODO code application logic here
new NNStringArray();
}
}
48. Create a method converting digits to words.
Solution:
www.javaassignmenthelp.net
public class NNStringArray {
NNStringArray()
{
System.out.println(digitsToWords("35001922"));
}
String digitsToWords(String s)
{
String words = "";
String [] digits =
{"Oh","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
for (int i = 0; i<s.length();i++)
{
int number = Integer.parseInt(s.substring(i,i+1));
words = words + digits[number] + " ";
}
return words;
}
public static void main(String[] args) {
// TODO code application logic here
new NNStringArray();
www.javaassignmenthelp.net
}
}
49. Create a method converting time into words.
Solution:
public class NNStringArray {
NNStringArray()
{
System.out.println(timeToWords("2:05"));
System.out.println(timeToWords("2:47"));
System.out.println(timeToWords("12:00"));
System.out.println(timeToWords("2:00"));
System.out.println(timeToWords("12:30"));
System.out.println(timeToWords("4:29"));
System.out.println(timeToWords("7:25"));
System.out.println(timeToWords("12:45"));
System.out.println(timeToWords("12:55"));
}
String timeToWords(String s)
{
String time = "";
String [] input = s.split(":");
www.javaassignmenthelp.net
String [] times = {"o'clock","one","two","three","four", "five", "six",
"seven","eight","nine","ten","eleven","twelve","thirteen","fourteen",
"quarter","sixteen","seventeen","eighteen","nineteen","twenty",
"twenty one","twenty two","twenty three","twenty four",
"twenty five","twenty six","twenty seven","twenty eight",
"twenty nine","half past"};
int hours = Integer.parseInt(input[0]);
int minutes = Integer.parseInt(input[1]);
//Watch out for e.g. 12:50 = Ten to One
String sayMinutesp = minutes%5 != 0 ? " minutes" : "";
if (hours == 12 && minutes > 30) hours = 1;
if (minutes == 0)
{
time = times[hours] + " o'clock";
}
else if (minutes == 15)
{
time = "Quarter past " + times[hours];
}
else if (minutes == 45)
{
www.javaassignmenthelp.net
time = "Quarter to " + times[hours];
}
else if (minutes>30)
{
time = times[60-minutes]+ sayMinutesp + " to " + times[hours];
}
else
{
time = times[minutes] + sayMinutesp + " past " + times[hours];
}
return time;
}
public static void main(String[] args) {
// TODO code application logic here
new NNStringArray();
}
}
50. Create a program with the use of arrays translating plain text to morse
code.
Solution:
A B C D E F G
www.javaassignmenthelp.net
.- -… -.-. -.. . ..-. –.
H I J K L M
…. .. .— -.- .-.. —
N O P Q R S T
-. — .–. –.- .-. … –
U V W X Y Z
..- …- .– -..- -.– __..
public class NNStringArray {
NNStringArray()
{
System.out.println(morse("I never saw a purple cow"));
}
String morse(String s)
{
s = s.toLowerCase();
String cipherText = "";
String code [] = {".-","-...","-.-.","-..",".","..-.","--.", "....",
"..", ".---","-.-",".-..","--","-.","---",".--.","--.-",".-.",
"...","-","..-","...-",".--","-..-","-.--","--..", "/"};
www.javaassignmenthelp.net
for (int i = 0; i<s.length(); i++)
{
int number = s.charAt(i) == ' ' ? 26 : s.charAt(i)-97;
cipherText = cipherText + code[number] + " ";
}
return cipherText;
}
public static void main(String[] args) {
// TODO code application logic here
new NNStringArray();
}
}
51. Create a program with the use of arrays translating morse back into
plain text.
Solution:
public class NNStringArray {
NNStringArray()
{
System.out.println(demorse(".. / -. . ...- . .-. / ... .- .-- / .- / .--. ..- .-. .--. .-.. . / -.-. --- .--"));
}
String demorse(String s)
www.javaassignmenthelp.net
{
s = s.toLowerCase();
String cipher = "";
String code [] = {".-","-...","-.-.","-..",".","..-.","--.", "....",
"..", ".---","-.-",".-..","--","-.","---",".--.","--.-",".-.",
"...","-","..-","...-",".--","-..-","-.--","--..", "/"};
String [] letters = s.split("s+");
String plainText= "";
for (int i = 0; i<letters.length;i++)
{
String letter = letters[i].trim();
if (letter.equals("/"))
{
plainText = plainText + " ";
}
else
{
String decode = "";
for(int j = 0; j <code.length; j++)
{
if (letter.equals(code[j]))
www.javaassignmenthelp.net
{
char c = (char)(j+97);
plainText = plainText + Character.toString(c);
}
}
}
}
return plainText;
}
public static void main(String[] args) {
// TODO code application logic here
new NNStringArray();
}
}
52. Rewrite morse code program with the use of Hashmap.
Solution:
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
public class NNStringArray {
www.javaassignmenthelp.net
Map <String, String> morse = new HashMap<String, String>();
NNStringArray()
{
morse.put("a" , ".-");
morse.put("b" , "-...");
morse.put("c" , "-.-.");
morse.put("d" , "-..");
morse.put("e" , ".");
morse.put("f" , "..-.");
morse.put("g" , "--.");
morse.put("h" , "....");
morse.put("i" , "..");
morse.put("j" , ".---");
morse.put("k" , "-.-");
morse.put("l" , ".-..");
morse.put("m" , "--");
morse.put("n" , "-.");
morse.put("o" , "---");
morse.put("p" , ".--.");
morse.put("q" , "--.-");
morse.put("r" , ".-.");
morse.put("s" , "...");
morse.put("t" , "-");
www.javaassignmenthelp.net
morse.put("u" , "..-");
morse.put("v" , "...-");
morse.put("w" , ".--");
morse.put("x" , "-..-");
morse.put("y" , "-.--");
morse.put("z" , "--..");
morse.put(" " , "/");
System.out.println(morseHash("I never saw a purple cow"));
}
String morseHash(String s)
{
s = s.toLowerCase();
String cipherText = "";
for (int i = 0; i<s.length(); i++)
{
cipherText = cipherText + morse.get(s.substring(i,i+1)) + " ";
}
return cipherText;
}
www.javaassignmenthelp.net
public static void main(String[] args) {
// TODO code application logic here
new NNStringArray();
}
}
53. Rewrite demorse code program with the use of Hashmap.
Solution:
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
public class NNStringArray {
Map <String, String> morse = new HashMap<String, String>();
NNStringArray()
{
morse.put("a" , ".-");
morse.put("b" , "-...");
morse.put("c" , "-.-.");
morse.put("d" , "-..");
morse.put("e" , ".");
morse.put("f" , "..-.");
www.javaassignmenthelp.net
morse.put("g" , "--.");
morse.put("h" , "....");
morse.put("i" , "..");
morse.put("j" , ".---");
morse.put("k" , "-.-");
morse.put("l" , ".-..");
morse.put("m" , "--");
morse.put("n" , "-.");
morse.put("o" , "---");
morse.put("p" , ".--.");
morse.put("q" , "--.-");
morse.put("r" , ".-.");
morse.put("s" , "...");
morse.put("t" , "-");
morse.put("u" , "..-");
morse.put("v" , "...-");
morse.put("w" , ".--");
morse.put("x" , "-..-");
morse.put("y" , "-.--");
morse.put("z" , "--..");
morse.put(" " , "/");
System.out.println(demorseHash(".. / -. . ...- . .-. / ... .- .-- / .- / .--. ..- .-. .--. .-.. . / -.-.
--- .--"));
www.javaassignmenthelp.net
}
String demorseHash(String s)
{
String [] letters = s.split("s+");
String plaintext = "";
for (int i = 0; i<letters.length; i++)
{
for (Entry<String, String> entry : morse.entrySet()) {
if (entry.getValue().equals(letters[i])) {
plaintext = plaintext + entry.getKey();
}
}
}
return plaintext;
}
public static void main(String[] args) {
// TODO code application logic here
new NNStringArray();
}
www.javaassignmenthelp.net
}
54. To write a java program to print fibonacci series upto 100, you need a
code. Give a code of it.
Solution:
import java.util.Scanner;
/**
* Java program to calculate and print Fibonacci number using both recursion
* and Iteration.
* Fibonacci number is sum of previous two Fibonacci numbers fn= fn-1+ fn-2
* first 10 Fibonacci numbers are 1, 1, 2, 3, 5, 8, 13, 21, 34, 55
*
* @author Javin
*/
publicclass FibonacciCalculator {
publicstaticvoid main(String args[]) {
//input to print Fibonacci series upto how many numbers
System.out.println("Enter number upto which Fibonacci series to print: ");
int number = new Scanner(System.in).nextInt();
System.out.println("Fibonacci series upto " + number +" numbers : ");
//printing Fibonacci series upto number
www.javaassignmenthelp.net
for(int i=1; i<=number; i++){
System.out.print(fibonacci2(i) +" ");
}
}
/*
* Java program for Fibonacci number using recursion.
* This program uses tail recursion to calculate Fibonacci number for a given number
* @return Fibonacci number
*/
publicstaticint fibonacci(int number){
if(number == 1 || number == 2){
return 1;
}
return fibonacci(number-1) + fibonacci(number -2); //tail recursion
}
/*
* Java program to calculate Fibonacci number using loop or Iteration.
* @return Fibonacci number
*/
public static int fibonacci2(int number){
if(number == 1 || number == 2){
return 1;
www.javaassignmenthelp.net
}
int fibo1=1, fibo2=1, fibonacci=1;
for(int i= 3; i<= number; i++){
//Fibonacci number is sum of previous two Fibonacci number
fibonacci = fibo1 + fibo2;
fibo1 = fibo2;
fibo2 = fibonacci;
}
return fibonacci; //Fibonacci number
}
}
Output:
Enter number upto which Fibonacci series to print:
12
Fibonacci series upto 12 numbers :
1 1 2 3 5 8 13 21 34 55 89 144
55. In java program to print fibonacci series upto 100, show how to create
Fibonacci series.
Solution:
Code:
?
package com.java2novice.algos;
www.javaassignmenthelp.net
public class MyFibonacci {
public static void main(String a[]){
int febCount = 15;
int[] feb = new int[febCount];
feb[0] = 0;
feb[1] = 1;
for(int i=2; i < febCount; i++){
feb[i] = feb[i-1] + feb[i-2];
}
for(int i=0; i< febCount; i++){
System.out.print(feb[i] + " ");
}
56. Show how to create Fibonacci series Java.
Solution:
public class JavaFibonacciSeriesExample {
public static void main(String[] args) {
www.javaassignmenthelp.net
//number of elements to generate in a series
int limit = 20;
long[] series = new long[limit];
//create first 2 series elements
series[0] = 0;
series[1] = 1;
//create the Fibonacci series and store it in an array
for(int i=2; i < limit; i++){
series[i] = series[i-1] + series[i-2];
}
//print the Fibonacci series numbers
System.out.println("Fibonacci Series upto " + limit);
for(int i=0; i< limit; i++){
System.out.print(series[i] + " ");
}
}
}
/*
www.javaassignmenthelp.net
Output of the Fibonacci Series Java Example would be
Fibonacci Series upto 20
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
*/
57. Show how to print Fibonacci series between one to one hundred.
Solution:
Print fibonacci series between 1 to 100
like 0 1 1 2 3 5 8 13 21 34 55 89 144
?
publicclassFibonaci {
publicstaticvoidmain(String[] args) {
intn1 = 0, n2 = 1, num;
System.out.print(n1 + " "+
n2 + " ");
for(inti = 0; i < 100; i++) {
num = n2 + n1;
System.out.print(num + " ");
n1 = n2;
n2 = num;
}
}
}
www.javaassignmenthelp.net
58.In vending machine java program,for example; if you enter 5 for item
and.10 for paid, then why does it say Please insert another $1.4 instead
of 1.40? Even though 1.50 - 1.4 and 1.50 - 1.40 would be the same, how
can you solve the issue?
Solution: use arrays to help solve the issue.
59. Write a program finding Fibonacci series of given number. For
example:
Input – 8
Output - 1 1 2 3 5 8 13 21
Solution:
class Fibonacci{
publicstaticvoid main(String args[]){
int num = Integer.parseInt(args[0]); //taking no. as command line
argument.
System.out.println("*****Fibonacci Series*****");
int f1, f2=0, f3=1;
for(int i=1;i<=num;i++){
System.out.print(" "+f3+" ");
f1 = f2;
f2 = f3;
f3 = f1 + f2;
}
}
}
There you have our list of the most popular Java problems and solutions to learn
about, so refer to this guide when looking for formulas to use in specific Java
problems and contact us when you need Java assignment help!

More Related Content

PPTX
Java 8 Puzzlers [as presented at OSCON 2016]
PPTX
The Groovy Puzzlers – The Complete 01 and 02 Seasons
PDF
The Ring programming language version 1.5.4 book - Part 79 of 185
PDF
Computer java programs
PDF
The Ring programming language version 1.5.3 book - Part 10 of 184
PDF
Java Performance Puzzlers
PDF
JVM Mechanics: Understanding the JIT's Tricks
DOCX
Java file
Java 8 Puzzlers [as presented at OSCON 2016]
The Groovy Puzzlers – The Complete 01 and 02 Seasons
The Ring programming language version 1.5.4 book - Part 79 of 185
Computer java programs
The Ring programming language version 1.5.3 book - Part 10 of 184
Java Performance Puzzlers
JVM Mechanics: Understanding the JIT's Tricks
Java file

What's hot (20)

PDF
The core libraries you always wanted - Google Guava
PDF
JVM Mechanics
PDF
The Ring programming language version 1.5.4 book - Part 10 of 185
DOCX
PDF
JDays 2016 - Beyond Lambdas - the Aftermath
PDF
Spotify 2016 - Beyond Lambdas - the Aftermath
PDF
The Ring programming language version 1.6 book - Part 82 of 189
PDF
Java programs
DOCX
Java practical
DOC
Final JAVA Practical of BCA SEM-5.
PDF
PythonOOP
PDF
Important java programs(collection+file)
PDF
PPT
2012 JDays Bad Tests Good Tests
PDF
Java 8 - Nuts and Bold - SFEIR Benelux
PDF
GeeCon 2016 - Beyond Lambdas, the Aftermath
PPT
Functional Patterns for the non-mathematician
PDF
Java practical(baca sem v)
DOCX
QA Auotmation Java programs,theory
PDF
Final Project
The core libraries you always wanted - Google Guava
JVM Mechanics
The Ring programming language version 1.5.4 book - Part 10 of 185
JDays 2016 - Beyond Lambdas - the Aftermath
Spotify 2016 - Beyond Lambdas - the Aftermath
The Ring programming language version 1.6 book - Part 82 of 189
Java programs
Java practical
Final JAVA Practical of BCA SEM-5.
PythonOOP
Important java programs(collection+file)
2012 JDays Bad Tests Good Tests
Java 8 - Nuts and Bold - SFEIR Benelux
GeeCon 2016 - Beyond Lambdas, the Aftermath
Functional Patterns for the non-mathematician
Java practical(baca sem v)
QA Auotmation Java programs,theory
Final Project
Ad

Similar to Best Java Problems and Solutions (20)

PDF
12 Java Programming Challenges for Beginners and Strategies to Overcome Them ...
PDF
Review Questions for Exam 10182016 1. public class .pdf
PDF
Java Unit 1 Project
PDF
JAVA PRACTICE QUESTIONS v1.4.pdf
DOCX
FSOFT - Test Java Exam
PPTX
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_01-Mar-2021_L12...
PDF
Sam wd programs
PDF
merged_document_3
PDF
Java Simple Programs
DOCX
Exercise 1 [10 points]Write a static method named numUniq.docx
PDF
Simple Java Program for beginner with easy method.pdf
DOC
Practical java
DOCX
Question 1 1 pts Skip to question text.As part of a bank account.docx
PDF
Microsoft word java
DOCX
Exercise 1 [10 points]Write a static method named num
PPTX
OOPS with Java experiment related to fundamentals
DOC
Object oriented programming la bmanual jntu
DOCX
ExamName___________________________________MULTIPLE CH.docx
PDF
Homework l2 java
12 Java Programming Challenges for Beginners and Strategies to Overcome Them ...
Review Questions for Exam 10182016 1. public class .pdf
Java Unit 1 Project
JAVA PRACTICE QUESTIONS v1.4.pdf
FSOFT - Test Java Exam
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_01-Mar-2021_L12...
Sam wd programs
merged_document_3
Java Simple Programs
Exercise 1 [10 points]Write a static method named numUniq.docx
Simple Java Program for beginner with easy method.pdf
Practical java
Question 1 1 pts Skip to question text.As part of a bank account.docx
Microsoft word java
Exercise 1 [10 points]Write a static method named num
OOPS with Java experiment related to fundamentals
Object oriented programming la bmanual jntu
ExamName___________________________________MULTIPLE CH.docx
Homework l2 java
Ad

Recently uploaded (20)

PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Institutional Correction lecture only . . .
PPTX
Cell Types and Its function , kingdom of life
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Basic Mud Logging Guide for educational purpose
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
Lesson notes of climatology university.
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
TR - Agricultural Crops Production NC III.pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPH.pptx obstetrics and gynecology in nursing
Abdominal Access Techniques with Prof. Dr. R K Mishra
O5-L3 Freight Transport Ops (International) V1.pdf
Institutional Correction lecture only . . .
Cell Types and Its function , kingdom of life
102 student loan defaulters named and shamed – Is someone you know on the list?
Module 4: Burden of Disease Tutorial Slides S2 2025
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
O7-L3 Supply Chain Operations - ICLT Program
Microbial diseases, their pathogenesis and prophylaxis
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Basic Mud Logging Guide for educational purpose
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Renaissance Architecture: A Journey from Faith to Humanism
Lesson notes of climatology university.
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...

Best Java Problems and Solutions

  • 1. www.javaassignmenthelp.net Most Popular Java Problems and Solutions If looking for Java problems and their solutions, check out this page and you will learn of the most popular ones that you might want to learn about so that you can start memorizing and applying them when needed. List of Java Problems and Solutions 1. Create a Java program displaying Hello World on screen Solution: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World."); } }
  • 2. www.javaassignmenthelp.net 2. Create a Java program displaying asterisk pattern. Solution: ***** ***** ***** ***** ***** public class JavaExercises { public static void main(String[] args) { printAsterisk(); } static void printAsterisk(){ System.out.println("*****"); System.out.println("*****"); System.out.println("*****"); System.out.println("*****"); System.out.println("*****"); } } 3. Create a Java program declaring 2 integer variables, 1 float variable as well as 1 string variable, then assign 10, 12.5 as well as “Java programming” respectively. After that, display the values on screen. Solution: public class JavaExercises {
  • 3. www.javaassignmenthelp.net public static void main(String[] args) { accessVariables(); } static void accessVariables(){ int x; float y; String s; x = 10; y = 12.5f; s = "Java programming"; System.out.println(x); System.out.println(y); System.out.println(s); } } 4. Create a Java program with the use of the BufferedReader class in prompting user to input her or his name. The output will be like this: Hello Dara! Solution: import java.io.*; public class JavaExercises { public static void main(String[] args) { printName(); } static void printName(){ String pname=null; try{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter your name:"); pname=br.readLine(); }catch(IOException e){}
  • 4. www.javaassignmenthelp.net System.out.println("Hello "+pname); } } 5. Write an example method printing numbers one to ten. Solution: oneToTen() *** Output *** 1 2 3 4 5 6 7 8 9 10 6. Write an example method printing positive odd numbers that is less than 20. Solution: oddNumbers() *** Output *** 1 3
  • 5. www.javaassignmenthelp.net 5 7 9 11 13 15 17 19 7. Write an example method printing square numbers up to one hundred. Solution: squares() *** Output *** 1 4 9 16 25 36 49 64 81 100 8. Write an example loop in printing out 4 random integers between one and ten.
  • 6. www.javaassignmenthelp.net Solution: random4() *** Output *** 3 5 2 8 9. Write an example method printing out positive even numbers that is less than n. Solution: 1: even(20) 2: *** Output *** 3: 2 4: 4 5: 6 6: 8 7: 10 8: 12 9: 14 10: 16 11: 18 10.Write an example method printing out powers of two from 21 up to 2n. Solution: powers(8)
  • 7. www.javaassignmenthelp.net : *** Output *** 2 4 8 16 32 64 128 256 11. Create an example program that outputs “ Are we there yet?” Wait for the input and if it say “yes”, then the outputs program is “good” and then exit. Solution: "Are we there yet?" No "Are we there yet?" Spoons "Are we there yet?" Yes Good! 12. Create an example method using nested loops in producing a pattern. Solution: triangle() *** Output ***
  • 8. www.javaassignmenthelp.net * ** *** **** ***** 13. Create an example method printing out 4x4 table square. Solution: tableSquare() *** Output *** A 4 x 4 table square | 1 | 2 | 3 | 4 | | 1 | 2 | 3 | 4 | | 2 | 4 | 6 | 8 | | 3 | 6 | 9 | 12 | | 4 | 8 | 12 | 16 | 14. Write an example extending your answer to last question producing method printing out n x n table square. Solution: tableSquares(6) *** Output *** A 6 x 6 table square | 1 | 2 | 3 | 4 | 5 | 6 | | 2 | 4 | 6 | 8 | 10 | 12 | | 3 | 6 | 9 | 12 | 15 | 18 |
  • 9. www.javaassignmenthelp.net | 4 | 8 | 12 | 16 | 20 | 24 | | 5 | 10 | 15 | 20 | 25 | 30 | | 6 | 12 | 18 | 24 | 30 | 36 | 15. Write an example method printing out string array, 1 element for every line Solution: String [] breakfast = {"Sausage", "Eggs", "Beans", "Bacon", "Tomatoes", "Mushrooms"}; printArray(breakfast) *** Output *** Sausage Eggs Beans Bacon Tomatoes Mushrooms 16. Create an example method returning to the last element of the string array. Solution: String [] breakfast = {"Sausage", "Eggs", "Beans", "Bacon", "Tomatoes", "Mushrooms"} System.out.println(lastElement(breakfast)); *** Output *** Mushrooms
  • 10. www.javaassignmenthelp.net 17. Create an example method returning to the last, but 1 element of the string array. Solution: String [] breakfast = {"Sausage", "Eggs", "Beans", "Bacon", "Tomatoes", "Mushrooms"} System.out.println(lastButOne(breakfast)); *** Output *** Tomatoes 18. Create an example method reversing elements of the Array. Solution: String [] breakfast = {"Sausage", "Eggs", "Beans", "Bacon", "Tomatoes", "Mushrooms"}; System.out.println(reverse(breakfast)); *** Output *** : Mushrooms : Tomatoes : Bacon : Beans : Eggs : Sausage : Tails 19. Write a method testing to see if the array is palindromic. For example, elements are same when it is reversed. Solution:
  • 11. www.javaassignmenthelp.net String [] palindromic = {"Sausage", "Eggs", "Beans", "Beans", "Eggs", "Sausage"}; String [] breakfast = {"Sausage", "Eggs", "Beans", "Bacon", "Tomatoes", "Mushrooms"} System.out.println(isPalindrome(palindromic)); System.out.println(isPalindrome(breakfast)); *** Output *** True False 20. Write a method printing out int array with the consecutive duplicated eliminated. Solution: int [] nums = {1,1,3,3,3,2,2,2,1,1,1,1,4,4,4,4}; compress(nums) *** Output *** 5: 1 : 3 : 2 : 1 : 4 : 1 : 1 21. Pack consecutive duplicated of the char array into the strings. Solution:
  • 12. www.javaassignmenthelp.net char [] letters = {'a' 'a' 'a' 'a' 'b' 'c' 'c' 'a' 'a' 'd' 'e' 'e' 'e' 'e'}; pack(nums) *** Output *** : aaaa, b, cc, aa, d, eeee 22. Create a method returning the number of words in the string. Solution: String s = "I never saw a purple cow" System.out.println(countWords(s)); *** Output *** 6 23. Create a method counting instances of letter “e” in the string. Solution: String s = "I never saw a purple cow" System.out.println(countEs(s)); *** Output *** 3 24. Write a method returning number of the alphanumeric characters in string. Solution: String s = "1984 by George Orwell."
  • 13. www.javaassignmenthelp.net System.out.println(countChars(s)); *** Output *** 18 25. Create a method returning the string, reversed. Solution: String s = "I never saw a purple cow" System.out.println(reverse(s)); *** Output *** woc elprup a was reven I 26. Create a method testing if the string is palindrome. Solution: String sentence = "I never saw a purple cow" String palindrome = "rotavator" System.out.println(isPalindrome(sentence)); System.out.println(isPalindrome(palindrome)); *** Output *** False True 27.Create improved palindrome that disregard punctuation, case and spaces as well as recognize sentences like “ A man, a plan, a canal, Panama!” as palindrome.
  • 14. www.javaassignmenthelp.net Solution: String s = "I never saw a purple cow" String p = "Rise to vote, Sir!" System.out.println(isPalindrome(s)); System.out.println(isPalindrome(p)); *** Output *** False True 28. Write a method replacing vowels in the word with the asterisks. Solution: String s = "I never saw a purple cow" star(s) *** Output *** * n*v*r s*w * p*rpl* c*w 29. Create method spelling out words. For example, This will become T-H-I- S. Solution: String s = "I never saw a purple cow" spellOut(s) *** Output *** I N-E-V-E-R S-A-W A P-U-R-P-L-E C-O-W
  • 15. www.javaassignmenthelp.net 30.In simple substitution cipher, A=1, B=2, C=3 and others. Create a method encoding sentence with the use of cipher. Solution: String s = "Hello World" encode(s) *** Output *** 8,5,12,12,15 23,15,18,12,4 31. Create decoder method for the substitution cipher. Solution: String s = "9 14,5,22,5,18 19,1,23 1 16,21,18,16,12,5 3,15,23" decode(s) *** Output *** i never saw a purple cow 32. Determine if the given integer number is a prime. Solution: public class NNArithmetic { NNArithmetic() { System.out.println(isPrime(7));
  • 16. www.javaassignmenthelp.net } boolean isPrime(int a) { //Special cases if(a==1) return false; if(a==2) return true; boolean prime = true; int count = 2; do { if (a%count==0) { prime = false; } count++; } while(prime == true && count*count <=a); return prime; } public static void main(String[] args)
  • 17. www.javaassignmenthelp.net { new NNArithmetic(); } } 33. Use the Euclid’s Algorithm in determining Greatest Common Divisor of 2 positive integer numbers. Solution: public class NNArithmetic { NNArithmetic() { System.out.println(gcd (1071, 462)); System.out.println(gcd (6, 35)); System.out.println(gcd (36, 63)); } int gcd(int a, int b) { if(b>a) //Make sure the first number is largest { int temp = a; a = b;
  • 18. www.javaassignmenthelp.net b = temp; } while(b>0) { int remainder = a%b; a = b; b = remainder; } return a; } public static void main(String[] args) { new NNArithmetic(); } } 34. Determine whether 2 positive integer numbers are co-prime. 2 numbers are co-prime if the greatest common divisor equals to 1. Solution: public class NNArithmetic { NNArithmetic()
  • 19. www.javaassignmenthelp.net { System.out.println(isCoprime(35,64)); } int gcd(int a, int b) { if(b>a) //Make sure the first number is largest { int temp = a; a = b; b = temp; } while(b>0) { int remainder = a%b; a = b; b = remainder; } return a; }
  • 20. www.javaassignmenthelp.net boolean isCoprime(int a, int b) { return (gcd(a,b) ==1) ? true : false; } public static void main(String[] args) { new NNArithmetic(); } } 35. Calculate Euler’s totient function phi(m). For instance, m = 10: r = 1,3,7,9; thus phi(m) = 4. Note the special case: phi(1) = 1. Solution: public class NNArithmetic { NNArithmetic() { System.out.println(phi(10)); } int gcd(int a, int b) { if(b>a) //Make sure the first number is largest {
  • 21. www.javaassignmenthelp.net int temp = a; a = b; b = temp; } while(b>0) { int remainder = a%b; a = b; b = remainder; } return a; } boolean isCoprime(int a, int b) { return (gcd(a,b) ==1) ? true : false; } int phi(int m) { if (m==1) return 1; //Special Case
  • 22. www.javaassignmenthelp.net int phi = 0; for(int i = 1; i<m; i++) { if (isCoprime(m,i)) phi++; } return phi; } public static void main(String[] args) { new NNArithmetic(); } } 36. Create a method printing prime factors of given positive integer. Solution: public class NNArithmetic { NNArithmetic() { printPrimeFactors(315); printPrimeFactors(98); } void printPrimeFactors(int n)
  • 23. www.javaassignmenthelp.net { for(int i = 2; i*i<=n; i++) { while(n%i == 0) { System.out.print(i + " "); n = n/i; } } if(n>1) System.out.print(n + "n"); else System.out.println(""); } public static void main(String[] args) { new NNArithmetic(); } } 37. With the given integers range by its lower limit and upper limit, construct a list of the prime numbers within that range. Solution: public class NNArithmetic { NNArithmetic()
  • 24. www.javaassignmenthelp.net { printPrimesInRange(10,20); } boolean isPrime(int a) { //Special cases if(a==1) return false; if(a==2) return true; boolean prime = true; int count = 2; do { if (a%count==0) { prime = false; } count++; } while(prime == true && count*count <=a); return prime; }
  • 25. www.javaassignmenthelp.net void printPrimesInRange(int a , int b) { for (int i = a; i<=b; i++) { if (isPrime(i)) System.out.print(i + " "); } System.out.println(""); } public static void main(String[] args) { new NNArithmetic(); } } 38. Create predicate in finding 2 prime numbers summing up to given even number. Solution: public class NNArithmetic { NNArithmetic() { printGoldbachPair(28); printGoldbachPair(1856); }
  • 26. www.javaassignmenthelp.net boolean isPrime(int a) { //Special cases if(a==1) return false; if(a==2) return true; boolean prime = true; int count = 2; do { if (a%count==0) { prime = false; } count++; } while(prime == true && count*count <=a); return prime; } int [] goldbachPair(int n) { int count = 2; int [] pair = new int[2];
  • 27. www.javaassignmenthelp.net boolean foundPair = false; while(foundPair == false && count <= n/2) { if(isPrime(count) && isPrime(n-count)) { foundPair = true; pair [0] = count; pair [1] = (n-count); } count++; } return pair; } void printGoldbachPair(int n) { int [] pair = goldbachPair(n); System.out.println(pair[0] + " + " + pair [1] + " = " + n); } public static void main(String[] args) { new NNArithmetic();
  • 28. www.javaassignmenthelp.net } } 39. With the given integers by lower limit and upper limit, print list of even numbers as well as their Goldbach composition. Find out how many cases there are in range of 2..3000. Solution: public class NNArithmetic { NNArithmetic() { printGoldbachList(9, 20); printGoldbachList(1,2000,50); } boolean isPrime(int a) { //Special cases if(a==1) return false; if(a==2) return true; boolean prime = true; int count = 2; do
  • 29. www.javaassignmenthelp.net { if (a%count==0) { prime = false; } count++; } while(prime == true && count*count <=a); return prime; } int [] goldbachPair(int n) { int count = 2; int [] pair = new int[2]; boolean foundPair = false; while(foundPair == false && count <= n/2) { if(isPrime(count) && isPrime(n-count)) { foundPair = true; pair [0] = count; pair [1] = (n-count); } count++;
  • 30. www.javaassignmenthelp.net } return pair; } void printGoldbachPair(int n) { int [] pair = goldbachPair(n); System.out.println(pair[0] + " + " + pair [1] + " = " + n); } void printGoldbachList(int a, int b) { if(a%2==1) a++; // make sure start on even number for (int i = a ; i<=b ; i+=2) { printGoldbachPair(i); } } void printGoldbachList(int a, int b, int min) { if(a%2==1) a++; // make sure start on even number for (int i = a ; i<=b ; i+=2)
  • 31. www.javaassignmenthelp.net { int [] pair = goldbachPair(i); if(pair[0] > min && pair[1] > min) printGoldbachPair(i); } } public static void main(String[] args) { new NNArithmetic(); } } 40. Create a method printing out the string array, 1 element for every line. Solution: public class NNArrays { NNArrays() { String [] breakfast = {"Sausage", "Eggs", "Beans", "Bacon", "Tomatoes", "Mushrooms"}; printArray(breakfast); //Alternative Method printArrayAlt(breakfast); }
  • 32. www.javaassignmenthelp.net void printArray(String [] array) { for(int i = 0; i<array.length; i++) { System.out.println(array[i]); } } //Alternative method using for each loop void printArrayAlt(String [] array) { for(String s: array) { System.out.println(s); } } public static void main(String[] args) { new NNArrays(); } } 41. Write a method returning last element of the string array.
  • 33. www.javaassignmenthelp.net Solution: public class NNArrays { NNArrays() { String [] breakfast = {"Sausage", "Eggs", "Beans", "Bacon", "Tomatoes", "Mushrooms"}; System.out.println(lastElement(breakfast)); } String lastElement(String [] array) { return array[array.length-1]; } public static void main(String[] args) { new NNArrays(); } } 42. Create a method returning last, but 1 element of the string array. Solution: public class NNArrays {
  • 34. www.javaassignmenthelp.net NNArrays() { String [] breakfast = {"Sausage", "Eggs", "Beans", "Bacon", "Tomatoes", "Mushrooms"}; //Added two extra arrays for testing String [] coin = {"Heads", "Tails"}; String [] lonely = {"solo"}; System.out.println(lastButOne(breakfast)); System.out.println(lastButOneSafer(breakfast)); System.out.println(lastButOneSafer(coin)); System.out.println(lastButOneSafer(lonely)); } String lastButOne(String [] array) { return array[array.length-2]; } // The previous method will throw an // arrayIndexOutOfBoundsException for arrays of length <2. // This method will return an empty string if length <2 String lastButOneSafer(String [] array) {
  • 35. www.javaassignmenthelp.net if (array.length<2) { return ""; } else { return array[array.length-2]; } } public static void main(String[] args) { new NNArrays(); } } 43. Create a method returning elements of the Array in a reverse order. Solution: public class NNArrays { NNArrays() { String [] breakfast = {"Sausage", "Eggs", "Beans", "Bacon", "Tomatoes", "Mushrooms"}; //Added two extra arrays for testing String [] coin = {"Heads", "Tails"};
  • 36. www.javaassignmenthelp.net String [] lonely = {"solo"}; printArray(reverse(breakfast)); printArray(reverse(coin)); printArray(reverse(lonely)); } void printArray(String [] array) { for(int i = 0; i<array.length; i++) { System.out.println(array[i]); } } String [] reverse(String [] array) { for(int i = 0; i<array.length/2;i++) { String temp = array[i]; array[i] = array[array.length-i-1]; array[array.length-i-1] = temp; }
  • 37. www.javaassignmenthelp.net return array; } public static void main(String[] args) { new NNArrays(); } } 44. Create a method testing to see if the array is a palindromic. For instance, elements are same when it is being reversed. Solution: public class NNArrays { NNArrays() { String [] breakfast = {"Sausage", "Eggs", "Beans", "Bacon", "Tomatoes", "Mushrooms"}; String [] lonely = {"solo"}; String [] palindromic = {"Sausage", "Eggs", "Beans", "Beans", "Eggs", "Sausage"}; System.out.println(isPalindrome(palindromic)); System.out.println(isPalindrome(breakfast)); System.out.println(isPalindrome(lonely)); }
  • 38. www.javaassignmenthelp.net boolean isPalindrome(String [] array) { boolean isPal = true; for (int i = 0; i<array.length/2; i++) { if (!array[i].equals(array[array.length -i -1])) isPal = false; } return isPal; } // The following method doesn't work. // Need to compare individual elements boolean isPalindromeWrong(String [] array) { if (array.equals(reverse(array))) { return true; } else {
  • 39. www.javaassignmenthelp.net return false; } } String [] reverse(String [] array) { for(int i = 0; i<array.length/2;i++) { String temp = array[i]; array[i] = array[array.length-i-1]; array[array.length-i-1] = temp; } return array; } public static void main(String[] args) { new NNArrays(); } } 45. Create a method printing out int array with the consecutives duplicates eliminated. Solution:
  • 40. www.javaassignmenthelp.net public class NNArrays { NNArrays() { int [] nums = {1,1,3,3,3,2,2,2,1,1,1,1,4,4,4,4}; int [] num2 = {1,1}; int [] num1 = {1}; compress(nums); compress(num2); compress(num1); } void compress(int [] array) { System.out.println(array[0]); for (int i = 1; i<array.length; i++) { if (array[i]!=array[i-1]) { System.out.println(array[i]); } }
  • 41. www.javaassignmenthelp.net } public static void main(String[] args) { new NNArrays(); } } 46. Create a method printing 2D array of any sizes, then test it with the use of 3 x 3 tic-tact-toe grid. Solution: public class NNStringArray { NNStringArray() { tictactoe(); } void tictactoe() { String [][] grid = new String[3][3]; for(int i = 0; i<grid.length; i++) {
  • 42. www.javaassignmenthelp.net for (int j = 0; j<grid.length; j++) { grid[i][j] = "X"; } } printArray(grid); } void printArray(String [][] grid) { for(int i = 0; i<grid.length; i++) { for(int j = 0; j<grid.length; j++) { System.out.print(" | " + grid[i][j]); } System.out.println(" | "); } } public static void main(String[] args) { // TODO code application logic here new NNStringArray();
  • 43. www.javaassignmenthelp.net } } 47. Create a 8 x 8 array representing chess board. You should print out array with the alternate “X” and “O”. Solution: public class NNStringArray { NNStringArray() { chess(); } void printArray(String [][] grid) { for(int i = 0; i<grid.length; i++) { for(int j = 0; j<grid.length; j++) { System.out.print(" | " + grid[i][j]); } System.out.println(" | "); } }
  • 44. www.javaassignmenthelp.net void chess() { String [][] grid = new String[8][8]; boolean fill = true; for(int i = 0; i<grid.length; i++) { for (int j = 0; j<grid.length; j++) { grid[i][j] = fill ? "X" : "O"; fill = !fill; } fill = !fill; } printArray(grid); } public static void main(String[] args) { // TODO code application logic here new NNStringArray(); } } 48. Create a method converting digits to words. Solution:
  • 45. www.javaassignmenthelp.net public class NNStringArray { NNStringArray() { System.out.println(digitsToWords("35001922")); } String digitsToWords(String s) { String words = ""; String [] digits = {"Oh","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"}; for (int i = 0; i<s.length();i++) { int number = Integer.parseInt(s.substring(i,i+1)); words = words + digits[number] + " "; } return words; } public static void main(String[] args) { // TODO code application logic here new NNStringArray();
  • 46. www.javaassignmenthelp.net } } 49. Create a method converting time into words. Solution: public class NNStringArray { NNStringArray() { System.out.println(timeToWords("2:05")); System.out.println(timeToWords("2:47")); System.out.println(timeToWords("12:00")); System.out.println(timeToWords("2:00")); System.out.println(timeToWords("12:30")); System.out.println(timeToWords("4:29")); System.out.println(timeToWords("7:25")); System.out.println(timeToWords("12:45")); System.out.println(timeToWords("12:55")); } String timeToWords(String s) { String time = ""; String [] input = s.split(":");
  • 47. www.javaassignmenthelp.net String [] times = {"o'clock","one","two","three","four", "five", "six", "seven","eight","nine","ten","eleven","twelve","thirteen","fourteen", "quarter","sixteen","seventeen","eighteen","nineteen","twenty", "twenty one","twenty two","twenty three","twenty four", "twenty five","twenty six","twenty seven","twenty eight", "twenty nine","half past"}; int hours = Integer.parseInt(input[0]); int minutes = Integer.parseInt(input[1]); //Watch out for e.g. 12:50 = Ten to One String sayMinutesp = minutes%5 != 0 ? " minutes" : ""; if (hours == 12 && minutes > 30) hours = 1; if (minutes == 0) { time = times[hours] + " o'clock"; } else if (minutes == 15) { time = "Quarter past " + times[hours]; } else if (minutes == 45) {
  • 48. www.javaassignmenthelp.net time = "Quarter to " + times[hours]; } else if (minutes>30) { time = times[60-minutes]+ sayMinutesp + " to " + times[hours]; } else { time = times[minutes] + sayMinutesp + " past " + times[hours]; } return time; } public static void main(String[] args) { // TODO code application logic here new NNStringArray(); } } 50. Create a program with the use of arrays translating plain text to morse code. Solution: A B C D E F G
  • 49. www.javaassignmenthelp.net .- -… -.-. -.. . ..-. –. H I J K L M …. .. .— -.- .-.. — N O P Q R S T -. — .–. –.- .-. … – U V W X Y Z ..- …- .– -..- -.– __.. public class NNStringArray { NNStringArray() { System.out.println(morse("I never saw a purple cow")); } String morse(String s) { s = s.toLowerCase(); String cipherText = ""; String code [] = {".-","-...","-.-.","-..",".","..-.","--.", "....", "..", ".---","-.-",".-..","--","-.","---",".--.","--.-",".-.", "...","-","..-","...-",".--","-..-","-.--","--..", "/"};
  • 50. www.javaassignmenthelp.net for (int i = 0; i<s.length(); i++) { int number = s.charAt(i) == ' ' ? 26 : s.charAt(i)-97; cipherText = cipherText + code[number] + " "; } return cipherText; } public static void main(String[] args) { // TODO code application logic here new NNStringArray(); } } 51. Create a program with the use of arrays translating morse back into plain text. Solution: public class NNStringArray { NNStringArray() { System.out.println(demorse(".. / -. . ...- . .-. / ... .- .-- / .- / .--. ..- .-. .--. .-.. . / -.-. --- .--")); } String demorse(String s)
  • 51. www.javaassignmenthelp.net { s = s.toLowerCase(); String cipher = ""; String code [] = {".-","-...","-.-.","-..",".","..-.","--.", "....", "..", ".---","-.-",".-..","--","-.","---",".--.","--.-",".-.", "...","-","..-","...-",".--","-..-","-.--","--..", "/"}; String [] letters = s.split("s+"); String plainText= ""; for (int i = 0; i<letters.length;i++) { String letter = letters[i].trim(); if (letter.equals("/")) { plainText = plainText + " "; } else { String decode = ""; for(int j = 0; j <code.length; j++) { if (letter.equals(code[j]))
  • 52. www.javaassignmenthelp.net { char c = (char)(j+97); plainText = plainText + Character.toString(c); } } } } return plainText; } public static void main(String[] args) { // TODO code application logic here new NNStringArray(); } } 52. Rewrite morse code program with the use of Hashmap. Solution: import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; public class NNStringArray {
  • 53. www.javaassignmenthelp.net Map <String, String> morse = new HashMap<String, String>(); NNStringArray() { morse.put("a" , ".-"); morse.put("b" , "-..."); morse.put("c" , "-.-."); morse.put("d" , "-.."); morse.put("e" , "."); morse.put("f" , "..-."); morse.put("g" , "--."); morse.put("h" , "...."); morse.put("i" , ".."); morse.put("j" , ".---"); morse.put("k" , "-.-"); morse.put("l" , ".-.."); morse.put("m" , "--"); morse.put("n" , "-."); morse.put("o" , "---"); morse.put("p" , ".--."); morse.put("q" , "--.-"); morse.put("r" , ".-."); morse.put("s" , "..."); morse.put("t" , "-");
  • 54. www.javaassignmenthelp.net morse.put("u" , "..-"); morse.put("v" , "...-"); morse.put("w" , ".--"); morse.put("x" , "-..-"); morse.put("y" , "-.--"); morse.put("z" , "--.."); morse.put(" " , "/"); System.out.println(morseHash("I never saw a purple cow")); } String morseHash(String s) { s = s.toLowerCase(); String cipherText = ""; for (int i = 0; i<s.length(); i++) { cipherText = cipherText + morse.get(s.substring(i,i+1)) + " "; } return cipherText; }
  • 55. www.javaassignmenthelp.net public static void main(String[] args) { // TODO code application logic here new NNStringArray(); } } 53. Rewrite demorse code program with the use of Hashmap. Solution: import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; public class NNStringArray { Map <String, String> morse = new HashMap<String, String>(); NNStringArray() { morse.put("a" , ".-"); morse.put("b" , "-..."); morse.put("c" , "-.-."); morse.put("d" , "-.."); morse.put("e" , "."); morse.put("f" , "..-.");
  • 56. www.javaassignmenthelp.net morse.put("g" , "--."); morse.put("h" , "...."); morse.put("i" , ".."); morse.put("j" , ".---"); morse.put("k" , "-.-"); morse.put("l" , ".-.."); morse.put("m" , "--"); morse.put("n" , "-."); morse.put("o" , "---"); morse.put("p" , ".--."); morse.put("q" , "--.-"); morse.put("r" , ".-."); morse.put("s" , "..."); morse.put("t" , "-"); morse.put("u" , "..-"); morse.put("v" , "...-"); morse.put("w" , ".--"); morse.put("x" , "-..-"); morse.put("y" , "-.--"); morse.put("z" , "--.."); morse.put(" " , "/"); System.out.println(demorseHash(".. / -. . ...- . .-. / ... .- .-- / .- / .--. ..- .-. .--. .-.. . / -.-. --- .--"));
  • 57. www.javaassignmenthelp.net } String demorseHash(String s) { String [] letters = s.split("s+"); String plaintext = ""; for (int i = 0; i<letters.length; i++) { for (Entry<String, String> entry : morse.entrySet()) { if (entry.getValue().equals(letters[i])) { plaintext = plaintext + entry.getKey(); } } } return plaintext; } public static void main(String[] args) { // TODO code application logic here new NNStringArray(); }
  • 58. www.javaassignmenthelp.net } 54. To write a java program to print fibonacci series upto 100, you need a code. Give a code of it. Solution: import java.util.Scanner; /** * Java program to calculate and print Fibonacci number using both recursion * and Iteration. * Fibonacci number is sum of previous two Fibonacci numbers fn= fn-1+ fn-2 * first 10 Fibonacci numbers are 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 * * @author Javin */ publicclass FibonacciCalculator { publicstaticvoid main(String args[]) { //input to print Fibonacci series upto how many numbers System.out.println("Enter number upto which Fibonacci series to print: "); int number = new Scanner(System.in).nextInt(); System.out.println("Fibonacci series upto " + number +" numbers : "); //printing Fibonacci series upto number
  • 59. www.javaassignmenthelp.net for(int i=1; i<=number; i++){ System.out.print(fibonacci2(i) +" "); } } /* * Java program for Fibonacci number using recursion. * This program uses tail recursion to calculate Fibonacci number for a given number * @return Fibonacci number */ publicstaticint fibonacci(int number){ if(number == 1 || number == 2){ return 1; } return fibonacci(number-1) + fibonacci(number -2); //tail recursion } /* * Java program to calculate Fibonacci number using loop or Iteration. * @return Fibonacci number */ public static int fibonacci2(int number){ if(number == 1 || number == 2){ return 1;
  • 60. www.javaassignmenthelp.net } int fibo1=1, fibo2=1, fibonacci=1; for(int i= 3; i<= number; i++){ //Fibonacci number is sum of previous two Fibonacci number fibonacci = fibo1 + fibo2; fibo1 = fibo2; fibo2 = fibonacci; } return fibonacci; //Fibonacci number } } Output: Enter number upto which Fibonacci series to print: 12 Fibonacci series upto 12 numbers : 1 1 2 3 5 8 13 21 34 55 89 144 55. In java program to print fibonacci series upto 100, show how to create Fibonacci series. Solution: Code: ? package com.java2novice.algos;
  • 61. www.javaassignmenthelp.net public class MyFibonacci { public static void main(String a[]){ int febCount = 15; int[] feb = new int[febCount]; feb[0] = 0; feb[1] = 1; for(int i=2; i < febCount; i++){ feb[i] = feb[i-1] + feb[i-2]; } for(int i=0; i< febCount; i++){ System.out.print(feb[i] + " "); } 56. Show how to create Fibonacci series Java. Solution: public class JavaFibonacciSeriesExample { public static void main(String[] args) {
  • 62. www.javaassignmenthelp.net //number of elements to generate in a series int limit = 20; long[] series = new long[limit]; //create first 2 series elements series[0] = 0; series[1] = 1; //create the Fibonacci series and store it in an array for(int i=2; i < limit; i++){ series[i] = series[i-1] + series[i-2]; } //print the Fibonacci series numbers System.out.println("Fibonacci Series upto " + limit); for(int i=0; i< limit; i++){ System.out.print(series[i] + " "); } } } /*
  • 63. www.javaassignmenthelp.net Output of the Fibonacci Series Java Example would be Fibonacci Series upto 20 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 */ 57. Show how to print Fibonacci series between one to one hundred. Solution: Print fibonacci series between 1 to 100 like 0 1 1 2 3 5 8 13 21 34 55 89 144 ? publicclassFibonaci { publicstaticvoidmain(String[] args) { intn1 = 0, n2 = 1, num; System.out.print(n1 + " "+ n2 + " "); for(inti = 0; i < 100; i++) { num = n2 + n1; System.out.print(num + " "); n1 = n2; n2 = num; } } }
  • 64. www.javaassignmenthelp.net 58.In vending machine java program,for example; if you enter 5 for item and.10 for paid, then why does it say Please insert another $1.4 instead of 1.40? Even though 1.50 - 1.4 and 1.50 - 1.40 would be the same, how can you solve the issue? Solution: use arrays to help solve the issue. 59. Write a program finding Fibonacci series of given number. For example: Input – 8 Output - 1 1 2 3 5 8 13 21 Solution: class Fibonacci{ publicstaticvoid main(String args[]){ int num = Integer.parseInt(args[0]); //taking no. as command line argument. System.out.println("*****Fibonacci Series*****"); int f1, f2=0, f3=1; for(int i=1;i<=num;i++){ System.out.print(" "+f3+" "); f1 = f2; f2 = f3; f3 = f1 + f2; } } } There you have our list of the most popular Java problems and solutions to learn about, so refer to this guide when looking for formulas to use in specific Java problems and contact us when you need Java assignment help!