Thursday, January 31, 2013

Free c++ Codes for projects and how c++ codes works with examples.

Hello guys! a lot of people find difficulty in getting codes for their assignments and projects. There are a lot of tutorials but they do not teach and provide enough examples to give you an understanding. I will provide you with codes of C++ for free and i will try my best to give you an understanding of how they work. You can check my blog again and again because i will be putting more examples and codes for your convenience and help. The C++ codes given below are in ascending order of their difficulty level. You can skip the ones which you think are easy. All the sample C++ codes given below are working, in case you find difficulty in debugging them then you can post comments.
I am using Visual Studio 2008. If you are using any other version then libraries may differ. You can check that on Google. Lets start with our first C++ program.


Example 1:  
Finding area of a circle.

#include<iostream>
#include<conio.h>                               //as i am using getch() so i had to include this library
using namespace std,
void main()
{
const float pi=3.14;                                  //i took const float but you can write it float for you convenience
float r=2.5;                                  
cout<<"area of circle is\n"<<pi*r*r;            //simple formula (area=pi*r ^2) and \n for next line
getch();                                                     //it is used to stop the screen  until user presses enter
}

Example 2:
Take radius as input from the user and then calculate area of a circle.
   
#include<iostream>
#include<conio.h>                              
using namespace std,
void main()
{
const float pi=3.14;                                
float r;
cout<<"enter radius\n";
cin>>r;                                 
cout<<"area of circle is\n"<<pi*r*r;          
getch();                                                    
}

Example 3:
Adding/subtracting/multiplying/dividing two numbers.

#include<iostream>
#include<conio.h>                              
using namespace std,
void main()
{
cout<<"Adding and subtracting and multiplying and dividing two numbers \n";
cout<<5-4<<endl;
cout<<5+4<<endl;
cout<<5*4<<endl;
cout<<5/4<<endl;
getch();                                                    
}

Example 4:
Program to implement power function. Two values are to be taken an input from the user. The first value is base and the second value is exponent and the power is calculated by base times  exponent.

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

int main()
{
    int base,exp,cal=1;
    cout<<"enter base\n";
    cin>>base;
    cout<<"enter exponent\n";
    cin>>exp;
    for(int i=1; i<=exp; i++) 
    cal*=base;
    cout<<"the result is "<<cal;
    getch();
}

Example 5:
The program guesses a number ranging from 1-100. The user is given three hints and then asked to think of the number and enter a number.

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

void main()
{
    int x,flag;
    cout<<"the number is an odd number\n";
    cout<<"the number is divided by 7\n";
    cout<<"the sum of the two digits it greater than 10\n";
flag:
    cout<<"guess the number!\n";
    cin>>x;

    if(x==77)
        cout<<"congrats! u guessed it right! :) \n";
    else
    if(x!=77)
    {cout<<"try again!";
    goto flag;}
   
    getch();
}

Example 6:
This program takes a number as input from the user and then display whether the number is even or odd.

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

void main()
{
    int num;
    cout<<"enter number\n";
    cin>>num;
    if(num%2==0)
    {
        cout<<"the number "<<num<<"is EVEN\n";
    }
    else
        cout<<"the number "<<num<<"is ODD\n";
    getch();


You can also use conditional operator (?:) instead of if/else. The following program uses this operator. You can use it in complex programs to make your coding simple and understandable. 

 Example 7:
The following program checks if the number which is entered by the user is greater than 40 or not. 

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

void main()
{
    int num;
    cout<<"enter number\n";
    cin>>num;
    num>=40 ? cout<<"number is greater": cout<<"number is smaller";
    getch();
}
 


In the above example the operator(?:) checks whether the value stored in num is greater than 40 or not. If it is greater than 40 then the cout statement after ? is printed else statement after : is printed.

 Example 8:
This program demonstrate the working of a simple four function calculator.

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

void main()
{
int num1,num2,result,choice;
cout<<"Welcome to my calculator\n";
cout<<"Enter first operand\n";
cin>>num1;
cout<<"Enter second operand\n";
cin>>num2;
cout<<"enter the operation for the two operands you just entered\n";
cout<<"Enter 1 to add two numbers\n";
cout<<"Enter 2 to subtract two numbers\n";
cout<<"Enter 3 to divide two numbers\n";
cout<<"Enter 4 to multiply two numbers\n";
cout<<"Now Enter your choice number 1-4: ";
cin>>choice;
switch(choice)
{
case 1:
    result=num1+num2;
    cout<<"result of addition is "<<result<<endl;
case 2:
    result=num1-num2;
    cout<<"result of subtraction is "<<result<<endl;
case 3:
    result=num1/num2;
    cout<<"result of division is "<<result<<endl;
case 4:
    result=num1*num2;
    cout<<"result of multiplication is "<<result<<endl;
}
getch();
}


 Example 9:
The program takes year as input from the user and prints if its a leap year or not.

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

void main()
{
    int year;
cout<<"If a year is divisible by 400 or 4 then its a leap year\n";
cout<<"Enter a year to know if its a leap year or not\n";
cin>>year;
if(year%4==0)
    cout<<"The year is a leap year!"<<endl;
else
cout<<"The year is a common year"<<endl;
getch();



 Example 10:
This program asks the user to enter two numbers in the range 0-10000 and then print all the even numbers between them.

#include<conio.h>
using namespace std;

void main()
{
    int num1,num2;
cout<<"Enter numbers between 0-10000\n";
cout<<"Enter first number\n";
cin>>num1;
cout<<"Enter second number\n";
cin>>num2;
for(int i=(num1+1);i<num2;i++)
{
    if(i%2==0)
    {
        cout<<i<<" ";
    }
}
getch();
}