Monday, March 17, 2014

[Tutorial C++] Basics of Programming Part 3

This is the third part in my basics of programming series. In this part I shall tell about arrays, how to declare, initialize and use them. Being a beginner your first question would be "What are arrays?". Well the answer for this is quite easy, arrays are like tables where you can store information for a particular object like names, address, phone number, player stats etc. If we had used variables here to store the data then obviously we had to make many variables and keeping a track of them would have been difficult. Arrays makes that really easy as everything is stored under a single name (with a small change which I will show later)  and accessing them becomes also easy.


So how do we declare an array. Well that's easy too. To declare an array first write its type, then the name you would like to give the array and then the number of data it needs to store but it should be written inside the square brackets. Like I want to store some marks which will be out 10 of students in a class, so I will first write its type that is going to be "int" and then I write the name I would like to give the array which is "Marks" and then I will write the number of data I am going to store, I want to store the marks of five students so I will write "5" but not like this we need to include them in square brackets so it should be like this "[5]" and then a semi-colon to end the statement. The final result would like this.

int Marks[5];
 
 
Now that we have done the declaration part we shall go on how to initialize an array and store data in it. Every data we store on array is known at its element. So if we store the first data then it is the first element of the array and when we store the second data it is the second element of the array and that continues till the last element in the array. In the array we have declared we have told that C++ that we want to store the marks of five students so we have declared an array of five elements. There are two ways we can store data an array one is that we store the data at the beginning and the other way is to store the data after declaration we can also use this way to edit the data later in the program. Below I will show you both methods.
 
//Method#1

int Marks[5]={5, 6, 9 ,3, 7};

//Method#2

int Marks[5];

Marks[0]=5;
Marks[1]=6
Marks[2]=9
Marks[3]=3
Marks[4]=7
Now here's the confusing part. You would have seen me starting the elements from zero rather than one. Well in C++ you start the first element of array from zero not one and then count to five so that the fifth element is four. So whenever you are going to refer to first element its going to be zero not one. Okay so let's move on to make a program where we shall input the marks of the students, number of students and then find the total and average marks.

#include<iostream> // Declaring header file

#include<conio.h>  // Declaring header file


using namespace std;


int main() //main function

{

int marks[5], sum, numofstd; //Declaring an array and then two variables

float average; /*This is a new thing, here I am using a new type to declare a variable as average mostly comes in decimal points so if we have a decimal number we have to use float type rather than the integer type.*/


cout<<"Enter the number of students:"<<endl;//Displaying a message and using endl to end the line

cin>>numofstd;  //taking input from the user


cout<<"Enter the marks of the students:"<<endl;//Displaying a message and using endl to end the line

cin>>marks[0];  //taking input from the user

cin>>marks[1];  //taking input from the user

cin>>marks[2];  //taking input from the user

cin>>marks[3];  //taking input from the user

cin>>marks[4];  //taking input from the user


sum=marks[0]+marks[1]+marks[2]+marks[3]+marks[4];  //calculating sum here


average=(float)sum/numofstd;  /*calulating average here, here I am using a new 
technique called casting. We use this because our variable "sum" was declared in "int" 
form but our "average" variable is in float form and if we want to get the average in 
float form than the variable given to it for finding average should also be in float 
form so to get that we use casting that is place two round brackets before the 
variable whose type you want to change and inside the the brackets write the type you 
want for it. In our case it is "float".*/


cout<<"Sum="<<sum<<endl; //Displaying sum

cout<<"Average="<<average<<endl; //Displaying average

getch();

}

In this program I have used two new things that is a float type variable and using the technique of casting. I hope you got what I tried to taught you and like always if you have any questions please do ask and I will try to anwser them asap. Here's how the program finally looks like when executed.
 
 

Simple + Scientific Calculator





So here comes a simple plus scientific calculator. Code is a little long but interesting and easy to understand. Calculations done using only loops.
For suggestions, feel free to comment below or contact us on our Facebook fan page.                                                                                                                                                                                                                                                                                                              

