Increasing and decreasing orders

Hello! Today I am going to show you how to read n numbers from the keyboard and rearrange them in an increasing or decreasing order.I hope this  helps you! Enjoy!

So,in order to do that you will need to work with arrays of numbers. Example code:

#include <iostream>

using namespace std;
int n,V[101],ok,i,aux;
int main()
{

    cin>>n;
    for (i=1;i<=n;i++)
        cin>>V[i];
    do
    {
        ok=0;
        for(i=1;i<=n-1;i++)
       {
           if (V[i]>V[i+1])
        {
            aux= V[i];
            V[i]= V[i+1];
            V[i+1]= aux;
            ok = 1;
        }
       }
    }

    while (ok==1);
    for(i=1;i<=n;i++)
    cout<<V[i]<<" ";

    return 0;
}
/*this code rearranges the numbers in an increasing order, if you want to
rearrange them in a decreasing order you have to change the if condition from
V[i]>V[i+1] to V[i]<V[i+1] */

 

Feel free to try the code yourselves, soon I will talk about reading the values from a file in case you have some values that are already generated.

Keep on coding 😉 !

Advertisements

Check if a number is palindrome

Hello! Today I am going to show you how to determine whether a number is a palindrome or not using a basic algorithm.

First of all, what is a palindrome number?

A palindrome number is a number that reads the same backwards as forward.

Now the code :

#include <iostream>

using namespace std;

int main()
{
   int num, reverse_num=0, remainder,temp;
   cout<<"Enter an integer : ";
   cin>>num;

   /* Here we are generating a new number (reverse_num)
    * by reversing the digits of original input number
    */
   temp=num;
   while(temp!=0)
   {
      remainder=temp%10;
      reverse_num=reverse_num*10+remainder;
      temp/=10;
   }

   /* If the original input number (num) is equal to
    * to its reverse (reverse_num) then its palindrome
    * else it is not.
    */
   if(reverse_num==num)
      cout<<num<<" is a palindrome number"<<endl;
   else
      cout<<num<<" is not a palindrome number"<<endl;
   return 0;
}

And this is how you check whether a number is a palindrome or not, I hope this helped you!

Keep on coding ;)!

The greatest common divisor and the least common multiple

Hello! Today I am going to show you another basic algorithm. one that finds out the greatest common divisor and the least common multiple of two numbers using repetitive subtractions. Enjoy!

#include 

using namespace std;

int main()
{
    int a,b,auxa,auxb, m;
    cout<<"a=";cin>>a;
    cout<<"b=";cin>>b;
    auxa=a;
    auxb=b;
    
    while (a!=b)
    {
      if (a>b)
        a=a-b;
      else
        b=b-a;
      }
    m=auxa*auxb/a;
    cout<<"The greatest common divisor is "<<a<<endl;
    cout<<"The least common multiple "<<m<<endl;
    return 0;
}

I hope this helped you! If you have any questions feel free to ask me!

Keep on coding 😉

Compare two numbers

Hello! Today I am going to show you the algorithm for comparing two numbers and printing the result inside the console, it is fairly easy so there shouldn’t be any problems.

#include <iostream>

using namespace std;
int a,b; // declaring the variables
int main()
{
    cout << "a= ";
     cin >>a; //reading the input from the console
    cout << "b= ";
     cin >>b;  //reading the input from the console 
     //comparing the two numbers
     if(a>b)
        cout<<a<<" is greater than "<<b;
     else if(a<b)
        cout<<b<<" is greater than "<<a;
     else if (a==b)
        cout<<"The introduced numbers are equal"<<'\n';
    
    return 0;
}

I hope this helped you ,you can try it yourselves inside code::blocks !If you haven’t seen the post in which I am explaining how to create a console application feel free check it out here.

Keep on coding 😉 !