I have shared some of
the basic programs written in Java. Please see below and practice them with
eclipse or any IDE. If you prefer to run it in a separate JVM you can do that
as well. Here I have shared the samples written using eclipse.
Please click on the images to see an enlarged version.
Please click on the images to see an enlarged version.
1. Fibonacci Series
package com.techietamizhan.basicprograms;
public class FibonacciSeries {
public static void
main(String[] args) {
getFibonacciSeries(10);
}
public static void
getFibonacciSeries(int count) {
int
a = 0;
int
b = 1;
int
c = 1;
for (int i = 1; i <= count; i++)
{
System.out.println(a);
a
= b;
b
= c;
c
= a + b;
}
}
}
2.Reversing a String
package com.techietamizhan.basicprograms;
public class ReverseString {
public static void
main(String[] args) {
System.out.println(reverseString("techietamizhan"));
}
public static String
reverseString(String str) {
if
(str == null)
return
null;
StringBuilder
out = new StringBuilder();
int
length = str.length();
for
(int i = length - 1; i >= 0; i--) {
out.append(str.charAt(i));
}
return
out.toString();
}
}
3. Checking a Prime
Number
package com.techietamizhan.basicprograms;
import java.util.Scanner;
public class PrimeNumberCheck {
public static void
main(String[] args) {
Scanner
s = new Scanner(System.in);
System.out.print("Enter
a number :");
int
n = s.nextInt();
s.close();
checkPrimeNumber(n);
}
private static void
checkPrimeNumber(int n) {
if
(n == 0 || n == 1) {
System.out.println(n
+ " is not a prime number");
return;
}
if
(n == 2) {
System.out.println(n
+ " is a prime number");
}
for
(int i = 2; i <= n / 2; i++) {
if
(n % i == 0) {
System.out.println(n
+ " is not a prime number");
return;
}
}
System.out.println(n
+ " is a prime number");
}
}
4. Checking a Palindrome
package com.techietamizhan.basicprograms;
import java.util.Scanner;
public class PalindromeCheck {
public static void
main(String[] args) {
Scanner
s = new Scanner(System.in);
System.out.println("Enter
a String:");
String
str = s.next();
s.close();
checkPalindrome(str);
}
private static void
checkPalindrome(String str) {
char[]
charArray = str.toCharArray();
StringBuilder
sb = new StringBuilder();
for
(int i = charArray.length - 1; i >= 0; i--) {
sb.append(charArray[i]);
}
if
(sb.toString().equalsIgnoreCase(str))
System.out.println(str
+ " is palindrome.");
else
System.out.println(str
+ " is not a palindrome");
}
}
5. Sorting an Array
using Bubble Sort
package com.techietamizhan.basicprograms;
import java.util.Arrays;
public class ArraySort {
public static void
main(String[] args) {
int[]
array = {20,50,40,10,60,30,20};
int[]
sortedArray = bubbleSort(array);
System.out.println(Arrays.toString(sortedArray));
}
public static int[]
bubbleSort(int[] arr){
int temp;
for(int i=0; i
< arr.length-1; i++){
for(int j=1; j
< arr.length-i; j++){
//the last
index has highest value in first loop,second last index has second last highest
value and so on
if(arr[j-1]
> arr[j]){
temp=arr[j-1];
arr[j-1]
= arr[j];
arr[j]
= temp;
}
}
//uncomment
the following line to see array after each iteration
//System.out.println("Array
after "+(i+1)+"th iteration:"+Arrays.toString(arr));
}
return arr;
}
}
Any doubts or challenges please feel free to comment.
Comments
Post a Comment