#include <iostream.h>

 #include <conio.h>                                   //header files used

#include <math.h>



void main_menu();

 void simple_cal();

  void scientific_cal();

   void addition();

    void subtraction();

     void multiplication();

    void division();

   void average();

  void percentage();

 void power();                                      //Prototypes of functions used

void root();

 void sine();

  void cosine();

   void tangent();

    void cosecant();

     void secant();

      void cotangent();

       void log_nat();

        void log_com();



void main()                                      //main function

{

   main_menu();                                     //calling main menu function

}





  void main_menu()                                 //function for main menu

  {

   int n;

   cout<< "\n\n-Enter 1 if you want to use a Simple Calculator.\n";

   cout<< "-Enter 2 if you want to use a Scientific Calculator.\n\n";

   cin>>n;

   if (n==1)

   simple_cal();                              //calling simple calculator function

   else if (n==2)

   scientific_cal();                          //calling scientific calculator function

  }



  void simple_cal()                           //function for simple calculator

  {

    int a;

    clrscr();

     cout<< "\n\t\t\t**** SIMPLE CALCULATOR ****\n";

      cout<< "\tAll rights reserved by the programmer MUHAMMAD TALHA YOUNUS..";

       cout<< "\n\n-Enter 1 for addition.\n";

        cout<< "-Enter 2 for subtraction.\n";

         cout<< "-Enter 3 for multiplication.\n";

          cout<< "-Enter 4 for division.\n";

         cout<< "-Enter 5 for average.\n";                                                   //menu of simple calcuator

        cout<< "-Enter 6 for Percentage.\n";

       cout<< "-Enter 7 for the power.\n";

      cout<< "-Enter 8 for root.\n\n";

     cin>>a;

   if (a==1)

            addition();                     //calling addition function

   else if (a==2)

            subtraction();                  //calling subtraction function

   else if (a==3)

            multiplication();               //calling multiplication function

   else if (a==4)

            division();                     //calling division function

   else if (a==5)

            average();                      //calling average function

   else if (a==6)

            percentage();                   //calling percentage function

   else if (a==7)

            power();                        //calling power function

   else if (a==8)

            root();                         //calling root function

}







  void scientific_cal()                              //funtion for scientific calculator

  {

       int a;

       clrscr();

         cout<< "\n\t\t\t**** SCIENTIFIC CALCULATOR ****\n";

          cout<< "\tAll rights reserved by the programmer MUHAMMAD TALHA YOUNUS..";

           cout<< "\n\n-Enter 1 if you want to find value of Sine Function of an angle.\n";

            cout<< "-Enter 2 if you want to find value of Cosine Function of an angle.\n";

             cout<< "-Enter 3 if you want to find value of Tangent Function of an angle.\n";

              cout<< "-Enter 4 if you want to find value of Cosecant Function of an angle.\n";              //menu of scientific calculator

             cout<< "-Enter 5 if you want to find value of Secant Function of an angle.\n";

            cout<< "-Enter 6 if you want to find value of Cotangent Function of an angle.\n";

           cout<< "-Enter 7 if you want to find value of Natural Logarithm of a number.\n";

          cout<< "-Enter 8 if you want to find value of Common Logarithm of a number.\n\n";

         cin>>a;

  if (a==1)

          sine();                              //calling sine function

  else if (a==2)

          cosine();                            //calling cosine function

  else if (a==3)

          tangent();                           //calling tangent function

  else if (a==4)

          cosecant();                          //calling cosecant function

  else if (a==5)

          secant();                            //calling secant function

  else if (a==6)

          cotangent();                         //calling cotangent function

  else if (a==7)

          log_nat();                           //calling natural log function

  else if (a==8)

          log_com();                           //calling common log function

 }





    void addition()                                              //function for addition

    {

       float a,sum=0.0;

        clrscr();

         cout<< "\n\t\t\t\t*** ADDITION ***\n";

          cout<< "\nEnter the numbers to be added.\n\n\t==>Enter 0 to complete the adddition.\n\n";

           cin>>a;

            while(a!=0)

            {

             sum=sum+a;

             cin>>a;

            }

          cout<< "The sum of given numbers is:\t"<<sum<<endl<<endl;

         getch();

    }





      void subtraction()                                        //function for subtraction

      {

        float a,difference=0.0;

         clrscr();

          cout<< "\n\t\t\t\t*** SUBTRACTION ***\n";

           cout<< "\nEnter the numbers.\n\n\t==>Enter 0 if you want to complete the subtraction.\n\n";

        cin>>a;

       while(a!=0)

         {

          difference=difference-a;

           cin>>a;

          a=-1*a;

         }

        difference=-1*difference;

       cout<< "The difference of given numbers is:\t"<<difference<<endl<<endl;

       getch();

      }





           void multiplication()                              //function for multiplication

           {

             float a,product=1.0;

              clrscr();

               cout<< "\n\t\t\t\t*** MULTIPLICATION ***\n";

                cout<< "\nEnter the numbers to be multiplied.\n\n\t==>Enter 1 if you want to complete the multiplication.\n\n";

             cin>>a;

                  while(a!=1)

                  {

                   product=product*a;

                   cin>>a;

              }

                 cout<< "The product of given numbers is:\t"<<product<<endl<<endl;

                getch();

           }





              void division()                              //function for division

                {

                  float a,b,division;

                   clrscr();

                    cout<< "\n\t\t\t\t*** DIVISION ***\n";

                     cout<< "\nEnter the DIVIDENT.\n\n";

                cin>>a;

                 cout<< "Enter the DIVISOR.\n\n";

                cin>>b;

                     division=a/b;

                    cout<< "The result is:\t"<<division<<endl<<endl;

                   getch();

                }





                 void average()                              //function for average

                     {

                      float a,sum=0.0,average;

                       int i=0;

                        clrscr();

                         cout<< "\n\t\t\t\t*** AVERAGE ***\n";

                  cout<< "\nEnter the numbers.\n\n\t==>Enter 0 if you want to complete the calculation.\n\n";

                   cin>>a;

                           while(a!=0)

                           {

                            i++;

                             sum=sum+a;

                            cin>>a;

                           }

                          average=sum/i;

                 cout<< "The average of given numbers is:\t"<<average<<endl<<endl;

                        getch();

                      }





                    void percentage()                      //function for percentage

                          {

                            float a,b,percentage;

                             clrscr();

                              cout<< "\n\t\t\t\t*** PERCENTAGE ***\n";

                             cout<< "\nEnter the total.\n\n";

                                cin>>a;

                                cout<< "Enter the number whose percentage value is to be found.\n\n";

                               cin>>b;

                              percentage=(b/a)*100;

                             cout<< "The percentage is:\t"<<percentage<<"%"<<endl<<endl;

                            getch();

                          }





                               void power()                          //function for power

                               {

                                float a,b,power;

                                 clrscr();

                                  cout<< "\n\t\t\t\t*** POWER ***\n";

                                   cout<< "\nEnter the base.\n\n";

                                    cin>>a;

                                    cout<< "Enter the power.\n\n";

                                   cin>>b;

                                  power=pow (a,b);

                                 cout<< "The entered power of the given number is:\t"<<power<<endl<<endl;

                                getch();

                               }





                                    void root()                        //function for root

                                    {

                                      float a,b,root;

                                       clrscr();

                                        cout<< "\n\t\t\t\t*** ROOT ***\n";

                                         cout<< "\nEnter the number whose root is to be calculated.\n\n";

                                          cin>>a;

                                           cout<< "Enter the root.\n\n";

                                          cin>>b;

                                         b=1/b;

                                        root=pow (a,b);

                                       cout<< "The entered root of the given number is:\t"<<root<<endl<<endl;

                                      getch();

                                    }





           void sine()                              //function for sine

           {

            int n;

             float a,sine_d,sine_r,PI=3.141592654;

              clrscr();

               cout<< "\n\t\t\t\t*** SINE VALUE ***\n";

              cout<< "\nEnter 1 if angle is in degrees.\n";

             cout<< "Enter 2 if angle is in radians.\n\n";

            cin>>n;

                 if (n==1)

                 {

                  clrscr();

                   cout<<"\n\t\t*** SINE VALUE WHEN ANGLE IS IN DEGREES ***\n";

                    cout<<"\nEnter the Angle.\n\n";

                   cin>>a;

                  sine_d=sin(a*PI/180);

                 cout<<"The value of SINE Function of given angle is:\t"<<sine_d<<endl<<endl;

                 }



                   else if (n==2)

                    {

                       clrscr();

                        cout<<"\n\t\t*** SINE VALUE WHEN ANGLE IS IN RADIANS ***\n";

                         cout<<"\nEnter the Angle.\n\n";

                        cin>>a;

                       sine_r=sin(a);

                      cout<<"The value of SINE Function of given angle is:\t"<<sine_r<<endl<<endl;

                    }

                 getch();

                 }





                    void cosine()                     //function foe cosine

                    {

                       int n;

                        float a,cosine_d,cosine_r,PI=3.141592654;

                         clrscr();

                          cout<<"\n\t\t\t\t*** COSINE VALUE ***\n";

                           cout<<"\nEnter 1 if angle is in degrees.\n";

                          cout<<"Enter 2 if angle is in radians.\n\n";

                         cin>>n;

                             if (n==1)

                             {

                              clrscr();

                               cout<<"\n\t\t*** COSINE VALUE WHEN ANGLE IS IN DEGREES ***\n";

                                cout<<"\nEnter the Angle.\n\n";

                                 cin>>a;

                                cosine_d=cos(a*PI/180);

                               cout<<"The Value of COSINE Function of given angle is:\t"<<cosine_d<<endl<<endl;

                             }



                               else if (n==2)

                               {

                                clrscr();

                                 cout<<"\n\t\t*** COSINE VALUE WHEN ANGLE IS IN RADIANS ***\n";

                                  cout<<"\nEnter the Angle.\n\n";

                                   cin>>a;

                                  cosine_r=cos(a);

                                 cout<<"The value of COSINE Function of given angle is:\t"<<cosine_r<<endl<<endl;

                               }

                        getch();

                        }





                               void tangent()                    //function for tangent

                               {

                                int n;

                                 float a,tangent_d,tangent_r,PI=3.141592654;

                                  clrscr();

                                    cout<<"\n\t\t\t\t*** TANGENT VALUE ***\n";

                                    cout<<"\nEnter 1 if angle is in degrees.\n";

                                  cout<<"Enter 2 if angle is in radians.\n\n";

                                 cin>>n;

                                       if (n==1)

                                       {

                                        clrscr();

                                         cout<<"\n\t\t*** TANGENT VALUE WHEN ANGLE IS IN DEGREES ***\n";

                                          cout<<"\nEnter the Angle.\n\n";

                                           cin>>a;

                                          tangent_d=tan(a*PI/180);

                                         cout<<"The Value of TANGENT Function of given angle is:\t"<<tangent_d<<endl<<endl;

                                       }

                                         else if (n==2)

                                            {

                                             clrscr();

                                              cout<<"\n\t\t*** TANGENT VALUE WHEN ANGLE IS IN RADIANS ***\n";

                                               cout<<"\nEnter the Angle.\n\n";

                                               cin>>a;

                                              tangent_r=tan(a);

                                             cout<<"The value of TANGENT Function of given angle is:\t"<<tangent_r<<endl<<endl;

                                            }

                                  getch();

                                  }





                                              void cosecant()                    //function for cosecant

                                              {

                                               int n;

                                                float a,cosecant_d,cosecant_r,PI=3.141592654;

                                                 clrscr();

                                                  cout<<"\n\t\t\t\t*** COSECANT VALUE ***\n";

                                                   cout<<"\nEnter 1 if angle is in degrees.\n";

                                                  cout<<"Enter 2 if angle is in radians.\n\n";

                                                 cin>>n;

                                                      if (n==1)

                                                      {

                                                       clrscr();

                                                        cout<<"\n\t\t*** COSECANT VALUE WHEN ANGLE IS IN DEGREES ***\n";

                                                         cout<<"\nEnter the Angle.\n\n";

                                                         cin>>a;

                                                        cosecant_d=1/sin(a*PI/180);

                                                       cout<<"The value of the COSECANT Function of given angle is:\t"<<cosecant_d<<endl<<endl;

                                                      }



                                                        else if (n==2)

                                                           {

                                                            clrscr();

                                                             cout<<"\n\t\t*** COSECANT VALUE WHEN ANGLE IS IN RADIANS ***\n";

                                                              cout<<"\nEnter the Angle.\n\n";

                                                              cin>>a;

                                                             cosecant_r=1/sin(a);

                                                            cout<<"The value of the COSECANT Function of the given angle is:\t"<<cosecant_r<<endl<<endl;

                                                           }

                                                 getch();

                                                 }





                                                        void secant()              //function for secant

                                                        {

                                                         int n;

                                                          float a,secant_d,secant_r,PI=3.141592654;

                                                           clrscr();

                                                            cout<<"\n\t\t\t\t*** SECANT VALUE ***\n";

                                                             cout<<"\nEnter 1 if angle is in degrees.\n";

                                                            cout<<"Enter 2 if angle is in radians.\n\n";

                                                           cin>>n;

                                                                if (n==1)

                                                                {

                                                                 clrscr();

                                                                  cout<<"\n\t\t*** SECANT VALUE WHEN ANGLE IS IN DEGREES ***\n";

                                                                   cout<<"\nEnter the Angle.\n\n";

                                                                   cin>>a;

                                                                  secant_d=1/cos(a*PI/180);

                                                                 cout<<"The value of the SECANT Function of given angle is:\t"<<secant_d<<endl<<endl;

                                                                }



                                                                  else if (n==2)

                                                                     {

                                                                      clrscr();

                                                                       cout<<"\n\t\t*** SECANT VALUE WHEN ANGLE IS IN RADIANS ***\n";

                                                                        cout<<"\nEnter the Angle.\n\n";

                                                                        cin>>a;

                                                                       secant_r=1/cos(a);

                                                                      cout<<"The value of the SECANT Function of the given angle is:\t"<<secant_r<<endl<<endl;

                                                                     }

                                                              getch();

                                                                     }







    void cotangent()                              //function for cotangent

    {

     int n;

      float a,cotangent_d,cotangent_r,PI=3.141592654;

       clrscr();

        cout<<"\n\t\t\t\t*** COTANGENT VALUE ***\n";

         cout<<"\nEnter 1 if angle is in degrees.\n";

        cout<<"Enter 2 if angle is in radians.\n\n";

       cin>>n;

            if (n==1)

            {

             clrscr();

              cout<<"\n\t\t*** COTANGENT VALUE WHEN ANGLE IS IN DEGREES ***\n";

               cout<<"\nEnter the Angle.\n\n";

               cin>>a;

              cotangent_d=1/tan(a*PI/180);

             cout<<"The value of the COTANGENT Function of given angle is:\t"<<cotangent_d<<endl<<endl;

            }

              else if (n==2)

                 {

                  clrscr();

                   cout<<"\n\t\t*** COTANGENT VALUE WHEN ANGLE IS IN RADIANS ***\n";

                    cout<<"\nEnter the Angle.\n\n";

                    cin>>a;

                   cotangent_r=1/tan(a);

                  cout<<"The value of the COTANGENT Function of the given angle is:\t"<<cotangent_r<<endl<<endl;

                 }

          getch();

                 }





                   void log_nat()                       //function for natural logarithm

                   {

                     float a,ln;

                      clrscr();

                       cout<<"\n\t\t\t\t*** NATURAL LOGARITHM ***\n";

                        cout<<"\nEnter the number.\n\n";

                        cin>>a;

                       ln=log(a);

                      cout<<"The value of Natural Logarithm of the given number is:\t"<<ln<<endl<<endl;

                     getch();

                   }





                        void log_com()                   //function for common logarithm

                        {

                          float a,log;

                           clrscr();

                            cout<<"\n\t\t\t\t*** COMMON LOGARITHM ***\n";

                             cout<<"\nEnter the number.\n\n";

                             cin>>a;

                            log=log10(a);

                           cout<<"The value of Common Logarithm of the given number is:\t"<<log<<endl<<endl;

                          getch();

                        }



