2008年12月11日 星期四

simple java(十一)---application(9)

Write a nested for loop to print the following pattern:
*
***
*****
*******
*********
***********
solution:
public class range {

public static void main ( String args [] ) {

int i,j;

for(i=1;i<=11;i+=2){

for(j=1;j<= i;j++){
System.out.print("*");}
System.out.print("\n");}}}
*************************
Write a program to ask the user to enter a positive integer N and then compute N! using afor loop to repeatedly multiply numbers from 1 to N to a value.
solution:
import javax.swing.*;
public class range {

public static void main (String args[]){
int limit = 0;int result = 1;
limit = Integer.parseInt(JOptionPane.showInputDialog( "Enter limit"));

for(int i=1;i<=limit;i++){
result *= i;}
System.out.print(result);
}}

沒有留言: