Factorial Calculator

--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:

  1. 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)
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. Then I printed the result of the factorial and asked the user if he wanted to try again.
  7. 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!”.
  8. 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
    screen-shot-2017-02-22-at-12-45-16-pm
    screen-shot-2017-02-22-at-2-47-38-pm
    Continue reading "Factorial Calculator"

On to functions!

--Originally published at my programming blog

What we needed to do in this assignment was to use the first program we did and make functions ( like the addition, difference, division, etc.).

This assignment was easy because I only needed to do simple functions.

  1. So the first thing I did was to make my functions, I named them Sum, Difference, Product, Division and Remainder.
  2. Then in the parameters I included the integers a and b.
  3. And in the return value I put (a+b), (a-b), (a*b), (a/b) and (a%b).
  4. Then I made the int main
  5. Then I asked the user for two integers and then I printed the results of each function.

Here is my code:

#include <iostream>
using namespace std;

int Sum(int a, int b){return (a+b);}
int Difference(int a, int b){return (a-b);}
int Product(int a, int b){ return (a*b);}
int Division(int a, int b){return (a/b);}
int Remainder(int a, int b){return (a%b);}

int main()
{
int a, b;
cout<<“Please enter two integers:”<<endl;
cin>>a>>b;
cout<<“The sum of these integers is: “<<Sum(a,b)<<endl;
cout<<“The difference between the integers is: “<<Difference(a,b)<<endl;
cout<<“The product of the integers is: “<<Product(a,b)<<endl;
cout<<“The division of the integers is: “<<Division(a,b)<<endl;
cout<<“The remainder of the division of the integers is: “<<Remainder(a,b)<<endl;
return 0;
}

screen-shot-2017-02-16-at-8-52-15-am

When I run it, it gives something like this:

screen-shot-2017-02-16-at-8-53-03-am

Thanks for reading! I hope it helps


Sum of numbers

--Originally published at my programming blog

This week the activity I needed to do was create a program that would ask the user for two numbers, the lower bound and the upper bound, and the program would give the user the sum of all the numbers from the lower bound to the upper bound.

I had some trouble with this because I was a little confused because every program from my classmates or on the internet were different so when Ken explained it in class I understood.

So what I did was:

  1. Named 3 integers, x,y and sum.
  2. I equaled sum to 0.
  3. Then I asked the user for the lower and upper bound.
  4. I used a while loop so if the user gave me a higher number in the lower bound the program would ask the user to enter the numbers in the right order.
  5. After that I used a condition where if the integers where in the right order there would be a for loop.
  6. In the for loop is like instructions, it is stated that while the int i that is equaled to x is less or equal to y the second number , i will increase.
  7. And then I equaled sum to i.
  8. Finally I printed the sum of the range.

The code:

#include <iostream>
using namespace std;
int main ()
{

int x, y, i, sum=0;
cout<< “Let’s to the sum of all the numbers in a range.”<<endl;
cout<< “Please give me the lower bound: “<< endl;
cin>> x;
cout<< “Please give me the upper bound: “<<endl;
cin>> y;

while (x>y){
cout<<“Please enter the numbers in the right order (from small to big).”<<endl;
cout<< “Please give me the lower bound: “<< endl;
cin>> x;
cout<< “Please give me the upper bound: “<<endl;
cin>> y;
}

if(x<=y){
for(int i=x; i<=y;

screen-shot-2017-02-08-at-11-24-21-am
screen-shot-2017-02-08-at-11-25-12-am
Continue reading "Sum of numbers"

Guess the number

--Originally published at my programming blog

The assignment is to create a program in which  you need to guess the number the computer was thinking, and if you get it wrong the program tells you if its too high or to small and when you get it right it tells the number of times it took you to get it right.

Here is my code:

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main (){
int x, secret,y=0;
srand(time(NULL));
secret=(rand()%100+1);
cout<< “I chose  a number between 1-100, can you guess it?: “<<endl;

do {
cout<<“Write your guess:”<<endl;
cin>>x;
y=y + 1;
if(x>secret){
cout<<“Wrong! ” <<x<< ” is too high, try again.”<<endl;
} else {
if (x<secret){
cout<<“Wrong! “<<x<< ” is too low, try again.”<<endl;
}
}
}while(x!=secret);
cout<<“Congratulations you got it right!!”<<endl;
cout<<“It took you “<<y<<” times to get it right.”<<endl;
return 0;
}

screen-shot-2017-01-31-at-7-40-53-amscreen-shot-2017-01-31-at-7-42-10-am

  1. I included the libraries “cstdlib” and “ctime” so that I could use the srandom function and the time function.
  2. Then I named my variables (the integers “x”, “y” and “secret”)
  3. I wrote the function of srand and inside the parenthesis I wrote time(NULL) so that the random number is always different.
  4. Then I equaled the integer “secret” to the random function “rand” and put %100 +1 to specify that I want a number between 1 and a 100.
  5. I added the cout to ask the user for a number.
  6. I made a loop called do-while, this means that while a condition is true this loop will repeat, so I did it with the condition that x!=secret, so while the user doesn’t guess the secret number this will repeat itself.
  7. Inside the loop I added an if so if x>secret the program tells you your number is too high, and another if so if
    Continue reading "Guess the number"

Temperature in Celsius

--Originally published at my programming blog

So this week my assignment was to ask the user to give me a temperature in Fahrenheit and my program would give the user the temperature in Celsius and tell him the state of water in that temperature.

I included some mastery topics, I used nested ifs, the use of ifs and elses.

It wasn’t hard, I enjoyed this assignment! I thought the use of ifs would be hard but I understood it pretty fast.

If you don’t understand the use of ifs, I’ll explain what I did, so I just put an if and inside that if I added another if and after that if an else. So first I thought okay.. if the degrees in Celsius are more or equal to a 100, water was gas, then inside that if I added if the degrees where less or equal to 0 water was  solid and if it wasn’t neither of them water was liquid.

This is my code:

screen-shot-2017-01-26-at-10-36-44-am

#include <iostream>
using namespace std;
int main()
{
int F, C;
cout<< “Do you want to convert Fahrenheit to Celsius?”<<endl;
cout<< “And know what is the state of water in that temperature?”<<endl;
cout<< “Let’s begin!”<< endl;
cout<< “What is the temperature in Fahrenheit?”<<endl;
cin>> F;
cout<< “A temperature of “<<F<< ” degrees Fahrenheit is: “<<endl;

C=5*(F-32)/9;
cout<< C << ” degrees Celsius” << endl;
if (C>=100) {
cout<< “Water is in a gas state.”<< endl;
} else {
if (C<=0) {
cout<< “Water is in a solid state.”<< endl;
} else {
cout<< “Water is in a liquid state.”<< endl;
}

}
return 0;
}

And this how it looks once the program runs:

screen-shot-2017-01-26-at-10-37-39-am


I did it!

--Originally published at my programming blog

When the teacher explained us the homework WSQ01 I was scared, I didn’t even know how to run the “Hello World” program, but right now I’m really proud of myself! It wasn’t as hard as I thought it would be, I read all chapter 2 of the book and it explains very well, also I read a post in the webpage cplusplus.com that also helped me. So thanks to this I understood how to do the program!

screen-shot-2017-01-18-at-5-01-45-pm

So what I did was:

  1. I typed the basic things like” #include <iostream>”, “using namespace std;” and “int main ()”
  2. Then I put an integer with the values of a and b
  3. Then I typed “cout<< ” First integer: “<<endl;
  4. Then typed “cin>> a; ” (“cin” means that you insert the value you want)
  5. The I typed “cout<< “Second integer: “<< endl;
  6. Again I typed “cin>> b;” ( but this time I entered the b value)
  7. And then I just typed “cout<< “Sum of the two numbers: ” << a+b << endl; (after the string I put operation I wanted which was the sum a+b)
  8. And I did the same with the different operations (difference, division and the remainder)

screen-shot-2017-01-18-at-5-00-09-pm

Here is my program:

#include <iostream>
using namespace std;
int main()
{
int a, b;
cout << “First integer: ” << endl;
cin >> a;
cout << “Second integer: ” << endl;
cin >> b;
cout << “Sum of the two numbers: ” << a+b << endl;
cout << “Difference of the two numbers: ” << a-b<< endl;
cout << “Integer based division of the two numbers: ” << a/b << endl;
cout << “Remainder of the integer division of the two numbers: ” << a%b << endl;
return 0;
}

Thanks for reading!