1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
/* Java Pyramid 1 Example This Java Pyramid example shows how to generate pyramid or triangle like given below using for loop. * ** *** **** ***** */ public class JavaPyramid1 { public static void main(String[] args) { for(int i=1; i<= 5 ;i++){ for(int j=0; j < i; j++){ System.out.print("*"); } //generate a new line System.out.println(""); } } } /* Output of the above program would be * ** *** **** ***** */ |
thanks for your program
Please show me this java code:-
1
1 2 1
1 2 4 2 1
1 2 4 8 4 2 1
1 2 4 8 16 8 4 2 1
1 2 4 8 16 32 16 8 4 2 1
1 2 4 8 16 32 64 32 16 8 4 2 1
1 2 4 8 16 32 64 128 64 32 16 8 4 2 1
public class GeneratePrimeNumbersExample {
public static void main(String[] args) {
int k = 0;
int temp = 0;
for(int i=1; i<=5; i++)
{
for(int j=1; j<=(2*i)-1; j++)
{
if(j==1)
{
k=i;
temp=1;
}
if(j<k) {=”” system.out.print(temp+”=”” “);=”” temp=”temp*2;” }=”” else=”” if(j=”=k)” {=”” system.out.print(temp+”=”” “);=”” }=”” else=”” {=”” temp=”temp/2;” system.out.print(temp+”=”” “);=”” }=”” }=”” system.out.println(“=”” “);=”” for=”” next=”” line=”” (line=”” change)=”” }=”” }=”” }=””>
public static void value_times_two(int n) {
int s, p;
s = 1;
p = 1;
for (int i = 1; i <= n; i++) {
p = 1;
System.out.print(p);
while (s <= i) {
System.out.print(p *= 2);
s++;
}
while (s > 1) {
System.out.print(p /= 2);
–s;
}
System.out.println();
}
}
import java.lang.Math;
class shape
{
public static void main(String a[])
{
int rows=8,i,j,half,t=1,ttemp;
double ptemp=0,power=0,temp;
for(i=1;i<=rows;i++)
{
half=t/2;
power=0;
for(j=1;j<=t;j++)
{
if(half>=power)
{
temp=Math.pow(2.0,power);
ttemp=(int)temp;
System.out.print(” ” + ttemp);
ptemp=power;
power++;
}
else
{
ptemp–;
temp=Math.pow(2,ptemp);
ttemp=(int)temp;
System.out.print(” ” + ttemp);
}
}
t=t+2;
System.out.println();
}
}
}
/*
1
1 2 1
1 2 4 2 1
1 2 4 8 4 2 1
1 2 4 8 16 8 4 2 1
1 2 4 8 16 32 16 8 4 2 1
1 2 4 8 16 32 64 32 16 8 4 2 1
1 2 4 8 16 32 64 128 64 32 16 8 4 2 1
*/
public class SurePyramid {
public static void main(String[] args) {
int k =0;
int t=0;
for(int i=1; i<= 7 ;i+=2){
t=(int)Math.round(i/2);
for(int j=0; j < i; j++){
if(j<=t)
k =(int)Math.pow(2,j);
else
k =(int)Math.pow(2,(–t));
System.out.print(k+””);
}
//generate a new line
System.out.println(“”);
}
}
}
please show me coding for
***********
***** *****
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
***** *****
***********
for(int i=1; i<=6; i++){
for(int j=6; j>=1; j–){
System.out.print(“**”);
}
System.out,println();
}
for(int k=1; k<=5; k++){
for(int l=0; l<=k; l++>){
System.out.print(“**”);
}
System.out,println();
}
5
10 15
15 20 25
25 30 35 40
40 45 50 55 60
1
12
123
1234
12345
for(int z = 1 ; z <= 5 ; z ++ )
{
for(int x = 1 ; x <=z ; x++)
{
System.out.print(“*”);
}
System.out.println(“”);
}
for (int r=1; r<=4>; r++){
for(int y=1; y<=r; y++){
System.out.print(y);
}
System.out.printla ();
}
/*
1
1 2 1
1 2 4 2 1
1 2 4 8 4 2 1
1 2 4 8 16 8 4 2 1
1 2 4 8 16 32 16 8 4 2 1
1 2 4 8 16 32 64 32 16 8 4 2 1
1 2 4 8 16 32 64 128 64 32 16 8 4 2 1
*/
import java.lang.Math;
class shape
{
public static void main(String a[])
{
int rows=8,i,j,half,t=1,ttemp;
double ptemp=0,power=0,temp;
for(i=1;i<=rows;i++)
{
half=t/2;
power=0;
for(j=1;j<=t;j++)
{
if(half>=power)
{
temp=Math.pow(2.0,power);
ttemp=(int)temp;
System.out.print(” ” + ttemp);
ptemp=power;
power++;
}
else
{
ptemp–;
temp=Math.pow(2,ptemp);
ttemp=(int)temp;
System.out.print(” ” + ttemp);
}
}
t=t+2;
System.out.println();
}
}
}
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
please send me programming of this type in java
for (int i=0; i<5;i++){
for(int x=0; x<=i; x++){
System.out.print(“*”);
}
System.out.printla ();
}
for (int r=4; r>=1; r–){
for(int y=1; y<r; y++){=”” system.out.print(“*”);=”” }=”” system.out.printla=”” ();=”” }=””>
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
System.out.println(“Enter n number :: “+n);
for(int i=1 ; i<=n;i++){
for(int j = 0 ;j<i;j++){ (j=”0+1;j<5;j++)” for=”” print=”” the=”” number=”” in=”” next=”” line=”” with=”” 2345,345,45,5=”” system.out.print(“*”+”=”” “);=”” }=”” system.out.println(“”);=”” if(i=”=n){” for(int=”” l=”n-1;l”>0;l–){
for(int k= 0 ;k<l;k++){ system.out.print(“*”+”=”” “);=”” }=”” system.out.println(“”);=”” }=”” }=”” }=””>
how about this code?
12345
2345
345
45
5
the reverse of this instead of staring from the left to right
it will start from right to left
thanks
for(int i=0; i<5; i++){
for(int j=1+i; j<=5; j++){
System.out.println(j);
}
System.out.println();
}
how to
1
3 2
6 5 4
10 9 8 7
*****
*****
*****
*****
*****
How to bring this output?
for (int r=0; r<5; r++){
for(int y=0; y<5; y++){
System.out.print(“*”);
}
System.out.printla ();
}
||||
| |
||||
How About this?
– *
– **
– ***
– ****
– *****
for(int i=1; i<=5; i++){
for(int j=0; j<=i;j++){
if(j==0){
System.out.print(“_”);
}
else{
System.out.print(“*”);
}
}
System.out.println();
}
print following pattern.
1
11
121
1221
12321
123321
1234321
12344321
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int i=0,j=1;
for(i=0;i<=n-1;i++)
{
if(i==0)
{
System.out.print("1");
}
else
{
System.out.print(i);
for(j=1;j<i;j++)
{
System.out.print("0");
}
if(i!=0)
{
System.out.print(i);
}
}
System.out.println();
}
}
}
public void pyramid(int n) {
for (int i = 1; i < n; i++) {
for (int j = 1; j = 1; j–) {
System.out.print(j);
}
System.out.println();
for (int j = 1; j = 1; j–) {
System.out.print(j);
}
System.out.println();
}
}
HOW TO PROGRAM that the output is looks likes a diamond,,,,
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
public class Pyramid_7 {
public static void main(String[] args) {
for (int i = 1; i <5; i++) {
for (int j = 1; j <=i; j++) {
System.out.print(j);
}
System.out.println(“”);
}
for (int i = 5; i >=1; i–) {
for (int j = 1; j <=i; j++) {
System.out.print(j);
}
System.out.println(“”);
}
}
}
I find it unnecessarily confusing
(1) to have the loop over “i” start from 1 and have the termination condition be “<=” while the loop over “j” starts from 0 and has a termination condition of “<“.
(2) to use “i++” and “j++” as the update statement. The syntax for post-increment adds nothing to the explanation of for-loops and would be better done as “i = i + 1” and “j = j + 1”.
(3) to use println(“”) instead of plain println().
loop example is false
Hi,
how to print caharacters in the array