How to write java program to print pyramid of stars?
Hi,
I am T.Y.B.Sc.IT student. I recently started learning java language. Yesterday our sir has given us assignment in which there is a program like write java program to print pyramid of stars. I used lot of various method but I didn't get the solution. If you have any idea about it please share with me.
Re: How to write java program to print pyramid of stars?
This is simple program to make pyramid.
Code:
public class pyramid
{
public static void main(String args[])
{
for(int x=1;x<10;++)
{
for(int y=o;y<x;++)
{
System.out.print("*");
}
System.out.println();
}
}}
Re: How to write java program to print pyramid of stars?
Code:
public class pyramid
{
public static void main(String args[])
{
int c=1;
for(int x=1;x<5;++)
{
for(int y=x;y<5;++)
{
System.out.print(" ");
}
for(int t=1;t<=c;t++)
{
if(t%2==0)
System.out.print(" ");
else
System.out.print("*");
c+=2
}
}}
Re: How to write java program to print pyramid of stars?
Code:
public static final void printStars(final int height) {
final int tl = (height * 2) - 1;
final int tl_2 = tl / 2;
StringBuilder str = new StringBuilder(tl);
for (int p = 0; p < tl; ++p) {
str1.append(' ');
}
for (int i = 0; i <= tl_2; ++i) {
str1.setCharAt(tl_2 + i, '*');
str1setCharAt(tl_2 - i, '*');
System.out.println(str1);
}
}
Re: How to write java program to print pyramid of stars?
I think this is simplest program.
Code:
package pyramid;
class pyramid{
public void pyramid(){
}
public static void main(String[] args){
for(int p=1; p<10; p++){
for(int q = 0; q < p; q++){
System.out.print("*");
}
System.out.println();
}
}
}
Re: How to write java program to print pyramid of stars?
Code:
public class PyramidStars {
public static void main(String[] args)
{
java.util.Scanner scanner = new java.util.Scanner(System.in);
System.out.print("Enter no. you want : ");
int S = scanner.nextInt();
for (int t = 1; t <= S; t++) {
for (int s = 1; s<=t; s++) {
System.out.print("*");
}
System.out.println();
}
}
}
pyraRe: How to write java program to print x shape of stars?
how to write java program to print x shape of stars?