a

Sunday, March 16, 2014

Factors of a Number


Here is a simple program to get the factors of a given number. A for loop (nested in while) is used which divides number with each number from 2 to that number, and it breaks when the remainder is zero. While loop just divides the number with the factor to get new number for next factor and the process goes on till end.                                                                                                                        

#include <iostream.h>

#include <conio.h>

void main()

 {

  int num, fact, rem;

  cout<< "                  *************    Factors     *************\n\n";

  cout<< "Enter the number:  ";

  cin>>num;                                  //entering the number

  cout<< endl<< "Factors are:  ";

  while (num>=1)

   {

    for (fact=2;fact<=num;fact++)

     {

      rem=num%fact;

      if (rem==0)

       {

        cout<< fact<< "  ";                 //here we get the factor when remainder is 0

        break;

       }

     }

    num=num/fact;                          //reducing the number

   }

getch();

 }

Introduction to JAVA Language

INTRODUCTION TO JAVA

Java is a high-level programming language originally developed by Sun Microsystems and released in 1995. Java runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX. 
Java is a general-purpose, concurrent, class-based, object-oriented computer programming language that is specifically designed to have as few implementation dependencies as possible. It is intended to let application developers "write once, run anywhere" (WORA), meaning that code that runs on one platform does not need to be recompiled to run on another.
Java applications are typically compiled to bytecode (class file) that can run on any Java virtual machine (JVM) regardless of computer architecture.
Java is, as of 2012, one of the most popular programming languages in use, particularly for client-server web applications, with a reported 10 million users. The language derives much of its syntax from C and C++, but it has fewer low-level facilities than either of them.

