






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Three Java programming examples. The first example prints a pattern of asterisks and ampersands based on user input. The second example rearranges a given string by swapping every pair of characters. The third example accepts user input of numbers and prints the sum of each pair. Additionally, the third example identifies which pair of numbers has the greatest sum. The fourth example checks if a given string can be a palindrome by changing a single character.
Typology: Assignments
1 / 12
This page cannot be seen from the preview
Don't miss anything!
Code: import java.util.Scanner; public class Main { public static void main(String[] args) { int m,n; Scanner s=new Scanner(System.in); System.out.println("Enter number of rows :"); m=s.nextInt(); System.out.println("Enter number of columns :"); n=s.nextInt(); System.out.println("Pattern :"); if(m==n) { for(int i=1;i<=m;i++) {
for(int j=1;j<=n;j++) { if(i==j) { System.out.print("#"); } else if(i%2==1) { System.out.print("*"); } else { System.out.print("&"); } } System.out.println(""); } } else { for(int i=1;i<=m;i++) { for(int j=1;j<=n;j++)
public static String swapPair(String str) { if (str == null || str.isEmpty()) return str; char[] ch = str.toCharArray(); for (int i = 0; i < ch.length - 1; i += 2) { char temp = ch[i]; ch[i] = ch[i + 1]; ch[i + 1] = temp; } return new String(ch); } public static void main(String args[]) { String str; Scanner scnr = new Scanner(System.in); System.out.print("Enter a string: "); System.out.println(); str = scnr.nextLine(); System.out.print("String after rearrange: "); System.out.println(); System.out.println(swapPair(str)); } }