Thursday, 31 October 2013

Program to find factorial of a number using Recursion

Recursion is one of the most awesome properties in the world of programming. It is used in many cases, but the most basic use of recursion is to find the factorial of a given number. There are many other ways to find the factorial, but the one which uses Recursion is the easiest. Recursion is the property in which a function calls itself inside its own body. This may be a hard to understand till you read the example.

Facorial : Consider an integer n. Factorial of n is denoted as n! and is found out by multiplying all numbers from 1 to n.
e.g.,
      1! = 1
      3! = 3 x 2 x 1  = 6
      5! = 5 x 4 x 3 x 2 x 1 = 120
    10! = 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 3628800

Here is the code :
#include<iostream.h>
long Factorial(long a)
{
   if(a>1)
   {
       return (a*Factorial(a-1));
   }
   else
   {
       return 1;
   }
}
int main()
{
   long number;
   cout<<"Enter the number : ";
   cin>>number;
   cout<<endl<<number<<"! = "<<Factorial(number)<<endl;
   return 0;
}

For newer IDEs, visit this page.
Working

  • Suppose the input number is 1. The program outputs 1! = 1 since we have written an else statement specifically when the input is 1. 
  • Suppose the input number is 4. Upon function calling, the variable in which 4 is stored, number, is changed to the variable we have in the function definition, i.e., a. Since 4>1, the if statement is activated.
    The function returns 4 x (Factorial(4-1)), which is equal to 4 x (Factorial (3)) which is equal to 4 x Factorial ( 3 x Factorial (2)) and so on. This continues till 4 becomes decremented to 1. So the final expression is 4 x 3 x 2 x 1. Hence the output is 4! = 24. Since the function Factorial is again called inside the function Factorial, the function is said to undergo Recursion.

FUN FACT : If you search 'Recursion' on Google, it will say
Did you mean: Recursion?
(Try it out for yourself!) If you understand the joke, Kudos, because, you have understood recursion!

Wednesday, 30 October 2013

C++ Program to check whether an year is 'Leap Year or Not'


In this program we will check whether an input year is a leap year or not!

The basic rules a leap year should follow are:

  • A year is a leap year if it is divisible by 400.
    e.g., 1600, 2400 etc., ARE leap years while 1700 or 1100 ARE NOT!
  • If an year is neither divisible by 400 nor by 100, then it IS a leap year, iff it is divisible by 4.
    e.g., 2004, 2016, 1996 etc., ARE leap years while 2007 or 1999 ARE NOT!
Here is the code:
/*Support us by subscribing, commenting, and viewing the 
blog frequently! - GeekyCircle Admin :)*/
#include<iostream.h>
#include<conio.h>
void main()
{
    int year;
    cout<<"Enter the year you want to check : ";
    cin>>year;
    if(((year%4==0)&&(year%100!=0))||(year%400==0))
          cout<<"\nYes! It cerainly looks like a leap year!";
    else
          cout<<"\nMhmmm... Nah... I don't think it is a leap year.";
getch();
}
For newer IDEs (like Dev C++) :
/*Support us by subscribing, commenting, and viewing the 
blog frequently! - GeekyCircle Admin :)*/
#include<iostream>
using namespace std;
int main()
{
    int year;
    cout<<"Enter the year you want to check : ";
    cin>>year;
    if(((year%4==0)&&(year%100!=0))||(year%400==0))
        cout<<"\nYes! It cerainly looks like a leap year!";
    else
        cout<<"\nMhmmm... Nah... I don't think it is a leap year.";
 return 0;
 }
Working

This program is really simple. First we check whether the given year is divisible by 4 and not by 100. If it is, then we will output "it is a leap year". OR we check if it is divisible by 400, and if it is, we will output "it is a leap year. If both the above conditions are false, we output the "year is not a leap year".

Tuesday, 29 October 2013

C++ Program - Conversion from anything to anything!

Here is the source code for a program to convert anything to anything, like Dollar to Euros and viceversa, or maybe Kelvin temperature to Celsius temperature!
What you need is :

  • Correct formula to convert between quantities :
        e.g., To convert from Celsius to Fahrenheit, you must know that F = (9/5 * C) + 32
Here is the program source code :
/*Support us by subscribing, commenting, and viewing the 
blog frequently! - GeekyCircle Admin :)*/
please share our blog to your friends to support us */
#include<iostream.h>
#include<conio.h>