Java Compiler is software that compiles the Java code into machine code. Machine code is a set of commands that the hardware understands. The Java Compiler is open source and is called as the Java Standard Development Kit (SDK). Currently version 1.7 is available online. The SDK must be available before editing or creating a Java program

Java Runtime Environment (JRE) is the virtual machine over which the java code is executed. This virtual machine executes the java code. The JRE must be provided before executing a Java program. 

Java Integrated Development Environment (IDE) is an editor tool complete with Java SDK and JRE and an editing tool that allows a developer to create edit and execute Java programs. Different IDEs are available including Netbeans, Eclipse, Kawa, etc.

To execute Java programs you will need to download the IDE. I follow Netbeans which can be acquired from https://netbeans.org/downloads/. Download the Netbeans with JDK for Windows (32 bit) and install the software. https://netbeans.org/downloads/

Thank You 
Comment About This Blog and Become Our Fan At  CPP Fuzz Facebook 

Decimal to Octal converter (basic way)


Here is a basic way to get the octal value of a decimal number. In this program, a for loop is used which will get the remainder, decrease the number by dividing it by 8 and populate the array. At the end, we will simply print the array in reverse order.                                                                                                                                                                                                                                                                      
 
#include<iostream.h>

#include<conio.h>

void main()

{

int num,rem,div,i=0,a[100];

cout<<"Enter the number :  ";

cin>>num;                       //getting the number

for (div=num/8;div>0;)

{

if (i==0)

rem=num%8;

else

{

rem=div%8;                      //dividing the number by 8 to get remainder and getting new number

div=div/8;

}

a[i]=rem;                      //populating the array with remainder

i++;

}

cout<<endl<<"The Octal Value of given number is :  ";

for (int k=i-1;k>=0;k--)

cout<<a[k];                       //printing the array in reverse order

getch();

}

