-->

WAP to print hello world without namespace.

//program to print hello world without namespace
#include <iostream>//header file
#include<stdlib.h>//header file for system("cls")

                   //no namespace here!
int main()
{
    system("cls");
    std::cout << "Hello world!" << std::endl;//we have used std to handle cout and endl.If we donot use, It will not work.
    return 0;
}

WAP to add two numbers without using namespace.

//program to add two numbers without namespace
#include <iostream>//header file
#include<stdlib.h>//header file for system("cls")

                   //no namespace here!
int main()
{
    int number1,number2;
    std::cout<<"enter two numbers"<<std::endl;//prints message to input numbers.we must use std.
    std::cin>>number1>>number2;          //gets inputs using chaining concept.we must use std.
    system("cls");                  //clears the screen
    std::cout<<"number1="<<number1<<std::endl<<"number2="<<number2<<std::endl;//we must use std.
    std::cout<<"their sum="<<number1+number2<<std::endl;//we must use std.
    return 0;
}
//note: If we do not use namespace or if we donot use "using namespace std;" before int main() then we must useconds_t
// std before all standard characters/words. Otherwise, It will not work.

WAP to clear the screen using system(cls)

//program to clear the screen using system("cls")
#include <iostream>//header file
#include<stdlib.h>//header file to work with system("cls")
using namespace std;//to handle all standard characters/functions,reserved words  used in program. we do not use in turboc++ compiler

int main()          //main function
{
    system("cls");//clears the screen
    cout << "Hello world!" << endl;//prints the literal
    cout<<"second line "<<endl;//prints the literal
    return 0;               //returns 0
}

WAP to understand conditional operator.

//program to understand conditional/ternary operator
#include <iostream>//header file
#include<stdlib.h>
using namespace std;//to handle all standard characters/functions,reserved words  used in program. we do not use in turboc++ compiler
int main()//main function
{
    int number1,number2;
    cout<<"enter two numbers"<<endl;//prints message to input numbers
    cin>>number1>>number2;          //gets inputs using chaining concept
    system("cls");                  //clears the screen
    cout<<"number1="<<number1<<endl<<"number2="<<number2<<endl;
    number1>number2?cout<<"number1="<<number1<<" is greater than"<<"number2="<<number2<<endl:
    cout<<"number1="<<number1<<" is smaller than"<<"number2="<<number2<<endl;
    //this part tests the condition. If the first part is true then first cout is printed. If not second part is printed.
    return 0;
}

WAP to understand logical operator.

//PROGRAM TO UNDERSTAND following logical operators
/*
and operator        &&
or operator         ||
not operator        !
*/
#include <iostream>//header file
#include<stdlib.h>//header file to handle system("cls")

using namespace std;//to handle all standard characters/functions,reserved words  used in program. we do not use in turboc++ compiler

int main()//main function
{
    int number1,number2;
    cout<<"enter two numbers"<<endl;//prints message to input numbers
    cin>>number1>>number2;          //gets inputs using chaining concept
    system("cls");                  //clears the screen
    cout<<"number1="<<number1<<endl<<"number2="<<number2<<endl;
    cout<<"result of (number1>=20 && number2<=50):-"<<(number1>=20 && number2<=50)<<endl;
    //returns 0 or 1(after testing both conditions) depending upon they are  false or true.Actually, it multiplies both results and returns 0/1.
    cout<<"result of (number1>=20 || number2<=50):-"<<(number1>=20 || number2<=50)<<endl;
    //returns 0 or 1(after testing both conditions) depending upon they are  false or true.
    //Actually, it adds both results and returns 0/1.
    cout<<"result of (!number1>20):-"<<!(number1>20)<<endl;
    //returns 0 or 1 if it is true or false.
    return 0;
}

WAP to understand relational operator.

//program to understand following relational operators
/*
1)greater than                  (>)
2) greater than or equals to    (>=)
3)smaller than                  (<)
4) smaller than or equals to    (<=)
5)equals to                     (==)
6) not equal to                 (!=)
*/
#include <iostream> //header file

