- Reversing a String without reverse() function
- Add numbers from the given input
- Count no. of words in the line or paragraph
- Find Armstrong number or not
- Find Prime number or not
Reversing a String without reverse() function
public class StringReverse { public static void main(String[] args) { String str = "Java Discover"; System.out.println("Input String : "+str); str = myReverseFunction(str); System.out.println("Reversed String : "+str); } /** * Need to reverse a String without reverse() function * @param str * @return */ public static String myReverseFunction(String str){ char[] chars = str.toCharArray(); StringBuilder sb = new StringBuilder(); for (int i=str.length()-1;i>=0;i--) { sb.append(chars[i]); } return sb.toString(); } }
OUTPUT:
Input String : Java Discover Reversed String : revocsiD avaJ
Add numbers from the given input
public class AddNumerals { public static void main(String[] args) { int value = 1234; int output = addNumerals(value); System.out.println("INPUT : "+value); System.out.println("OUTPUT : "+output); } /** * Add numerals in the given param value * For example input=1234 then output should be 1+2+3+4=10 * @param value * @return */ public static int addNumerals(int value){ int output = 0; while(value > 0){ output += value%10; value = value/10; } return output; } }
OUTPUT:
INPUT : 1234 OUTPUT : 10
Count no. of words in the line or paragraph
public class CountWords { public static void main(String[] args) { String line = "The string tokenizer class allows an application to break a string into tokens"; int noOfWords = countNoOfWordsInLine(line); System.out.println("Input : "+line); System.out.println("No Of Words : "+noOfWords); } /** * Count no. of words in a line or paragraph * @param line * @return */ public static int countNoOfWordsInLine(String line){ int output = 0; StringTokenizer tok = new StringTokenizer(line); output = tok.countTokens(); return output; } }
OUTPUT:
Input : The string tokenizer class allows an application to break a string into tokens No Of Words : 13
Find Armstrong number or not
public class ArmstrongOrNot { public static void main(String[] args) { long input = 54748; boolean flag = checkArmstringOrNot(input); if(flag){ System.out.println(input+" is Armstrong number"); }else{ System.out.println(input+" is NOT Armstrong number"); } } /** * Check given number is Armstrong number or not * For example, 371 is an Armstrong number, since 3^3 + 7^3 + 1^3 = 371 * @param input * @return */ public static boolean checkArmstringOrNot(long input){ int output = 0; long tmp = input; int length = Long.toString(input).length(); while(tmp != 0){ long rem = tmp % 10; long tmp1 = 1; for(int i = 0; i < length ;i++){ tmp1 *= rem; } output += tmp1; tmp = tmp/10; } if(input == output){ return true; } return false; } }
OUTPUT:
54748 is Armstrong number
Find Prime number or not
public class PrimeOrNot { public static void main(String[] args) { int input = 23; boolean flag = primeOrNot(input); if(flag){ System.out.println(input+" is a Prime number"); }else{ System.out.println(input+" is NOT a Prime number"); } } /** * Check whether given number is Prime or Not * @param input * @return */ public static boolean primeOrNot(int input){ for(int i=2;i<=input/2;i++){ if(input%i == 0){ return false; } } return true; } }
OUTPUT:
23 is a Prime number
More questions will be added shortly.
2 comments:
Write comments