Decimal to Hexadecimal converter (basic way)


Here is a basic way to get  the hexadecimal value of a decimal number. In this program, a for loop is used which will get the remainder, decrease the number by dividing it by 16 and populate the array. At the end, we will simply print the array in reverse order. For remainder values greater than 9, a function is present which gives alphabetic values of numbers 10-15.                                                                                                                                                             

#include<iostream.h>

#include<conio.h>

void hex(int [], int);

void main()

{

int num,rem,div,i=0,a[100];

cout<<"Enter the number :  ";

cin>>num;                                      //getting the number

for (div=num/16;div>0;)

{

if (i==0)

rem=num%16;

else                                   //dividing number by 16 to get remainder and next number

{

rem=div%16;

div=div/16;

}

a[i]=rem;                             //populating array with the remainder

i++;

}

cout<<endl<<"The Hexadecimal Value of given number is :  ";

for (int k=i-1;k>=0;k--)

{

if (a[k]>9)

hex(a,k);                              //if remainder > 9

else

cout<<a[k];                           //printing array

}

getch();

}

 void hex(int a[], int k)

 {

 if (a[k]==10)

 cout<<'A';

  else if (a[k]==11)

  cout<<'B';

   else if (a[k]==12)

  cout<<'C';

    else if (a[k]==13)                  //Values of remainder > 9

  cout<<'D';

     else if (a[k]==14)

  cout<<'E';

      else if (a[k]==15)

  cout<<'F';

  }


