Wednesday 7 August 2019

SK T4: JAVA Struktur Kawalan Ulangan Do While

/**
 * Do...While untuk mengira nilai faktorial bagi input integer yang dimasukkan oleh pengguna.
 */

import java.util.Scanner;
public class LatihanFactorial {
  public static void main(String[] args) {
  int n;
  int factorial=1;

     Scanner input =new Scanner(System.in);

     System.out.println("Masukkan nombor integer : ");
     n=input.nextInt();
               
     int i=1;
    
     System.out.println("Pengiraan faktorial bagi " + n);
    
     do
     {
         factorial = factorial * i;
         System.out.print(i + " x ");
         i++;
     }
     while(i<n);
                 
 System.out.print( i + " = " +  i*factorial);
   
 }
}



Contoh Output:
Masukkan nombor integer :
7
Pengiraan faktorial bagi 7
1 x 2 x 3 x 4 x 5 x 6 x 7 = 5040
}

0 comments:

Post a Comment

Featured post

Handwritten note... ASK T1 HTML

 

 

SAINS KOMPUTER & ASAS SAINS KOMPUTER Template by Ipietoon Cute Blog Design