






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
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)); } }