using namespace std;//to handle all standard characters/functions,reserved words  used in program. we do not use in turboc++ compiler

int main()          //main function
{
    int number;
    cout << "enter a number" << endl;
    cin>>number;
    cout<<"number<20:-"<<(number<20)<<endl;//returns 0 or 1 if it is false or true
    cout<<"number<=20:-"<<(number<=20)<<endl;//returns 0 or 1 if it is false or true
    cout<<"number>20:-"<<(number>20)<<endl;//returns 0 or 1 if it is false or true
    cout<<"number>=20:-"<<(number>=20)<<endl;//returns 0 or 1 if it is false or true
    cout<<"number!=20:-"<<(number!=20)<<endl;//returns 0 or 1 if it is false or true
    cout<<"number==20:-"<<(number==20)<<endl;//returns 0 or 1 if it is false or true
    return 0;
}
//note:kindly put parentheses for expression(number<20); if we do not put then it does not work.

Program to generate given output.

//program to generate following output.
/*
10
20
19
*/

#include <iostream>//header file

using namespace std;//to handle all standard characters/functions,reserved words  used in program. we do not use in turboc++ compiler.


int main()//main function
{
    int number=10;
    cout<<number<<endl;//prints 10
    number+=10;        //increses the value by 10
    cout<<number<<endl;//prints the 10
    cout<<--number;//prints 19 using prefix decrement operator.
    return 0;
}

WAP to generate given table using single cout.

//program to generate following table using single cout statement
/*
1990    135
1991    7290
1992    11300
1993    16200
*/

#include <iostream>//header file
#include<iomanip>//header file for manipulator(width to print)

using namespace std;//to handle all standard characters/functions,reserved words  used in program. we do not use in turboc++ compiler.
int main()//main function
{
    cout<<setw(4)<<"1990"<<setw(7)<<"135"<<endl
        <<setw(4)<<"1991"<<setw(7)<<"7290"<<endl
        <<setw(4)<<"1992"<<setw(7)<<"11300"<<endl
        <<setw(4)<<"1993"<<setw(7)<<"16200"<<endl;//we have used chaining concept.
                                                //for first column, we have used 4 boxes using setw(4) and
                                                //for second column, we have used 7 boxes. 5 boxes to print and 2 for space between columns.

    return 0;
}

WAP to understand unary operator(prefix and postfix).

//program to understand prefix and postfix operator
#include <iostream>//header file
using namespace std;//to handle all standard characters/functions,reserved words  used in program. we do not use in turboc++ compiler.

int main()          //main function
{
    int a=4,b;
    cout << "++a="<<++a << endl;//gives 5. It means first increase the value then use that. Its increased part (a=a+1) is invisible.
    a=4;
    cout<<"a++="<<a++;//gives 4. It means first use the value then increase the value. Its increased part is excuted after this statement and is not shown here.
                     // ++ is called increment operator. It can be prefix (++a)or postfix(a++).
    b=5;
   cout << "--a="<<--a << endl;//gives 4. It means first decrease the value then use that. Its decreased part (a=a-1) is invisible.
                    //-- is called decrement operator. It can be prefix(--a) and postfix(a--).
    b=5;
    cout<<"a--="<<a--;//gives 5. It means first use the value then decrease the value. Its decreased part is executed after this statement and is not shown here.
    return 0;
}

WAP to understand arithmetic remainder operator.

//program to understand remainder operator(modulus operator)

#include <iostream>//header file

using namespace std;//to handle all standard characters/functions,reserved words  used in program. we do not use in turboc++ compiler.

int main()          //main function
{
    cout << "remainder, when divided 5 by 7, is " <<5%7<<endl;
    return 0;
}

WAP to understand define directive or symbolic constant.

//program to understand define directive or symbolic constant
#include <iostream>//header file
#define name "RAM KUMAR KARKI"  //  defining symbolic constant for name
using namespace std;//to handle all standard characters/functions,reserved words  used in program. we do not use in turboc++ compiler.


int main()          //main function
{
    cout << "name="<<name << endl;//prints the name. Similarly we can also put numeric value.
    return 0;                       //returns zero value
}

