Wednesday, October 21, 2009

While Loops and Factorials Help Please?

Please could you help me with this? I would extremely appreciate it.


Factorials - The factorial of n (written n!) is the product of the integers between 1 and n. Thus 4! = 1*2*3*4 = 24. By definition, 0! = 1. Factorial is not defined for negative numbers.


1. Write a program that asks the user for a non-negative integer and computes and prints the factorial of that integer. You'll need a while loop to do most of the work -- this is a lot like computing a sum, but it's a product instead. And you'll need to think about what should happen if the user enters 0.


2. Now modify your program so that it checks to see if the user entered a negative number. If so, the program should print a message saying that a nonnegative number is required and ask the user the enter another number. The program should keep doing this until the user enters a nonnegative number, after which it should compute the factorial of that number. Hint: you will need another while loop before the loop that computes the factorial. You should not need to change any of the code that computes the factorial!


Here is what I have typed out so far into Java. I have it all programmed out correctly for the part where it responds to it not being a negative number, but the only problem is that after I compile and execute it, when the black box pops up on the screen, I type in a nonnegative number, but then after that the factorial doesn’t pop up! I was hoping that you could check over what I have done so far and tell me what I have done wrong and or am missing in this program. I would greatly appreciate it ….thanks in advance for your help.


//************************************...


// Factorials.java


// This application will print out a factorial in response to negative


// and nonnegative numbers.


//************************************...


import java.text.*;


import javax.swing.*;


public class Factorials


{


//--------------------------------------...


// Prints the factorial of an integer when nonnegative and negative


// numbers are added.


//--------------------------------------...


public static void main (String[] args)


{


int sum=1, integer, Fact;


String x= JOptionPane.showInputDialog ("Enter an nonnegative number (0 to quit): ");


Fact = Integer.parseInt(x);


while(Fact %26lt; 0.0)


{


x= JOptionPane.showInputDialog ("Enter an nonnegative number (0 to quit):");


Fact = Integer.parseInt(x);


}


while (Fact %26gt; 0.0);


{sum *= Fact;


Fact--;


}


if (Fact==0)


System.out.println("");


if (Fact%26gt;0)


System.out.println("The factorial is "+ sum);


}


}








Could you please help me with this? Could you check my answers and tell me what you think are the correct ones, because I’m not sure if what I thought at the time was entirely right. Also I can’t figure out part b of #2 and problem #4, could you help me with those as well? Thanks in advance for your time and have a great day.








While Loop Exercise





In a while loop, execution of a set of statements (the body of the loop) continues until the boolean expression controlling the loop (the condition) becomes false. As for an if statement, the condition must be enclosed in parentheses. For example, the loop below prints the numbers from 1 to to LIMIT:


final int LIMIT = 100; // setup


int count = 1;





while (count %26lt;= LIMIT) // condition


{ // body


System.out.println(count); // -- perform task


count = count + 1; // -- update condition


}


There are three parts to a loop:


• The setup, or initialization. This comes before the actual loop, and is where variables are initialized in preparation for the first time through the loop.


• The condition, which is the boolean expression that controls the loop. This expression is evaluated each time through the loop. If it evaluates to true, the body of the loop is executed, and then the condition is evaluated again; if it evaluates to false, the loop terminates.


• The body of the loop. The body typically needs to do two things:


o Do some work toward the task that the loop is trying to accomplish. This might involve printing, calculation, input and output, method calls -- this code can be arbitrarily complex.


o Update the condition. Something has to happen inside the loop so that the condition will eventually be false -- otherwise the loop will go on forever (an infinite loop). This code can also be complex, but often it simply involves incrementing a counter or reading in a new value.


