0% found this document useful (0 votes)
57 views

Computer Programs - BW

Computer Science ISC Practical Use

Uploaded by

chill sings
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
57 views

Computer Programs - BW

Computer Science ISC Practical Use

Uploaded by

chill sings
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 70
ax Compile) undo cut_| copy || raste || Find. || close netic t retu m P import java.util.Scanner public class Q1 { public static void main(String{] args) { Scanner scanner = new Scanner(System.in); System.out .print("Enter an integer: *); int inputNumber = scanner.nextInt(); System.out .printin("Prine numbers after ~ + inputNunber + °:*); printllext1 5Prines inputNunber) ; scanner close() ; , private static boolean isPrime(int number) { if (number <= 1) { return fal t for (ant i= 2; 4 <= Wath.sqrt(number): 14) ( if (number % i et return felse; ) + » private static void printNext15Primes(int startNunber) { int count = @; int currentNumber = startNunber + 1; while (count < 18) { af (asPrime(currentNunber)) { ‘System, out.print(currentNumber + ys countt+; > currentNumber++; } systen.out .printin() ; Enter an integer: 3 Prime numbers after 3: 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 Caleulates: x Compile | undo | cut || copy | Paste | Find... || close 2. Inpt numbers m ind an nv here m! (n=m import java.util.Scanner; public class Calculates public static void main(String[] args) { Scanner scanner = new Scanner (System. in); Input the values of m a System.out.print("Enter the value of m: int m = scanner.nextInt(); System.out.print("Enter the value of n: "); int n = scanner .nextInt(); Calculate and prir long s = calculates(m, n); System.out.println("S = " +s); scanner .close() ; } private static long calculateS(int m, int n) { long mFactorial = for (int i= 1; i <= mj; itt) { mFactorial *= i; + long nFactorial = for (int i= 1; i < nFactorial *= i; is) { } long nMinusMFactorial = 1; for (int i = 1; i <= (n - m); it) { nMinusMFactorial *= i; + return nFactorial / (mFactorial * nMinusMFactorial) ; Enter the value of m: 2 Enter the value of n: 5 S= 18 Enter the value of m: 8 Enter the value of n: 4 sS=6 Inport java, uta]. Scanner public class SinpleTriangleClaseification { public static void main(String[] args) ( ‘Scanner scanner = new Seanner(S ‘Systen.out print("Enter the Fir Int angle? = scanner nextInt() systen.out .print("Enter the second angle: ") ant anglez = scanner nextint() systen.out .print("enter the third angle: "); sn angle’ = scanner nextint(): af (4sValitriengle(anglet, angle2, angies)) ( ‘Syeten.out printin( "The tiangl Sf (isightangled(anglet, angled Systen-out.printin( Tt is 2 1: } else if (istcutetngled(angle1, angle2, angle3)) { System.out printla( It is 29 acute-angled triongle."); ) else if (1s0btuseAngled{anglet, angle2, angle3)) { Systen-out printin(-Tt is an obtuse-angled triangle.) ) ease ¢ yetan.ot.prinan("The triangle ie ot possible with the piven ogi : 1c ) private stati totean 1VeLidTrianla(intarglet, int angel, int angie) ( Teturn (ange! + angled nngiet © 180) (ogle1 6 td angled» ® Ub angled > 8) > private static boolean isRightAngled(int anglet, int angle2, int angles) ( return (angle! == 9@ || angle? == 80 || angle == 90) > private static boolean isdeutesngled(snt anglet, int angle2, int anglea) ( return (angle! < 96 && angle? < 90 8 angle3 < 99) > private static boolean 1sobtuseangled(int angler, ant englez, int angles) ¢ return (angle > 98 |) angle2 > 96 || angles > 99) » Enter the first angle: 68 Enter the second angle: 54 Enter the third angle: 74 The triangle is possible. It is an acute-angled triangle. import java.uti1.Scanner, public class SimplearnstrongNuabers { public static void main(String[] args) { Scanner scanner = nen Scanner (System.in) system. out print("Enter the = t start = scanner-nextInt(); system.out .print("Enter the ending number of the range: "); end = scanner-nextInt() system. out println(“Arastrong numbers between "4 start + * and" + end + °:"): for (int 4 = start; 4 <= end; i++) { Af (AsArmstrong(4)) { Systen.out.print(i +" ") arting number of the range: *) > > seanner.close(); , private static boolean isArmstrong(int nunber) { originalNunber = number; sua = @ nt nunberOfDigits while (number > 8) { ant digit = runber % 10; sum += Math.pow(digit, nunberOfOigits) umber /= 14; ountDigits(number) return sum originalNunber: private static int countDigits(int number) { while (number > @) { nuaber /= 10; counts: ) return count; Enter the starting number of the range: 1 Enter the ending number of the range: 560 Armstrong numbers between 1 and 508: 1234567 8 9 153 378 371 407 java, utsl Scanner public clase Sinpletrescaleuletor { public stetie vo1d main(Steinal] args) { ‘Ssamer scanner” = now Seanner(Syster.3n) syetem.out print("Enter base and height of paralle oseParallologran = scanner next00uble() (9 nesgre?arallelogran = seanner-nexzoouble() Syatem.out printia(-Ares Gt parallelogran’ "+ ealeulateParalielograndrea(baseParallelogren, hesghtParsllelogran)) system.out.print(“Enter esagonals of thonbus! “) oubis stagonaleronbue = seanner.nextouD}e() double diagonal2RPonbus = Scanner nextDouble() System out-printin(-ares of shontus:” + calculateRanbustrea(diagonal Rhombus, diagans17Rhanbus)) System.out print("Enter bases, height, and side of trapezium: *) double basetTrapezsun + seanner.rextDouble() (oubie bere2Trapersien = scanner nextDouble() Govbte heightTrapezium = scanner-nextOovble() System.out-printin( ares oF trapeziun: "+ caleulatetrapeziundrea(basetTrapezium, base2Trapeziun, heightTrapeziun)) scamer.close() ‘ealeulateParalielogrankrea( ue base, double hasght) { private static double ealeulateRhonbushrea(double dlagenalt, double dagonel2) ) private etatie > Teturn 8,8 * diagonali » éiagonalZ: © ealculaterrapezsuesrea(double Basel, eouble base, double height) ( Toturn 0,6 (O3eet + base?) = height! Enter base and height of parallelogram: 45 23 Area of parallelogram: 1035.8 Enter diagonals of rhombus: 76 45 Area of rhombus: 1718.8 Enter bases, height, and side of trapezium: 89 32 45 Area of trapezium: 2722.5 import java.util.Scanner public class SinplearesCalculetor public static void main(Stringl] args) ( Scanner scanner = new Scanner(Systen.in); system.out print("Enter the radius of the circle: ") double eireleRadius = scanner .nextDouble() couble eirelekres = calculateCirclesrea(carcleRadius) systemout println( "Area of the circle: " + eirclesrea); systemout print("Enter the side length of the square: "); double squareSideLength = scanner .nextDouble() double squaredres = calculateSquareArea(squareSideLength) ; system.out .println( "Ares of the square: + squaresrea); System.out print("Enter the Length of the rectangle: “); double rectangletength = scanner.nextDouble( ) system.out.print("Enter the width of the rectangle: double rectangleltidth = scanner.nextDouble() ; double rectanglearea = calculateRectanglesrea(rectangleLength, rectangleltidth} system.out ,printIn("Area of the rectangle: " + rectangleares) scanner .close(), > Funct z private static double caleulateCircleArea(dovble radius) ( return Math.PI * radius * radius: ) Funct e private static double calculateSquareArea( double sideLength) ( return sideLength * sideLength; ) private static double calculateRectangleArea(double Length, double width) { return length * width; ) Enter the radius of the circle: 5 Area of the circle: 78.53981633974483 Enter the side length of the square: 7 Area of the square: 49.0 Enter the length of the rectangle: 8 Enter the width of the rectangle: 6 Area of the rectangle: 48.0 lpueise class Velueconparator { public vois compare( nt nut uma) ( SF (nut > nud) Syetem.out.printIn(num + ~ is greater than ~ + uz) ) eise at (numi< numa) { Systea.out.println(numd + ° is greater than” + euet) yeise ¢ Syeten.out.printin( L . public vois cenpare(shar char’, char chard) 4 nt nim2 = ohar?: SF (mum » non) ( Systen.out printn(chert + " has @ higher nunersc value than " + char2) ) else 3f (num « nana) ( Syeten.out.println(cher? + " has a higher nunerse value than * + chart) yelse ( Systea out printin(“Both characters have the seme nunerie value"); ’ ) public static void main(Stringl] args) { ValueCenparator comparator = new ValueConparator{) comparator sconpare(5, 8) comparator conpara(A', '3") Enter the first integer: 9 Enter the second integer: 5 9 is greater than 5 Enter the first character: a Enter the second character: A a has a higher numeric value than A public class TechNunberFinder { public static vosd main(Stringl] arge) { for (Ant num = 1000; num <= 9999; ume+) ( Sf (4sTechunber(num)) { Systen.out-printin( Tech Nusber: "+ num); private static fan isTechuaber(int number) { String nunst Integer. toString (nusber) SF (numstr -Length() 2 == 6) { ant middleindex = numSte.Length() / 2; String farsthalfStr = numstr.substring(®, middlerndex) String secondHalfStr = nunste .substring(middlerndex) ant farstialf = Integer.parselnt(firstHalfStr) Aint secondHalf = Integer .parseTnt(secondHalfStr) nt squareofsun = (firsthalf + secondHalf) * (Farstialf + secondHalt); return squareofsun umber; Tech Number: 2025 Tech Number: 3025 Tech Number: 98@1 ‘import java.util. Scanner: public class EvenNuaberInput { Public static void main(Stringl] args) { Scanner scanner = new Scanner (Systes.in) int evenCount = @ whale [evencount < 10) { Systea.outprint(*Enter a number: *) Apt number = scanner nextInt() af (number & 2 == 8) { ‘evencounter: Systen.out.printIn(nuaber + " is even. Even Count: " + eventount) ) else { Systen.out.println(nusber +" is not even.”); ) Aff (evenCount == 4) { System.out.printIn("Maxinua Limit reached. Exiting the pragran.") break; » » scanner .close() Enter a number: 22 22 is even. Even Count: 1 Enter a number: 42 42 is even. Even Count: 2 Enter a number: 33 33 is not even. Enter a number: 987 Enter a numbe Enter a number: 88 88 is even. Even Count: 3 Enter a number: 42 Maximum limit reached. ExiMaximum limit reached. Exiting the program. pore Java,util, Seanners public clase Telephonesslicalcularor { public stetsc votd mesn(Stringt] args) ( Seamer scanner ~ new Scanner (Systen.:n) System.ovt print “Enter the number of colle: °) nt nowberOFGalle = soanner.nextine() oubie monthiyBill = ealeulateBsli (numberofCeiie) System.ovt.prantin( Wonthly 8811+ Re” * monthy8si1) scanner close) > private static double caleulateBili(int nunberOfCaMls) { ouble fixedRentel = 200 SF (nimberOfoslle <= $8) rotors faxedkental 4 (assstionalealle <= 100) flse if (aeditionaltaits <= 208) i secitionalcharge = 199 + 1 + (additéonalcells - 168) + 2.5: assstionalcharge ~ 199+ 1 208 + 2.6 + (addtdonalcalle - 200) + 2: Enter the number of calls: 458 Monthly Bill: Rs 1160.80 ‘SimpleVlumeCakulator ompite undo || cut_| copy || Paste | Find... close Using @ me prograr ul f 1 =4 i. Volume ¢ der = " import java.util.Scanner: public class SimpleVolumeCalculator { public static void main(String{] args) { Scanner scanner = new Scanner (System.in) ; ‘System.out .println("Choose a shape to calculate volume:") System.out .println("1. Sphere"); System.out println("2, Cylinder"); System.out .printIn("3. Cone"); System.out .print("Enter your choice (1, 2, or 3): "): int choice = scanner-nextInt(): ‘switch (choice) { case 1: caloulateSphereVolume(); break; case 2: calculateGylinderVolume(); break; case 3: calculateConeVolume(); break default: System.out.printIn( "Invalid choice. Please choose 1, 2 scanner .close() ;} private static void calculeteSphereVolume() If Scanner scanner = new Scanner (System.in) ; System.out print("Enter the radius of the sphere: "); double radius = scanner.nextDouble(); double volume = (4.6 / 3.8) * Math.PI * Math.pow(radius, 3); System.out .printn("Volune of Sphere: " + volume scanner .close() ;} private static void calculateCylinderVolume() Scanner scanner = new Scanner (System.in) ; System.out .print("Enter the radius of the cy! double radius = scanner.nextDouble(); System.out .print("Enter the height of the cylinder: “); double height = scanner.nextDouble(); double volune = Math.PI * Math.pow(radius, 2) * height; System.out .println("Volune of Cylinder: "+ volume) ; scanner .close();} private static void caleulateConeVolume() ( Scanner scanner = new Scanner (System.in) ; System.out print("Enter the radius of the cone: *) double radius = scanner .nextDouble(); System.out.print("Enter the height of the cone: "); double height = scanner .nextDouble() ; double volume = (Math.PI * Math.pow(radius, 2) * height) / 3 System.out .println("Volune of Cone: " + volume); scanner .close();} inder: *); 3.5} [choose a shape to calculate volume: 1. Sphere 2. Cylinder 3. Cone Enter your choice (1, 2, or 3): 2 Enter the radius of the cylinder: 7 Enter the height of the cylinder: 9 Volume of Cylinder: 1385.4423602330987 compte | undo | cut Copy Paste || Fina... || close import java.util.Scanner; public class DuckNumberChecker { public static void main(String[] args) { Scanner scanner = new Scanner (System.in); System.out.print("Enter a number: "); int number = scanner.nextInt(); Sf (AsDuckNumber (number) { ‘System.out.println(number + ° is a Duck number } else { System.out.println(number +" is not a Duck number.") » he Scanner to avoid scanner .close(); ) private static boolean isDuckNunber(int number) ( String nunberStr = Integer. toString(nunber) ; the di f ' for (int i = 0; i < numberstr.length(); i++) { if (numberStr.charAt(i) == '@") ¢ return true; num : y + return false; // The num m Enter a number: 96658 96658 is a Duck number. 2atomarptichumberC import java.util.Seanner: public class AutonorphicNunberchecker { public static void main(String{] args) { Scanner scanner = new Scanner (Syates.in) system.out.prant( ant number = scanner .nextInt(); {if (4sautonorphicNumber(nunber)) { System.out.printIn(nunber +" is an Automorghic nunber."); ) else { System.out.printIn(nunber + ° is not an Autonorphie nunber > seanner.close(); , private static boolean isAutonorphicHunber(int number) { fine square = number * nunber: String nunberStr = Integer toString{ number) , String squarestr = Integer toString{ square) return squareStr endowith(numberStr) Enter a number: 76 76 is an Automorphic number. ore java.utsl Seamer public class Disariumlumbercheeker { public static void main(steing{] args) ( ‘Scanner acanner = new Scanner (Syeten in) systen.out prant( ant number = scanner. nextrne() AF (ssb4serdunmunver number) { Syston out printia(nnber * "is o Diserium ounber .") ) eiee { Systea.out printn(number + * is rot Disarium nunber.") ? scamer.close(): private static boolean SeDssarsumunber (int number) ( String nunberstr = Integer. toStrsng{nunber). for (ant 4 = 0} 4 « numberSer.tength()? 344) ant digit * Character gethunericValue(nunberStr.charke(4)) sun $= Hath.pon(@igit, 4+ 1) return gum == nueber; Enter a number: 135 135 is a Disarium number. : the duplicate nunbe port java.utal Arrays; public class SimpleMergeSortedArrays public stetic void main(Stringl] args) { ant{] array = (5, 2, 9, 1, 7, 3, 6, 4, 8 18} int[] array2 = (15, 12, 19, 11, 17, 13, 16, 14, 18, 20}; sortandReverse(array1) sortAndReverse(array2): int[] mergedArray = mergeandEliminateDuplicates(array1, array2); isp: rted 2 System.out printin( "Sorted and Merged Array: " + Arraye.toString(mergedArray)) :) private static void sortAndReverse(intI] array) { Arrays.sort (array): for (int start = 8, end = array.length - 1; start < end; start++, end--) { 4 indice int temp = array[start]; array[start] = array[end] arraylend] = tenp;}} : Function to a F m (i private static int[] mergeAndEliminateDuplicates(int[] arrayt, int{] array2) { antl] mergedArray = Arrays.copyof(array!, array!.length + array2.length): int index = array1.length; for (int num : array2) { if (Icontains(mergedArray, num)) { mergedArray[indext+] = nun; + } return Arrays.copyOf(mergedArray, index);) Function eck if an arra ine ific ni private static boolean contains(int[] array, int num) { for (int element : array) { if (element == num) { return > } return false;} Sorted and Merged Array: [98, 65, 45, 33, 28, 12, 9, 6, 5, 4, 906 278, 232, 208, 189, 145, 194, 183, 128, 115] here r " t nunbe r F inport java.util. Scanner; public cless PrimeNunberArray { public static void main(Stringl} args) { Scanner scanner = new Scanner (System.in) System.out.print("Enter the first number: *); int firstNumber = scanner.nextint(): , nd the next 20 pri t int] primeNunbers = findNextPrimes(firstNunber, 28); i 1 y th System. out printIn(*Prine Numbers: for (int prime : primeNumbers) ( Systen.out.print(prime + ° "); i 1 resou 7 scanner .close(); private static int[] findNextPrimes(int startNunber, int count) ¢ intl] primeNunbers = new int{count]; int prameCount = 0 ‘int currentNunber = startNunber; while (primeCount < count) { if (sPrime(currentNunber)) ¢ primeNunbers[primeCount++] = currentNumber ; ) currentNunber++; » return primeNumbers ;} private static boolean isPrime(int number) { if (number <= 1) { return false; + for (int i= 2; i Af (number % return false Math.sqrt(number); i++) ( 8) { > > return true;) Enter the first number: 54 Prime Numbers : 59 61 67 71 73 79 83 89 97 101 183 167 109 113 127 131 137 139 149 151 compile | undo | cx |) copy || pase || find. | cose sport java.util. Scanner public class PrimeFibonacci { ion t af_a number is pri static boolean isPrime(int n) ( af (n <= 1) { return false; » for (int 4 = 2; i <= Nath.sqrt(n); i++) { if (n% i == 8) { return false; » + return true; + public static void main(String{] args) { Scanner scanner = new Scanner (System. in); system.out prant("Enter the first Fibonacci number: "); Ant first = scanner.nextInt() ; System.out.print("Enter the second Fibonacci number: “); Ant second = seanner .nextInt(); antl] fibonacczArray = new int{20] fabonaceiArray[@] = first; fabonaceiArray[1] = second; for (ant i= 2; i < 20; ist) { Fabonaceisrray[a] = fibonacciArray[s - 1] + Tibonacesarrayl. - 2]; » System.out printIn(*Prime Fibonacci nunbers:") for (ant 1 = @; 1 < 26; a+) if (sPrime(fibonaceiArraylil)) { System. out. print (fibonaceiArray[4] + > i scanner .close() ; Enter the first Fibonacci number: 8 Enter the second Fibonacci number: 13 Prime Fibonacci numbers: 13 89 233 1597 28657 import java.util. Scanner; public class StdCodeLookup ( public static void main(Strangl] arge) ( Scanner scanner = new Scanner (Systen in) ; String[] erties = ("Delhi", "Mumbai", "Kolkata", “Chennai”, “Bangelore"); Stringl] stdCodes = ('e11", "622", "Ges", “ead”, “e80"); systen.out.print(“Enter the ity name: *) String AnputCaty = scanner nextLine(): boolssn eftyFound = false; for (snt 1 = 0; 4 « cities. length; i+) { Af (cities{:i].equalsTgnoreCase(inputCity)) ( Systen.out.printla( STO Code for * + eities[i] +”: ° + stdcodes(}); cityround break; t pl mn Sf (lestyFound) { System-out.printIn( "City not found! > scanner.close() Enter the city name: Bangalore STD Code for Bangalore: 68@ | compte] | undo |/ cut || copy || paste || find.. || close he unk d quivaler import java.util. Scanner; public class BinaryTobecimal { public static void moin(Steingl] args) { Scanner scanner = new Scanner (System.in); Get the t nb ‘ System.out.print("Enter a binary number: *); String binaryInput = scanner.nextLine() ; . : ' if (lisBinary(binaryInput)) { systen.out. printin{ ‘Invalid binary number. Please enter 2 valid binary nunber scanner .close(); return; ) invert b int decimalEquivalent = binaryToDecimal (binaryInput) ; system.out.prantin( “Decimal equivalent: " + decimalEquivalent); scanner .close(); , F i b i private static boolean isBinary(String binary) { for (char digit : binary.tocharArray()) ( if (digit |= '0" && digit I= '1') { return » ) return true; , private static int binaryToDecimal(String binary) { int decimalEquivalent = @; int poner = 0; erate h the verse orde! for (int 4 = binary.length() - 1; 1 >= @; 1--) int digit = Character .getNumericValue (binary .charAt(i)); decimalEquivelent += digit + Math.pow(2, power) poner++; i return decimalequivalent ; Enter a binary number: 9016110161 Decimal equivalent: 181 ee Compile Undo | cut_|| copy | paste || rind. || close import java.util.*; public class WordFrequency { public static void main(Stringl] args) { Scanner scanner F new Scanner (Syeten.in) ; System.out.print("Enter the first string: "); String str1 = scanner.nextLine().toLowerCase(); System.out.print("Enter the second string: ") String str2 = scanner.nextLine().toLonercase() String combinedString = str1 +" " + str2 String] words = combinedString.split(’\\s*"); Map wordFrequency = new HashMap<>(); for (String word : words) { wordFrequency.put(word, wordFrequency.getOrDefault(word, 8) + 1) > i th System.out.printn("Words with frequency more than one:"): for (Map.Entry entry : wordFrequency.entrySet()) ( if (entry.getValue() > 1) { Systen.out.printn(entry.getKey()); y » scanner .close(); Enter the first string: What a wonderful world Enter the second string: The world is messed up world with frequency more than one: world pport java.util.Scanner; public class SurnamoWiddleNane ( public static void main(stringf] args) { Scanner scanner = new Scanner (Systen.in); : 1 Lu system.out .print("Enter your full nane (three words): ~); String fullNane = scanner.nextLine() Stringl] nameParts = fullNane. split("\\st"); ane con 1 if (nameParts.length == 3) ( String surname = naneParts(2]; ist x i String middletane naneParts{1] va le nom Systea.out.println("Surnane: " # surname); System.out println("Middle Nae: " + middleName) ; } else ( System out println("Invalid input. Please enter @ name with three words ? seanner.close() ) Enter your full name (three words): Pratham RBK Gowda Surname: Gowda Middle Name: RBK a compile || unde | cut copy || Paste || Find... || close out brary ful : > t 2 import java.util. Scanner; public class ConvertCase { public static void main(String[] args) { Scanner scanner = new Scanner (System.in); System.out .print("Enter a string: "); String inputString = scanner.nextLine( pper cai c : String convertedString = convertCase(inputString) ; System.out .printIn("Converted string: " + convertedString) ; scanner.close(); » private static String convertCase(String input) { char[] charArray = input toCharArray(); for (int i i < charArray-length; i++) F currentChar = charArray[i]; 4¥ (Character isUppertase(currentGhar)) { charArray[i] = Character .toLowerCase(currentChar) ; } else if (Character istowerCase(currentChar)) { charArray[i] = Character.toUpperCase(currentChar); > i return new String(charArray) ; Enter a string: what a wonderful world Converted string: WHAT A WONDERFUL WORLD linport java.util. Scanner; public class Franeword ( public static void main(String[] args) { Scanner scanner = new Scanner (Systen.in); system.out .print("Enter @ sentence in upper case String sentence = scanner.nextLine() c String result = franeandEliminate( sentence) ; System.out .prantin("Result: ~ + result); seanner .close(); > private static String FraneAndEliminate(String input) ( StringBuilder result = new StringBuilder (); Stringl ] words = input .split(”\\s+") for (String word + words) ( result append(word.charat (2) return result.toString().replaceall("(.)(?=.*\\1 Dd: Enter a sentence in upper case: The Elections Were Rigged Result: TEWR Inport java.util. Scanners public class Reversetharacters { public static void main(stringl] args) { ‘Scanner scanner = new Scanner(System.in) systen.out.print("Enter a string: ") String inputString = scanner.nextLine() ; Steing reversedString = reverseCharacters(anputStrang) system. out println( "Reversed string: * + reversedString) scanner .close() ; > private static String reverseCheracters(String input) { StringBuilder reversed = new StrangButlder() for (int 4 = input.length() ~ 1; 4 >=; 4--) { reversed. append( input charat(t)) ’ return reversed.tostring(): Enter a string: Hello World Reversed string: dlroW olleH Encryptivord x Compile || undo | cut | Copy || Paste || Find. || close e gram in Java e ind encrypt ‘import java.util, Scanner; public class Eneryptiord ( public static void main(stringl] args) { Scanner scanner = new Scanner (System. in); systen.out.print("Enter 2 word: "); String inputtiord = scanner.nextLine() ; ncrypt the String encryptedWord = encryptWord(inputWord) ; System.out.println("Encrypted form: " + encryptedWord) ; scanner .close() ; » private static String encryptWord(String word) { StringBuilder encrypted = new StringBuilder(); for (int i = 0; i < word.length(); it+) { char currentChar = word.charAt (3); if (Character.isUpperCase(currentChar)) { encrypted.append((char) (((currentChar - "A’ + 5) % 26) + °A')): } else if (Character.isLowerCase(currentChar)) { encrypted.append((char) (((currentChar - ‘a’ + 5) % 26) + ‘a')): } else { ‘encrypted. append(currentChar) ; + } return encrypted. toString(); Enter a word: Bangalore Encrypted form: Gfslfqtwj import java.util, Seanners public class PrimeElenentsinNatrix { public static void main(Stringl] args) { Scanner scanner = new Scanner(Systen.in); ant{]{] matrix = new int {3][3]; System.out println( "Enter the elesents of the matrix (3 x 3):") for (ant 4 = @; i< 3; art) ( for (ant j= 6; j <3; $+) { System.out.prant("Enter element at position ("+44 "][7 #9 + "]: 7; nnatrax{a][}] = scanner.nextint() > System.out println( "Prine elenente of the matrix along with their posstic for (ant 4 = 0; 4 <3; +4) { for (ant j » 0; j <3; j+4) { Af (asPrine(metrx(2}[}])) { ‘ystem-out.prantin("Prine elenent " + mateix[4][J] +" at position ("+ 4+ "][" +3 #")") ) ? > Scanner. close(); , private static boolean isPrane(1! af (nunber <= 1) { » for (ant a= 2; 4 if (number's return false ? Nath.sqrt(number); irs) { ®) « > return true

You might also like