Java Prgms
Java Prgms
LazyQA.com
3. Fibonacci Series –
Input = 12321
Output =12321
LazyQA.com
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter number which you want for Factorial: ");
int num = sc.nextInt();
int fact = 1;
for (int i = 1; i <= num; i++) {
fact = fact * i;
}
System.out.println("Factorial of" + num + " is " + fact);
}
------------------------------------------------------------------------------------------------------------
6. OddEvenNumbers
Input = 11
Output = Given number is odd number
public static void main(String[] args) {
// 1. Using Brute Forcew Approach
Scanner sc = new Scanner(System.in);
System.out.println("Enter Number:-");
int num = sc.nextInt();
if (num % 2 == 0)// Brute Forcew Approach
{
System.out.println("Given is even number");
} else {
System.out.println("Given number is odd number");
}
------------------------------------------------------------------------------------------------------------
7. Prime Number
Prime number is a number that is greater than 1 and divided by 1 or itself only.
LazyQA.com
8. Largest number from 3 number/ given list
LazyQA.com
num = num / 10;
count++;
}
System.out.println("Number of digits : " + count);
// 2. Converting given number to string solution to count digits in an integer
String result = Integer.toString(num2); // calculate the size of string
System.out.println(+result.length());
}
------------------------------------------------------------------------------------------------------------
Input = mama
Output = mama
public static void main(String[] args) {
String str = "mama";
String s2 = "";
// 1. by using the charAt() method
for (int i = str.length() - 1; i >= 0; i--) {
s2 = s2 + str.charAt(i);// extracts each character and store in string
}
System.out.println("Reversed word: " + s2);
// below is code to check weather given string is Palindrome or not
if (str.equalsIgnoreCase(s2)) {
System.out.println("String is Palindrome");
} else {
System.out.println("String is not Palindrome");
}
}
// 2. Using built in reverse() method of the StringBuilder class:
String input = "Welcome To Jave Learning";
StringBuilder input1 = new StringBuilder();
input1.append(input); // append a string into StringBuilder input1
input1.reverse();
System.out.println(input1);
// 3. Using StringBuffer:
String strText = "Java Learning";
// conversion from String object to StringBuffer
StringBuffer sbr = new StringBuffer(strText);
sbr.reverse();
System.out.println(sbr);
------------------------------------------------------------------------------------------------------------
LazyQA.com
2. Remove space form given string
Input =
array1 = { 4, 2, 3, 1, 6 }; array2 = { 6, 7, 8, 4 };
Output = 6,4
LazyQA.com
4. Find first and last element of ArrayList in java
Input = array1 = { 4, 2, 3, 1, 6 };
Output = First is:4, Last is: 6
Output = 5 10 12 20 57 60 63 88
LazyQA.com
7. Counting number of occurrences of given word in a string using Java?
LazyQA.com
10. count the occurrences of each character?
Input = "This is an example";
Output = p = 1, a = 2, s = 2, T = 1, e = 2, h = 1, x = 1, i = 2, l = 1, m = 1, n = 1
--------------------------------------------------------------------------------------------------------
11. Removing Duplicates from an Array
// using for loop
String[] strArray = {"abc", "def", "abc", "mno", "xyz", "pqr", "xyz", "pqr"};
//1. Using Brute Force Method
for (int i = 0; i < strArray.length-1; i++)
{
for (int j = i+1; j < strArray.length; j++)
{
if( (strArray[i]==(strArray[j])) )
{
System.out.println("Brute Force Method : Duplicate Element is : "+strArray[j]);
}}}
// using Hashset
HashSet<String> hs = new HashSet<String>();
for (String arrayElement : strArray)
{
if(!hs.add(arrayElement))
{System.out.println("HashSet :Duplicate Element is : "+arrayElement);
}}
-------------------------------------------------------------------------------------------------------
LazyQA.com
12. Reverse each word in a sentence
Input =
String str1 = "Army";
String str2 = "Mary";
Output = army and mary are anagram.
LazyQA.com
14. How to print duplicate characters from the string?
Input = "apple is fruit";
Output = p i
Input = "Welcome234To567Java89Programming0@#!!";
Output =
WelcomeToJavaProgramming
234567890
@#!!
LazyQA.com
if (Character.isDigit(str.charAt(i)))
num.append(str.charAt(i));
else if (Character.isAlphabetic(str.charAt(i)))
alpha.append(str.charAt(i));
else
special.append(str.charAt(i));
}
System.out.println(alpha);
System.out.println(num);
System.out.println(special);
}
LazyQA.com