Qn: Write the logic to print star * as right pyramid and left pyramid as follows
Type-1 Type-2 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-By Admin, Last Update On 30th May,2019 09:57 am
Type-1
Type-1-ஐ நன்றாக கவனித்தால் இதை கீழ்க்கண்டவாறு இரண்டாக பிரிக்கலாம்.
part-1 * * * * * * * * * * * * * * * * * * * * * * * * * part-2
n=9 rows, அதோடு part-1,part-2 ஆகிய இரண்டும் ஒன்று சேர்ந்த தொக்குப்பு தான் இந்த Type-1. ஆகையால் இவ்விரண்டின் logic-ஐயும் ஒன்றாக சேர்த்தல் நமக்கு தேவையான output கிடைத்துவிடும்.
Part-1: இதில் 5 rows இடம் பெற்றுள்ளது. ஆகையால் row ஆனது 1-லிருந்து துவங்கி n-ல் முடியவேண்டும்.for(row=1;row<=n;row++){ for(col=1;col<=row;col++){ System.out.print("*"); } System.out.println(); }Part-2: இதில் 4 rows இடம் பெற்றுள்ளது. ஆகையால் row ஆனது (n-1)-லிருந்து துவங்கி 1-ல் முடியவேண்டும்.
for(row=n-1;row>=1;row--){ for(col=1;col<=row;col++){ System.out.print("*"); } System.out.println(); }
இபொழுது part-1 part-2 ஆகிய இரண்டையும் ஒன்று சேர்க்கவேண்டும்.
Complete Code
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();
//part-1
for(int row=1;row<=n;row++){
for(int col=1;col<=row;col++){
System.out.print("*");
}
System.out.println();
}
//part-2
for(int row=n-1;row>=1;row--){
for(int col=1;col<=row;col++){
System.out.print("*");
}
System.out.println();
}
}
}
Output:
Enter how many rows you want: 5
Enter how many rows you want: 5
* * * * * * * * * * * * * * * * * * * * * * * * *
Type-2
Type-2-ஐ நன்றாக கவனித்தால் இதை கீழ்க்கண்டவாறு இரண்டாக பிரிக்கலாம்.
part-1 * * * * * * * * * * * * * * * * * * * * * * * * * part-2
மேலே உள்ள இரண்டிற்கான code-ஐ ஒன்றாக சேர்த்தால் தேவையான output கிடைத்துவிடும்.
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();
int row,col,s;
//part-1
for(row=1;row<=n;row++){
for(s=1;s<=(n-row);s++){
System.out.print(" ");
}
for(col=1;col<=row;col++){
System.out.print("*");
}
System.out.println();
}
//part-2
for(row=n-1;row>=1;row--){
for(s=1;s<=(n-row);s++){
System.out.print(" ");
}
for(col=1;col<=row;col++){
System.out.print("*");
}
System.out.println();
}
}
}
Output:
Enter how many rows you want: 5
Enter how many rows you want: 5
* * * * * * * * * * * * * * * * * * * * * * * * *
Pgcomments
இது பற்றிய தங்களின் கருத்துகளை இங்கே பதிவிடுங்கள் . இது பயனுள்ளதாக விரும்பினால் மற்றவர்களுக்கும் இதை share செய்யுங்கள்.
Comments