Category Archives: Conditionals & loops

Homework 8: Yo soy 196

We are going to calculate the quantity of Lychrel numbers (natural numbers that doesn’t convert on palindromes after a serie of aditions, for example, 196 is a Lychrel number), natural and made palindromes that are present in a range of integer numbers given by the user.

If we want to convert a non-natural palindrome to a palindrome we have to sum its reverse until we obtain the palindrome.

Using this logic, we can declare some variables that acumulate the reverse and suspect numbers, and evaluate a significant number of times until either do we find the palindrome or do we determine that the given number is Lychrel.

To make all this process I utilized some while loops where I flip the numer and then compare the resulting number with the original one.

You can see my code here…

// El programa usa Big Integer para almacenar más datos. Esta es una librería creada por el maestro.
#include <iostream>
//#include <math.h>
#include <string>
#include "BigIntegerLibrary.hh"
using namespace std;

BigInteger reverse(BigInteger N)
{
  BigInteger M=0, n=N;
  int d=0;

  while(n!=0)
  {
    d++;
    n/=10;
  }

  for (;d>0; d--)
  {
    BigInteger D=1;
    for(int c=0; c<d-1; c++)
      {D*=10;}
    M+=(N%10)*D;
    N/=10;
  }
  return M;
}

int main()
{
  BigInteger n, N, NatPal=0, MadePal=0, Lych=0, m, l;
  string s, S;

cout << "\nLower bound of the sequence: ";
  cin >> s;
  cout << "Upper bound of the sequence: ";
  cin >> S;
  n = stringToBigInteger(s);
  l = n;
  N = stringToBigInteger(S);

  for (; n<=N ; n++)
  {
    BigInteger c=0;
    m=n;
    if (n == reverse(n))
    {NatPal++;}
    else
    {
      do {
        m+=reverse(m);
        c++;
      } while(m!=reverse(m) && c!=30);

      if (c==30)
      {Lych++;}
      else
      {MadePal++;}
    }
  }

  cout << "\nWe are calculating how many natural and made palindromes, as well as Lycherel numbers, there are in the range given...\n\n"<< endl;
  cout << "The results for the range "<< l << " to " << N << " are:\n"<< endl;
  cout << "Natural palindromes: " << NatPal << endl;
  cout << "Made palindromes (Non-Lycherel numbers): " << MadePal << endl;
  cout << "Lycherel candidates: " << Lych << "\n\n";
  return 0;
}

 

 

 

 

Homework 7: Lists

This program will ask the user for 10 numbers and will be stored in a vector. Then, the user will be able to see the total of the sum, average and standard deviation of those numbers.

A vector is very easy to use, you have to asign a type to the vector and give it a dimension. Something like this “type Vector[dimension]”. To asign a value inside the vector, you give the number of the row, e.g. “Vector[2] = Example”. You must begin from cero (first row) and end in “dimension-1” (last row). Now with for’s we can access to each row one by one.

Knowing this, we can make the program easily. This is my code…

#include <iostream>
#include <math.h>
using namespace std;

int main()
{
  float numbers[10];
  float X;
  float SUM=0, AVG, SD, VAR=0;
  cout << "\n\n";

  for (int c=0; c<10; c++)
  {
    cout << "Number " << c+1 << "= ";
    cin >> X;
    numbers[c] = X;
    SUM += numbers[c];
  }

  AVG = SUM/10;

  for (int c=0; c<10 ; c++)
  {
    VAR+= pow ((numbers[c] - AVG), 2);
  }
  SD = sqrt(VAR/10);

  cout << "\n\nSum = " << SUM << endl;
  cout << "Average = " << AVG << endl;
  cout << "Standard deviation = " << SD << endl;

  return 0;
}

 

 

Homework 6: Factorial Calculator

Let’s make a factorial calculator!

First of all, we have to know what a factorial number is. If you see a number that is accompanied by an exclamation sign, you’re seing a factorial number, that consists in a number multiplied by all the precedent integer numbers until reach one. For example, 6!= 65432*1 = 720.

Knowing that, we can proceed to elaborate the code. We are going to create a function, of type “long double”, which is in charge of calculating factorial numbers.

You can see what I did in the following code.

// Using long double to obtain big values of factorial numbers

#include <iostream>
#include <math.h>
using namespace std;

long double fact (long double n)
{
  long double R=n;
  if (n==0)
    {R=1;}
  for (n--; n>0; n--){
    {R*=n;}
  }
  return R;
}

int main()
{
  long double N;
  long double n;
  char C='Y';

  do
  {
    if (C!='Y' && C!= 'y')
      {cout << "\n\nInvalid choise! \nTry again"<< endl;}
    else
    {
      cout << "\nNon-negative integer: ";
      cin >> N;
      n = floor(N);

      if (N<0)
        {cout << "\nMATH ERROR! \nPlease, introduce a NON-NEGATIVE number\n\n" << endl;}
      else if (N!=0 && (N/n)!=1)
        {cout << "\nMATH ERROR!\nPlease introduce an INTEGER number!\n\n";}
      else
        {cout << "\n" << N << "! = " << fact(N) << "\n\n";}
    }

    cout << "\nDo you want to introduce another number?\n(Yes = Y / No = N)\n\n";
    cin >> C;

  } while (C!='N' && C!='n');

  cout << "\n\nHave a nice day!!\n\n\n";
  return 0;
}

 

 

 

 

Homework 4: Sum of Numbers

This program sums a range of consecutive numbers, between a given range by the user. It’s very simple to make, I will show you step by step how to make it.

  1. As usual, you need to call the necessary libraries and open the main function.
  2. We have to declare two variables for the bounds, one for the sum,  and another one that keeps the value of the lower bound. The variable for sum must be inicialized in zero.
  3. Then, we ask the user for the bounds and validate if the lower bound is less than the upper one. If it is, we proceed to sum number by number, increasing the lower bound value one by one, this with a while loop, that will work until the variable ‘L’ is the same as ‘U’ (upper bound).
  4. Finally, we print on the screen the value of the sum made.

 

Here is the code…

#include <iostream>
using namespace std;

int main()
{
  int L,U,l,S=0;

  cout << "I'll give you the sum of a rage of consecutive integer numbers, you only have to indicate the interval\nWhich is the lower bound? ";
  cin >> L;
  cout << "Which is the upper bound? ";
  cin >> U;
  l=L;

  while (L>U)
  {
    cout << "You entered the numbers in the wrong order.\nPlease do it rightly.\n\nLower bound: ";
    cin >> L;
    cout << "Upper bound: ";
    cin >> U;
  }

  while (L<=U)
  {
    S+=L;
    L++;
  }


  cout << "The sum of the consecutive integer numbers between " << l << " and " << U << " is equal to: " << S << endl;
  return 0;
}