WAP to understand const identifier

//program to understand const identifier

#include <iostream> //header file

using namespace std;//to handle all standard characters/functions,reserved words  used in program. we do not use in turboc++ compiler.


int main()             //main function
{
    const int a=90;         //declaration of variable with constant value
   // a=8;                // not allowed here; because const identifier
    cout << "value of a(constant)="<<a << endl;
    return 0;
}

WAP to understand assignment operator.

//program to understand assignment operator with contracted/condensed approach

#include <iostream>     //header file

using namespace std;//to handle all standard characters/functions,reserved words  used in program. we do not use in turboc++ compiler.

int main()              //main function
{
    int a;              //declaration
    a=56;               //value assignment
    cout << "the value of a=" <<a<< endl;//prints the value
    a+=4;                      //condensed method. it means a=a+4.
    cout<<"after adding 4 to 56, it is  "<<a;// prints the value after adding 4.
    return 0;
}

WAP to understand type casting.

// program to understand type conversion
#include <iostream>
#include<stdlib.h>   // to handle system("cls")
using namespace std;
int main()
{
    system("cls");
    int a;
    int b;
    float result;
    cout<<"a and b="<<endl;
    cin>>a>>b;              //multiple inputs using >> operator called 'extraction' or 'get from' operator. 'cin' is an object.
    result=(a*b)/2;        // type casting takes place. It is implicit.
    cout<<"implicit result ="<<result<<endl;// gives result 8 if we input 1 and 17.
    result=float(a*b)/2;    // type casting takes place (Explicit).
    cout << "explicit result=" <<result<< endl;//gives result= 8.5 if we input 1 and 17
    return 0;
}

WAP to understand library function.

//program to understand library function
#include <iostream>// header file
#include<cmath>     // header file to handle mathematical functions
using namespace std;//to handle all standard characters/functions,reserved words  used in program. we do not use in turboc++ compiler
int main()
{
    double number,number_square;// declaration of variables
    cout << "enter a number" << endl;// input message
    cin>>number;                    // accepts input
    number_square=pow(number,2);    // finds square and stores in variable
    cout<<"square ="<<number_square;// prints output.
    return 0;
}

WAP to understand setw() function.

//program to understand setw() function   //comment line
#include <iostream>              // header file to handle input and output
#include<stdlib.h>               //header file to handle return statement.
#include<iomanip>
#include<conio.h>
using namespace std;             //to handle all standard characters/functions,reserved words  used in program. we do not use in turboc++ compiler
int main()                       // main function as starting point of execution
{

    system("cls");                     // clears the screen. It exists in stdlib.h
    cout<<setw(17)<<"first line"<<endl;// setw creates a box of 17 widths to put given string
    cout<<setw(17)<<"second line"<<endl;//setw creates a box of 17 widths to put given string
    cout <<setw(17)<<"Hello world!" << endl;// cout is an object and is pre-defined. It is responsible for output stream. endl is to end the line.
                                   //<< is called insertion operator or put -to operator.
    return 0;                       // returns 0.
}

WAP to print character constant.

//program to print character constant    //comment line
#include<iostream>              // header file to handle input and output
#include<stdlib.h>
using namespace std;             //to handle all standard characters/functions,reserved words  used in program. we do not use in turboc++ compiler
int main()                        // main function as starting point of execution
{
    char value='s';
    cout<<"value="<<value<<endl//endl is a manipulator and causes a linefeed
    <<"second line"<<value;// cout is an object and is pre-defined. It is responsible for output stream. endl is to end the line.
                                   //<< is called insertion operator or put -to operator.
    return 0;                       // the string inside double quote is string constant. Here it is "hello world".
}

WAP to understand definition of variables.

//program to understand definition of variables
#include <iostream>// header file
using namespace std;//to handle all standard characters/functions,reserved words  used in program. we do not use in turboc++ compiler

int main()      // main function
{
    int number;  //declaration of variable.
     number=90;//definition of variable
    cout << "number =" <<number<< endl;
    return 0;
}

