Skip to content

Commit 45e3454

Browse files
authored
Merge pull request #8 from vaishakhvh/main
Adding sample java programs
2 parents 59f394d + 5d51979 commit 45e3454

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

Fibonacci.java

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
public class Fibonacci {
3+
public static void main(String[] args) {
4+
5+
//Prints first 12 fibonacci series numbers
6+
int n = 12, t1 = 0, t2 = 1;
7+
System.out.print("First " + n + " terms: ");
8+
9+
for (int i = 1; i <= n; ++i)
10+
{
11+
System.out.print(t1 + " + ");
12+
13+
int sum = t1 + t2;
14+
t1 = t2;
15+
t2 = sum;
16+
}
17+
}
18+
}

HelloWorld_OneCharEverySecond.java

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
public class HelloWorld_OneCharEverySecond {
3+
public static void main(String[] args) throws InterruptedException {
4+
String helloWorld = "Hello, World!";
5+
6+
for(char c:helloWorld.toCharArray()) {
7+
System.out.print(c);
8+
Thread.sleep(1000);
9+
}
10+
}
11+
}

Palindrome.java

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
public class Palindrome {
3+
4+
// Function that returns true if
5+
// str is a palindrome
6+
static boolean isPalindrome(String str)
7+
{
8+
9+
// Pointers pointing to the beginning
10+
// and the end of the string
11+
int i = 0, j = str.length() - 1;
12+
13+
// While there are characters to compare
14+
while (i < j) {
15+
16+
// If there is a mismatch
17+
if (str.charAt(i) != str.charAt(j))
18+
return false;
19+
20+
// Increment first pointer and
21+
// decrement the other
22+
i++;
23+
j--;
24+
}
25+
26+
// Given string is a Palindrome
27+
return true;
28+
}
29+
30+
// Test code
31+
public static void main(String[] args)
32+
{
33+
String str = "wow";
34+
35+
if (isPalindrome(str))
36+
System.out.print("Yes");
37+
else
38+
System.out.print("No");
39+
}
40+
}
41+
42+

0 commit comments

Comments
 (0)