Decimal to Binary converter (basic way)


Here is a basic way to get the binary value of a decimal number. In this program, a for loop is used which will get the remainder, decrease the number by dividing it by 2 and populate the array. At the end, we will simply print the array in reverse order.                                                                                                        
#include<iostream.h>

#include<conio.h>

void main()

{

int num,rem,div,i=0,a[100];

cout<<"Enter the number :  ";

cin>>num;

for (div=num/2;div>0;)

{

if (i==0)

 rem=num%2;

else

{                    //dividing the given number by 2 and getting remainder

rem=div%2;

div=div/2;

}

a[i]=rem;          //transfering remainder to array

i++;

}

cout<<endl<<"The Binary Value of given number is :  ";

for (int k=i-1;k>=0;k--)                      //printing array in reverse order

cout<<a[k];

getch();

}

Prime Number


With the help of this program, you can easily find out whether a given number is prime of not. In this program, we divide the given number from 1 to that number and check the remainder. If it (remainder) becomes zero at any point before reaching that number, the program declares the number as "not prime". Otherwise, if remainder becomes zero at the given number, then the number will be prime. Here 'count' is used as a checker.                                                                                                                                                
#include<iostream.h>

#include<conio.h>

void main ()

{

int n,count=1,rem=1;

cout<<"Enter the number:  ";

 cin>>n;               //entering the number

while (rem!=0)

{

 count++;                //dividing the given the number with each number (from 1 to that num) to chech remainder

 rem=n%count;

}

if (count<n)

 cout<<endl<<"Given number is not prime";

  else if (count==n)         //output

 cout<<endl<<"Given number is prime";

getch ();

}

