ok

Sunday, 26 March 2017

Contoh penggunaan perintah perulangan dalam program java

Pyramid Programs in Java 
Codingnya adalah
- Number triangle pyramid program in Java



import java.util.Scanner;
public class NumberTriangle
{
 public static void main(String[] args) 
 {
  int num;
  System.out.print("Enter loop repeat number(rows): ");
  Scanner sc = new Scanner(System.in);
  num = sc.nextInt();
  for (int r = 1; r <= num; r++) {
   for (int sp = 1; sp <= num - r; sp++) {
    System.out.print("  ");                         //it is 2 blank space
   }
   for (int c = 1; c <= 2 * r - 1; c++) {
    System.out.print(" " + c);
   }
   System.out.println();
  }
 }
}

 
- Number triangle pyramid program in Java


import java.util.Scanner;
public class NumberTraingle
{
 public static void main(String[] args) 
 {
  int num;
  System.out.print("Enter number of rows : ");
  Scanner sc = new Scanner(System.in);
  num = sc.nextInt();
  for (int r = 1; r <= num; r++) {
   for (int sp = num - r; sp > 0; sp--) {
    System.out.print(" ");
   }
   for (int c = 1; c <= r; c++) {
    System.out.print(r);
   }
   for (int k = 2; k <= r; k++) {
    System.out.print(r);
   }
   System.out.println();
  }
 }
}

Share:

Baca Juga Artikel

Like

Rusdyanha.com