WAP to understand character variable and constant.

//program to understand character variable and constant
#include <iostream>     //header file

using namespace std;//to handle all standard characters/functions,reserved words  used in program. we do not use in turboc++ compiler.
int main()          //main function
{
    char a='r';        //declaration and value assignment(initialization). We put inside single quote.
    cout << "character="<<a << endl;//value printing
    return 0;
}

WAP to print multiple string using single cout.

//program to print multiple string using single cout object.
#include <iostream>// header file
using namespace std;//to handle all standard characters/functions,reserved words  used in program. we do not use in turboc++ compiler
int main()
{
    cout << "first line" << endl
        <<"second line"<<endl
        <<"third line"<<endl; //We have used only one cout .
                              //It is also called chaining concept. Keep in mind, put semi-colon at the last of statement.
    return 0;                   // otherwise it will not work.
}

WAp to input string

// program to understand string input in C++ using string data type
#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
    string name;    //string data type to accept string. We can not write identifier value i.e name[30].
                    // If we write then we should use char name[30].
    system("cls");  // clears the screen
    cout<<"enter name"<<endl;// cout object to print message.<< is also called insertion operator.
    getline(cin,name);     // accepts name using 'get from' operator (>> or extraction operator) with space.
    cout<<"name="<<name<<endl;// prints input values/string/name.
    cout<<"enter name again"<<endl;//prints the literal
    cin>>name;// accepts name without space.
    cout<<"name="<<"without space is:-"<<name;//prints name
    return 0;

}

WAP to input using cin.

// program to understand cin object in C++
#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
    system("cls");  // clears the screen
    int a,b;        //declaration of variables
    cout<<"enter values for a and b"<<endl;
    cin>>a>>b;// multiple inputs using 'get from' operator (>>). we can use in different line.
    cout<<"a="<<a<<" and b="<<b<<endl;// prints input values in same line with string literal. we can use different line.
    return 0;                         // it is also called chaining outputs/inputs
}

WAP to input

// program to understand cin object in C++
#include<iostream>
#inlcude<stdlib.h>
using namespace std;
int main()
{
    system("cls");
    int a,b;
    cout<<"enter values for a and b"<<endl;
    cin>>a>>b;
    cout<<"a="<<a<<"and b="<<b<<endl;
    return 0;
}

WAP to use header file with double quote.

//program to understand header file with double quote
#include "iostream"  //header file enclosed inside double quote
#include"stdlib"     // header file for system("cls")
using namespace std;
int main()
{

    cout << "Hello world!" << endl; // cout object with insertion operator/put to operator(<<)
    cout<<"this is second line"<<endl;
    return 0;
}

WAP to understand white space

//program to understand white space
#include <iostream>                 //header file
#include<stdlib.h>                  //header file
using namespace std;
int main()                          //main function
{
    cout << "Hello C++!" << endl;// string print with space called white space. It also can be tab/line feed
                                    //vertical tab or formfeeds etc.
    return 0;                       // returns 0 after execution
}

WAP to understand escape sequence.

//PROGRAM TO understand escape sequence

#include <iostream>//header file

using namespace std;//to handle all standard characters/functions,reserved words  used in program. we do not use in turboc++ compiler.

int main()          //main function
{
    cout << "first line"<<"\n";//escape sequence for new line
    cout<<"second line"<<"\n";//escape sequence for new line. we can also use single quote. It works.
    return 0;
}

WAP to understand endl manipulator

// program to understand endl manipulator
#include <iostream>  //header file
using namespace std; //to handle all standard characters/functions,reserved words  used in program. we do not use in turboc++ compiler

int main()
{
    cout<<"first line"<<endl;// cout is an object and is pre-defined. It is responsible for output stream.
                             // endl is used to feed a line in stream and is called manipulator. It works in same manner as \n does in program.
                            // we can put \n in single quote or double quote.
    cout << "second line\n";
    cout<<"third line"<<"\n"<<"or"<<endl;
    cout<<"fourth line"<<'\n';
    return 0;
}

c++ to print hello world