Saturday, March 15, 2014

[Tutorial] Gimp for beginners Part 3 - Making a meme

How to add text like a meme

Welcome to the third tutorial of the Gimp series. In this tutorial I am going to tell you how to write text like the way you see in memes. I know that many people out there want to learn this so I will cut this intro short and will just tell you how to write the text.

Okay first of all open an image. Mine is the one shown below.




Now select the "Text Tool" from the toolbox. Draw your text box on the image.



After that write done the text for your meme.


 Now go to the layers and right-click on your layer. Select "text to path".


Right-click again and select "New layer".

 
Now a dialog box will open asking for the new layer properties. The new layer should have the same dimensions as the current layer and by default it usually has the same dimensions. So all you need to do is give the layer a name though I think I don't need one but if you want give one. After that in the "layer fill type" property select "Transparency". Now click on "OK".

 
A new layer shall come up in the layers. Click on that and make sure that it is selected. You will know if it is selected when it becomes outlined by a square. Now click on "Select" which is on the task bar at the top. Select "From path".

 
You will see that your text has become outlined.

 
Now click on "Select" again. Click on "Grow".

 
A dialog box will open. In it choose how much you want your text outline to be. I think 2 is fine. After that click on "OK".

 
Now select a color for the outline and use the "Fill" tool from the toolbox to add color to the outline.

 
Now go to layers and drag your text layer above the outline layer. Your text should become visible.

 
Right-click on text layer and select "Merge visible layers".

 
Now again click on "Select" on the task bar at the top. Select "None".

 
Your selection will go away and here's how your is now complete.

 
That's all for this tutorial. See you in the next one.