void main()
{
    int op_1,op_2,op_3,op_4;
    float lbs,kg,faren, cel, dol, eur;

    cout<<"Choose any :"<<endl;
    cout<<"1. Kilogram and pound"<<endl;
    cout<<"2. Farenheit and celcius"<<endl;
    cout<<"3. Dollars to Euros (Current exchange rates applicable)"<<endl;

    cin>>op_1;

    switch(op_1)
    {
        case 1:
           cout<<"Press 1 for Kilogram to pound and 2 for pound to kilogram"<<endl;
           cin>>op_2;
       switch(op_2)
       {
           case 1:
               cout<<"Enter the weight in kg"<<endl;
               cin>>kg;
               lbs = 2.204*kg;
               cout<<kg<<" kg = "<<lbs<<" pounds"<<endl;
               break;
           case 2:
               cout<<"Enter weight in pounds"<<endl;
               cin>>lbs;
               kg = .45*lbs;
               cout<<lbs<<" pounds = "<<kg<<" kg"<<endl;
               break;
        }
        break;
        case 2:
           cout<<"Press 1 for Farenheit to celsius and 2 for Celsius to farenheit"<<endl;
           cin>>op_3;
           break;
       switch(op_3)
       {
           case 1:
              cout<<"Enter the temperature in farenheit"<<endl;
              cin>>faren;
              cel = .5555*(faren - 32.0);
              cout<<faren<<" F = "<<cel<<" C"<<endl;
              break;
           case 2:
              cout<<"Enter the temperature in celcius"<<endl;
              cin>>cel;
              faren = ((1.8*cel) + 32.0);
              cout<<cel<<" C = "<<faren<<" F"<<endl;
              break;
       }
      break;
    case 3 :
       cout<<"Press 1 for Dollars to Euros and 2 for Euros to Dollars"<<endl;
       cin>>op_4;
      switch(op_4)
       {
        case 1:
            cout<<"Enter currency in Dollars"<<endl;
            cin>>dol;
            eur = .73 * dol;
            cout<<dol<<" USD = "<<eur<<" EUROS"<<endl;
            break;
        case 2:
            cout<<"Enter currency in Euros"<<endl;
            cin>>eur;
            dol= 1.38 * eur;
            cout<<eur<<" EUROS = "<<dol<<" USD"<<endl;
            break;
       }
      break;
    default :
        cout<<"Invalid choice! Try again!!!"<<endl;
    }
cout<<"Thanks for using the program!!!"<<endl;
}
For newer IDEs (like Dev C++):
/*Support us by subscribing, commenting, and viewing the 
blog frequently! - GeekyCircle Admin :)*/
please share our blog to your friends to support us */
#include<iostream>
using namespace std;
 
int main()
{
    int op_1,op_2,op_3,op_4;
    float lbs,kg,faren, cel, dol, eur;
 
    cout<<"Choose any :"<<endl;
    cout<<"1. Kilogram and pound"<<endl;
    cout<<"2. Farenheit and celcius"<<endl;
    cout<<"3. Dollars to Euros (Current exchange rates applicable)"<<endl;
 
    cin>>op_1;
 
    switch(op_1)
       {
      case 1 :
         cout<<"Press 1 for Kilogram to pound and 2 for pound to kilogram"<<endl;
         cin>>op_2;
     switch(op_2)
       {
         case 1:
            cout<<"Enter the weight in kg"<<endl;
            cin>>kg;
            lbs = 2.204*kg;
            cout<<kg<<" kg = "<<lbs<<" pounds"<<endl;
            break;
         case 2:
            cout<<"Enter weight in pounds"<<endl;
            cin>>lbs;
            kg = .45*lbs;
            cout<<lbs<<" pounds = "<<kg<<" kg"<<endl;
            break;
       }
       break;
    case 2 :
       cout<<"Press 1 for Farenheit to celsius and 2 for Celsius to farenheit"<<endl;
       cin>>op_3;
      switch(op_3)
       {
        case 1:
            cout<<"Enter the temperature in farenheit"<<endl;
            cin>>faren;
            cel = .5555*(faren - 32.0);
            cout<<faren<<" F = "<<cel<<" C"<<endl;
            break;
        case 2:
            cout<<"Enter the temperature in celcius"<<endl;
            cin>>cel;
            faren = ((1.8*cel) + 32.0);
            cout<<cel<<" C = "<<faren<<" F"<<endl;
            break;
       }
       break;
    case 3 :
        cout<<"Press 1 for Dollars to Euros and 2 for Euros to Dollars"<<endl;
        cin>>op_4;
      switch(op_4)
       {
        case 1:
            cout<<"Enter currency in Dollars"<<endl;
            cin>>dol;
            eur = .73 * dol;
            cout<<dol<<" USD = "<<eur<<" EUROS"<<endl;
            break;
        case 2:
            cout<<"Enter currency in Euros"<<endl;
            cin>>eur;
            dol= 1.38 * eur;
            cout<<eur<<" EUROS = "<<dol<<" USD"<<endl;
            break;
       }
       break;
    default :
       cout<<"Invalid choice! Try again!!!"<<endl;
   }
   cout<<"Thanks for using the program!!!"<<endl;
 return 0;
}
Working