Sometimes doing the work and updating the condition are related. For example, in the loop above, the print statement is doing work, while the statement that increments count is both doing work (since the loop's task is to print the values of count) and updating the condition (since the loop stops when count hits a certain value).


The loop above is an example of a count-controlled loop, that is, a loop that contains a counter (a variable that increases or decreases by a fixed value -- usually 1 -- each time through the loop) and that stops when the counter reaches a certain value. Not all loops with counters are count-controlled; consider the example below, which determines how many even numbers must be added together, starting at 2, to reach or exceed a given limit.


final int LIMIT = 16; TRACE


int count = 1; sum nextVal count


int sum = 0; --- ------- -----


int nextVal = 2; 0 2 1





while (sum %26lt; LIMIT) 2 4 2


{


sum = sum + nextVal; 6 6 3


nextVal = nextVal + 2;


count = count + 1; 12 8 4


}





System.out.println("Had to add together " + (count-1) + " even numbers " +


"to reach value " + LIMIT + ". Sum is " + sum);


Note that although this loop counts how many times the body is executed, the condition does not depend on the value of count.


Not all loops have counters. For example, if the task in the loop above were simply to add together even numbers until the sum reached a certain limit and then print the sum (as opposed to printing the number of things added together), there would be no need for the counter. Similarly, the loop below sums integers input by the user and prints the sum; it contains no counter.


int sum = 0; //setup


String keepGoing = “y”;


int nextVal;





while (keepGoing.equals(“y”) || keepGoing.equals(“Y”))


{


String enterValue = JOptionPane.showInputDialog("Enter the next integer: "); //do work


nextVal = Integer.parseInt(enterValue);


sum = sum + nextVal;





//update condition


keepGoing = JOptionPane.showInputDialog("Type y or Y to keep going");





}





System.out.println("The sum of your integers is " + sum);


Exercises


1. In the first loop above, the println statement comes before the value of count is incremented. What would happen if you reversed the order of these statements so that count was incremented before its value was printed? Would the loop still print the same values? Explain.


The count would become two because the count=count+1 would be before the system.out.println and that would cause the increment operator from the very beginning to add one to one. I would print different values if it was kept the way it is currently because it is after the system.out.println.


2. Consider the second loop above.


a. Trace this loop, that is, in the table next to the code show values for variables nextVal, sum and count at each iteration. Then show what the code prints.


0=0+2 2=2+2 1=1+1


Sum=0 nextVal=2 count=1





b. Note that when the loop terminates, the number of even numbers added together before reaching the limit is count-1, not count. How could you modify the code so that when the loop terminates, the number of things added together is simply count?





3. Write a while loop that will print "I love computer science!!" 100 times. Is this loop count-controlled?


final int LIMIT = 100;


int count = 1;





while (count %26lt;= LIMIT)


{


System.out.println(“I love computer science!!”);


count = count + 99;


}


4. Add a counter to the third example loop above (the one that reads and sums integers input by the user). After the loop, print the number of integers read as well as the sum. Just note your changes on the example code. Is your loop now count-controlled?





5. The code below is supposed to print the integers from 10 to 1 backwards. What is wrong with it? (Hint: there are two problems!) Correct the code so it does the right thing.


count = 10;


while (count %26gt;= 0)


{


System.out.println(count);


count = count + 1;


}








count = 10;


while (count %26gt;= 0)


{


count = count -1;


System.out.println(count);





}


count = 10;


while (count %26gt;= 0)


{


System.out.println(count);


count = count -1;


}

While Loops and Factorials Help Please?
Here goes but this is not going to be easy as you have a lot of information that needs to be digested.





Question 1.


int sum=1, integer, Fact;


Here we see that you have defined integer as an int. You have also defined Fact as an int too.


Why have you defined integer as an int?





Also with Fact being an int why are you testing it against 0.0?


0.0 is a floating point value. You should be comparing like with like


so


if (Fact%26gt;0)





"And you'll need to think about what should happen if the user enters 0."


You are allowing 0 to terminate the program.


I don't think that that is what is required.


I think that you have to output the value 1 if 0 is input.


I also think that you should have another way of terminating the program like input x.





2b.


I want to just give the answer but then you will never learn. I want to help in a way that you will learn and progress.





So


Start loop with a while test (sum%26lt;LIMIT)


Now add nextVal to sum. This will sum all on the even numbers


Increment nextVal by 2. This will get the next even number.


If this is the first time through the loop then I want count to now be 1. This is the problem that you need to solve.





So add 1 to count.


End loop.





So what was the value of count at the start of the program which will give a first pass through the loop a value of 1 in count?





4.


This not difficult. You should be able to solve this yourself.





5.


This is the correct answer.





count = 10;


while (count %26gt; 0)


{


System.out.println(count);


count = count -1;


}



annual credit report

No comments:

Post a Comment