--Originally published at my programming blog
This week I needed to do the assignment WSQ06, where I needed to aske the user for a number and calculate its factorial, also the number couldn’t be negative and I needed to ask the user if he wanted to try again or no.
So what I did was:
- I named my variables, an integer “x”, a string ans, and a long “factorial” (which is like an int but it has space for more numbers)
- I made a do while loop which makes all the request and at the end if the condition of the while is true then it repeats.
- Inside the do while loop I asked the user for a number and if the number was negative I did an if so that the user could enter a positive number and then I used an else where if the number was positive the program calculated the factorial.
- The factorial was calculated by a for loop, I names int i=1 and the if i was smaller or equal to the number the user had entered each time this loop would repeat itself.
- Inside the for loop I equaled the long factorial to factorial*i so that each time the loop repeats the factorial multiplies bye the value of i (that increases each time) until i is equal to the number entered.
- Then I printed the result of the factorial and asked the user if he wanted to try again.
- if the user entered y for YES the program would repeat, but if the user entered n the program would stop and print “Okay thank you and have a good day!”.
- This was done with while of the do while loop, the condition of the while was that while the character ans was not equal to n then the program Continue reading "Factorial Calculator"