Factorial Calculator

--Originally published at Loading…

This one was a little harder than the others, and it was a challenge to begin with because I couldn’t remember what is a factorial. But after I asked Ken and he explained me ( )…ie_factorial__6…the activity, it wasn’t too difficult. The activity consist in this:

Create a program that asks the user for a non-negative integer (let’s call that number n) and display for them the value of n! (n factorial).

After showing them the answer, ask them if they would like to try another number (with a simple y/n response) and either ask again (for y) or quit the program and wish them a nice day (if they answered n).

So, the first thing was do a function for the factorial, I established this function with the loop for, I wasn’t too sure of how doing it, so with the help of one page of Google and a classmate’s blog I could do it . In my main I established my variables as int and char, and then add the loop do/while for repeat until the user said no. Inside the “do” I put an if for the case that the number should be negative. This is my code:

factorial01factorial02

And this is how it works ?:

factorial03


(WSQ04) Please, don’t joke around

--Originally published at Programming Path

The activity is to run a program where it gives you the sum of a range of numbers, which that range is asked to the user. We needed to be careful if the user inserts the numbers in the wrong order. If that happens then we should put something like “your wrong, please enter it again.

Here is the code:

sum2

Again the code:

#include <iostream>
using namespace std;

int main () {
int l, h, i, r = 0;
cout << “I will sum the numbers in the range you give.” << endl;
cout << endl << “Please enter the lowest number: “;
cin >> l;
cout << “Please enter the highest number: “;
cin >> h;
if (h < l) {
cout << “That number is lower than the first one, please don’t joke around.” << endl;
cout << “Enter the highest number again, please: “;
cin >> h;
}
if (l == 0) {
for (i = 0 ; i <= h; i++) {
r = r + i;
}
}
else {
for (i = l; i <= h; i++) {
r = r + i;
}
}
cout << “The sum from ” << l << ” to ” << h << ” is: ” << r << endl;
return 0;
}

This are the result, with two options:

Wrong order:

I will sum the numbers in the range you give.

Please enter the lowest number: 3
Please enter the highest number: 2
That number is lower than the first one, please don’t joke around.
Enter the highest number again, please: 7
The sum from 3 to 7 is: 25

Right order:

I will sum the numbers in the range you give.

Please enter the lowest number: 3
Please enter the highest number: 7

Continue reading "(WSQ04) Please, don’t joke around"