Thursday, October 23, 2014

Building Java Programs A Back To Basics Approach - 3rd Edition
by Stuart Reges and Marty Stepp

Chapter 2 Exercise 3

Write nested for loops that compute and print the factorial of integers from 1 
to 7.


/* Copyright Mihai George Forlafu @ 2014
 * Copenhagen School of Design and Technology
 * Building Java Programs A Back To Basics Approach 5th edition
 * Chapter 2 Exercise 3, page 156
 */

package ch2_ex3;

public class Ch2_Ex3 
{

    public static void main(String[] args) 
    {
         int n = 7;
         for (int i = 1; i<=n; i++)
         {
             int factorial = 1;
             for (int j = 1; j<= i; j++)
             {
                 factorial = factorial * j;
             }
             System.out.println(i + "! = " + factorial);
         }
          
    }
 

0 comments:

Post a Comment