Java program to demonstrate looping 1 : For « Language Basics « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » Language Basics » ForScreenshots 
Java program to demonstrate looping 1
Java program to demonstrate looping 1

/*
Java Programming for Engineers
Julio Sanchez
Maria P. Canton


ISBN: 0849308100
Publisher: CRC Press
*/


// File name: Factorial.java
//Reference: Chapter 10
//
//Java program to demonstrate looping
//Topics:
// 1. Using the for loop
// 2. Loop with multiple processing statements
//
//Requires:
// 1. Keyin class in the current directory

public class Factorial {

  public static void main(String[] args) {

    int number;
    int facProd;
    int curFactor;

    System.out.println("FACTORIAL CALCULATION PROGRAM");
    number = Keyin.inInt("Enter a positive integer: ");

    facProd = number; // Initializing

    for (curFactor = number - 1; curFactor > 1; curFactor--) {
      facProd = curFactor * facProd;
      System.out.println("Partial product: " + facProd);
      System.out.println("Current factor:  " + curFactor);
    }

    // Display the factorial
    System.out.println("\n\nFactorial is: " + facProd);
  }
}

//**********************************************************
//**********************************************************
//Program: Keyin
//Reference: Session 20
//Topics:
//1. Using the read() method of the ImputStream class
//  in the java.io package
//2. Developing a class for performing basic console
//  input of character and numeric types
//**********************************************************
//**********************************************************

class Keyin {

  //*******************************
  //   support methods
  //*******************************
  //Method to display the user's prompt string
  public static void printPrompt(String prompt) {
    System.out.print(prompt + " ");
    System.out.flush();
  }

  //Method to make sure no data is available in the
  //input stream
  public static void inputFlush() {
    int dummy;
    int bAvail;

    try {
      while ((System.in.available()) != 0)
        dummy = System.in.read();
    catch (java.io.IOException e) {
      System.out.println("Input error");
    }
  }

  //********************************
  //  data input methods for
  //string, int, char, and double
  //********************************
  public static String inString(String prompt) {
    inputFlush();
    printPrompt(prompt);
    return inString();
  }

  public static String inString() {
    int aChar;
    String s = "";
    boolean finished = false;

    while (!finished) {
      try {
        aChar = System.in.read();
        if (aChar < || (charaChar == '\n')
          finished = true;
        else if ((charaChar != '\r')
          s = s + (charaChar; // Enter into string
      }

      catch (java.io.IOException e) {
        System.out.println("Input error");
        finished = true;
      }
    }
    return s;
  }

  public static int inInt(String prompt) {
    while (true) {
      inputFlush();
      printPrompt(prompt);
      try {
        return Integer.valueOf(inString().trim()).intValue();
      }

      catch (NumberFormatException e) {
        System.out.println("Invalid input. Not an integer");
      }
    }
  }

  public static char inChar(String prompt) {
    int aChar = 0;

    inputFlush();
    printPrompt(prompt);

    try {
      aChar = System.in.read();
    }

    catch (java.io.IOException e) {
      System.out.println("Input error");
    }
    inputFlush();
    return (charaChar;
  }

  public static double inDouble(String prompt) {
    while (true) {
      inputFlush();
      printPrompt(prompt);
      try {
        return Double.valueOf(inString().trim()).doubleValue();
      }

      catch (NumberFormatException e) {
        System.out
            .println("Invalid input. Not a floating point number");
      }
    }
  }
}
           
       
Related examples in the same category
1. For loop: all conditions
2. for Demofor Demo
3. Check out for loopCheck out for loop
4. Java labeled for loop.Java labeled for loop.
5. Demonstrates for loop by listing all the lowercase ASCII letters.Demonstrates for loop by listing all the lowercase ASCII letters.
6. Comma OperatorComma Operator
7. Java program to demonstrate loopingJava program to demonstrate looping
w_w__w__.j__a__v_a_2s___._c_om___ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.