//program to print hello world    //comment line
#include <iostream>              // header file to handle input and output
#include<stdlib.h>               // header file to handle system(cls).
using namespace std;             //to handle all standard characters/functions,reserved words  used in program. we do not use in turboc++ compiler
int main()                       // main function as starting point of execution
{
    cout<<"firstline"<<endl;
    system("cls");                 //clears the screen
    cout << "Hello world!" << endl;// cout is an object and is pre-defined. It is responsible for output     // stream. endl is to end the line.
                                   //<< is called insertion operator or put -to operator.
    return 0;                       // the string inside double quote is string constant. Here it is "hello world".
}

c++ basic programs

We have following basic programs.
----------------------------------------------------------------------------------------
1)wap to print hello world.
                                                                                                 solution

2)WAP to understand endl.

                                                                                                solution

3)WAP to understand escape sequence.

                                                                                               solution

4)WAP to understand white space.

                                                                                              solution

5)WAP to use header file with double quote.

                                                                                                    solution

6)WAP to input.
                                                                                               solution

7)WAP to input using cin.

                                                                                                 solution

8)WAp to input string .

                                                                                                     solution

9)WAP to print multiple string using single cout.

                                                                                                solution

10)WAP to understand character variable and constant.

                                                                                                     solution

11)WAP to understand definition of variables.

                                                                                                   solution

12)WAP to print character constant.

                                                                                                solution

13)WAP to understand setw() function.

                                                                                               solution

14)WAP to understand library function.
     
                                                                                                solution

15)WAP to understand type casting.

                                                                                                solution

16)WAP to understand assignment operator.

                                                                                                 solution

17)WAP to understand const identifier.

                                                                                               solution

18)WAP to understand define directive or symbolic constant.

                                                                                                solution

19)WAP to understand arithmetic remainder operator.

                                                                                                solution

20)WAP to understand unary operator(prefix and postfix).

                                                                                                solution

21)WAP to generate given table using single cout.

                                                                                                solution

22)Program to generate given output.

                                                                                              solution

23)WAP to understand relational operator.

                                                                                              solution

24)WAP to understand logical operator.

                                                                                            solution

25)WAP to understand conditional operator.

                                                                                            solution

26)WAP to clear the screen using system(cls).

                                                                                            solution

27)WAP to add two numbers without using namespace.

                                                                                            solution

28)WAP to print hello world without namespace.

                                                                                            solution


-----------------------------------------------------------------------------------------
If you find broken link, please let me know.

C++ programs examples

------------------------------------------------------------------------------------------------
click the unit you want to know/understand.
--------------------------------------------------------------------------------------

                        It contains basic fundamental of C++ programs. Like, inputs outputs,operators,strings etc.


                                                 It contains programs related to conditions and decisions. Like if program, if..else program,if..else if program,switch program ,break and continue program. 


3. Loop related programs(loop , nested loop):- 
                                       It contains programs using while, or for or do..while loop. It is used to solve complex problem in very say way. We can use any loop to solve any program.


4. Array related programs:-
                                     It contains program using one and multidimensional array. Array is used to use as pointer. It is helpful when we write a program for multiple data using same variable.

5.Array with string programs
                                   If we want to to write program for strings. then either we can use directly string or  we can use array with string.It contains programs related to string processing (with/out library)


6.Function related programs:
                                      We can understand what is function and how to use it. It is helpful to solve complex program in divide and conquer process.It contains programs using user defined function. Click the above link to know more.

7.struct,typedef and union related program : 
                                     This section contains programs using struct, union and typedef . We can be clear about what is struct,union and typedef through examples.

Pointer programs: 
                                 When we write program by using memory directly, then pointer is helpful. It also becomes faster. Here we can get programs related to pointer, array as pointer, pointer as an array, pointer in function, call by value and call by reference. We can also use pointer for strings.

Data file handling related program:
                                      It contains programs using file handling functions etc. If we want our data permanent then we have no option than this.It makes our entered data permanent on hard disk. 

------------------------------------------------------------------------------------------------------------------
Now we are gonna talk about object Oriented program in detail.

1. Object and class program


2.