Type-1 * * * * * * * * * * * * * * * * * Type-2 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Type-1
இதில் முதல் row-வில் முதல் மற்றும் கடைசியாக , stars print செய்யப்பட்டுள்ளது. இரண்டாவது row-வில், இரண்டாவது position மற்றும் கடைசியிலிருந்து இரண்டாவது position-னில் stars print செய்யப்பட்டுள்ளது . மூன்றாவது row-வில் மூன்றாவது position மற்றும் கடைசியிலிருந்து மூன்றாவது position-னில் stars print செய்யப்பட்டுள்ளது. இதே போல் ஒவ்வொரு row-விலும் stars print செய்யபட்டுள்ளது
ஆகவே row-ன் value-ம் column value-ம் எப்பொழுது சமமாக வருகின்றதோ அப்பொழுது star print செய்தால் போதும். சமமாக இல்லாத பொழுது space print செய்யவேண்டும். அதற்காகதான் இரண்டு loop-ன் values (-n)ல் துவங்கி n-ல் முடியவேண்டும்.
Complete Program
import java.util.Scanner;
public class PrintPattern {
public static void main(String[] args) {
Scanner sn = new Scanner(System.in);
System.out.print("Enter how many rows you want: ");
int n = sn.nextInt();
for (int row = -n; row <= n; row++) {
for (int col = -n; col <= n; col++) {
if (row * row == col * col) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
Enter how many rows you want: 5
* * * * * * * * * * * * * * * * *
Type-2
இதுவும் type-1 logic-ஐ போன்று row-ன் value-ம் column value-ம் எப்பொழுது சமமாக வருகின்றதோ அப்பொழுது space print செய்தால் போதும். சமமாக இல்லாத பொழுது star print செய்யவேண்டும்.
Complete Program
import java.util.Scanner;
public class PrintPattern {
public static void main(String[] args) {
Scanner sn = new Scanner(System.in);
System.out.print("Enter how many rows you want: ");
int n = sn.nextInt();
for (int row = -n; row <= n; row++) {
for (int col = -n; col <= n; col++) {
if (row * row == col * col) {
System.out.print(" ");
} else {
System.out.print("*");
}
}
System.out.println();
}
}
}
Enter how many rows you want: 5
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Pgcomments
இது பற்றிய தங்களின் கருத்துகளை இங்கே பதிவிடுங்கள் . இது பயனுள்ளதாக விரும்பினால் மற்றவர்களுக்கும் இதை share செய்யுங்கள்.
Comments