Ascending Sorting of Array



This Program will help you to sort an array using bubble sorting technique.                                                                                                                                                                                                                                                                                                                                               
#include<iostream.h>
#include<conio.h>
void main()
{
int a[10],b;
cout<<"Enter the array: "<<endl;
 for (int i=0;i<10;i++)
  cin>>a[i];                       //this will populate the array
 for (int i=0;i<10;i++)
 {
  for (int k=i+1;k<10;k++)
  {
   if (a[i]>a[k])
    {
     b=a[k];
     a[k]=a[i];                    //this will compare each element of array with rest
     a[i]=b;
    }
  }
 }
cout<<endl<<"Sorted array is:   ";
for (int i=0;i<10;i++)
 cout<<a[i]<<" ";                          //this will display the array
getch();
}

Friday, March 7, 2014

[Tutorial] Gimp for beginners Part 2 - Make the background transparent or change its color

How to make the background transparent

In this tutorial I will teach you how to make the background of a picture transparent and add color to it. First of all you need to do is open Gimp and open a picutre whose background you want to make transparent. In my case its the same old picture from the previous tutorial.
You can get it from here.

http://www.cppfuzz.com/2013/11/beginners-tutorials-using-gimp.html


Now select the fuzzy select tool from the toolbox.









Now select the background color which we want to make transparent.










Now if you want you can make changes to your selection by choosing adding or subtracting from current selection in tool options.










Now its time to make the background transparent. Select "Layer" from the task bar at the top. Select "transparency" and then select the "Color to Alpha" option.










A dialog box shall open. In this dialog box you have to choose which color you want to convert to transparent in our case its black so select black by clicking on the box which is displaying a color. By default it is white. Press "OK" when you are done.










Your picture shall contain a background which is a combination of grey and balck boxes. Don't worry, this means now that your picture has a transparent background. Now we will be adding a new background color.










Now from the right tool box which will be showing layers right click on it and select the option "New layer" or go to layer in the top task bar and then select new layer.










A dialog box will open which will ask you some properties of the new layer. From the fill type option choose foreground or background color. I chose "Foreground color". This will be used for our new background color. Press "OK" when you are done.










After pressing "OK", your new layer will be displayed. In your layers channel in right tool box, drag your picture above our new layer.










It will be displayed then. Now right-click on your picture (not the new layer) and select "Merge Down". Your layers shall be combined and will become a single picture.

Here's the final result. When we created the new layer you could also have added a picture and then followed the other steps to add a background image to your picture rather then a color. Now go and have fun trying this and making new backgrounds for your picture.

 

Copyright @ 2014 CPP Fuzz.