There ain't much to say about the working of this program. It looks huge and confusing, but it really is not! The program is a little bit large since I used nested switch statements (i.e., switch inside body of another switch). But if you look carefully, you will understand that the program is really simple and if I had used, if-else-if statements, the program would have become more complex.


P.S : You may add as many conversions as you want to this program! :)


Sunday, 27 October 2013

Program to print out whether a number given is prime or not

This might be a little difficult to understand, so just be with me...
Let us revise some basic math first.

  • A Prime Number is a number which has no divisors other than 1 and that number itself. e.g.,  2, 5,7, 97 etc.
  • A Composite Number is just opposite to a prime number, which has one or more divisors other than 1 and that number itself. e.g., 4, 8, 9, 27.
In this program we are going to input a number from the user and show whether the number is prime or not prime. Here is the code:

/*Support us by subscribing, commenting, and viewing the 
blog frequently! - GeekyCircle Admin :)*/ #include<iostream.h> #include<conio.h> void main() { int num, i, counter=0; cout<<"Enter the number to be processed : "<<endl; cin>>num; for(i=2;i<=num/2;i++) { if(num%i==0) { counter=counter+1; //Or 'counter++;' break; } } if(counter==0) { cout<<"The number is a prime number"<<endl; } else { cout<<"The number is not a prime number"<<endl; } getch(); }

For newer IDEs (like Dev C++) :
/*Support us by subscribing, commenting, and viewing the 
blog frequently! - GeekyCircle Admin :)*/
#include<iostream>
using namespace std;
int main()
{
    int num, i, counter=0;
    cout<<"Enter the number to be processed : "<<endl;
    cin>>num;
    for(i=2;i<=num/2;i++)
    {
       if(num%i==0)
       {
           counter=counter+1; //Or 'counter++;'
           break;
       }
    }
    if(counter==0)
    {
        cout<<"The number is a prime number"<<endl;
    }
    else
    {
        cout<<"The number is not a prime number"<<endl;
    }
 return 0;
}
Working of the program

So basically what happens in the loop is :

  1.  i is initially 2. The loop checks if the remainder of the division num/i is 0. If it is 0, then a counter set at 0 becomes 1. i is incremented one be one to num/2. This is because, we need not check if, any number greater than the half of the input number is the factor of the input number (from basic math). 
  2. If the counter is more than 1 (i.e., at some point, i becomes a perfect divisor of the input number i.e., number is not prime), we output "The number is not prime" and the loop breaks. If the counter is 0 at the end of the loop(i.e., There is no umber that is the perfect divisor of the input number i.e., the number is prime) we output "The number is prime"
Let us consider two examples:

Suppose input number is 8.
  • Initially i is 2. 8%2 is 0. counter is incremented from 0 to 1 and the loop breaks since 8%2 is 0
  • Since counter is 1, output is "The number is prime".
Suppose the input number is 7. 
  • Initially i is at 2. First 7%2 is found out. 7%2 = 1. Loop continues since remainder is not 0.
  • i is incremented by 1. i is now 3. 7%3 is also 1. Loop breaks here since the next integer after 3 is 4 which is greater than half of 7. 
  • At the end counter is still 0. So the output is "The number is prime".

Odd or Even - C++ Program Sourcecode!

Here is the C++ program to find out whether a give number is odd or even :

/*Please support me by commenting and subscribing and viewing 
my blog frequently! - GeekyCircle Admin */
#include<iostream.h>
#include<conio.h>
void main()
{
    int num; cout<<"Enter the number to be processed : "<<endl;
    cin>>num;
    if(num%2==0)
    {
       cout<<"The number is even!";
    }
    else if(num%2==1)
    {
       cout<<"The number is odd!";
    }
getch();
}
For newer IDEs (like Dev C++) :
/*Please support me by commenting and subscribing and viewing 
my blog frequently! - GeekyCircle Admin */
#include<iostream>
using namespace std;
int main()
{
   int num; cout<<"Enter the number to be processed : "<<endl;
   cin>>num;
   if(num%2==0)
   {
       cout<<"The number is even!";
   }
   else if(num%2==1)
   {
       cout<<"The number is odd!";
   }
return 0;
}
Here is the video tutorial of this program :

The program works on the simple logic that the remainder of an even number when divided by 2 is 0 and that of an odd number is 1. 




You might also like