C++

Some simple example programs in C++ language

Some Simple example program in C++ language

In this tutorial, we will discuss the Some simple example program in C++ language

C++ is one of the object-oriented program similar java

C++ programming language defines several Header files similar C. That contains much useful information run your program, generally, we can use <iostream> header file

Using namespace std;  instruct the compiler to use the std namespace

main ()– main() is the main function and program execution begins. That is a beginning point in C++ 

cout<< – This is to use to display the output on the screen

return 0 – this is to help to terminate the main function and then return the value 0

C++ first program


 Here, Some C++ programs and their outputs

First Program

Helloworld program

Example 1

When the above code is compiled and executed, it produces the following result:

Output

C++ second program

Variable assignment and display default value

#include <iostream>

using namespace std;
int main()
{    
int age;   
 float average;   
 char sex;    
bool isit;   
 cout << age << endl << average << endl << sex << endl << isit << endl;    
return 0;   
 }

When the above code is compiled and executed, it produces the following result:

Example 2



out put some garbage values

C++ third program

variable assignment and display given value

#include <iostream>

using namespace std;

int main()
{
    int age=23;
    float average=22.52;
    char sex='m';
    bool isit=true;

    cout << age << endl << average << endl << sex << endl<< isit<< endl;
    return 0;
}

 

When the above code is compiled and executed, it produces the following result:

Example 3

output the assigned value.

C++ forth program

Display the value using cout in C++ language

using the cout

#include <iostream>

using namespace std;

int main()
{
    cout << "Hello this is using cout in C++" << endl;
    return 0;

}

 

When the above code is compiled and executed, it produces the following result:

Example 4

bio data using cout

#include <iostream>
using namespace std;
int main()

{

int age=35;
int dob=19770805;
int icno=772182503;

    cout <<"Hello my name is jhon"<<endl<<"my age is "<<age<< endl
    <<"my dob is "<<dob<< endl
    <<"my ic no is "<<icno<< endl<< "ya this is my biodata"<<endl;
    return 0;

}

 


When the above code is compiled and executed, it produces the following result:

Example 5

 

C++ fifth program

Takes input from thr user

using cin

#include <iostream>

using namespace std;

int main()
{
    int age;
    int marks;
    float avg;
    cout << "enter your age and average and marks "<< endl;
    cin >> age >> avg >> marks;
    cout << "you have entered age is "<< endl << age<< endl << "average is "
    << endl << avg << endl <<"marks is " << endl<< marks << endl;
    return 0;

}



Program 6

When the above code is compiled and executed, it produces the following result:

Output

C++ sixth program

Arithmetic operation in C++

#include <iostream>

using namespace std;

int main()
{
    int num1=10 , num2=20;
    cout << num1 <<"+"<<num2<<"="<<num1+num2<< endl;
    cout << num1 <<"-"<<num2<<"="<<num1-num2<< endl;
    cout << num1 <<"*"<<num2<<"="<<num1*num2<< endl;
    cout << num1 <<"/"<<num2<<"="<<num1/num2<< endl;

    return 0;
}

 

When the above code is compiled and executed, it produces the following result:

Program 7

C++ seventh program

increment and decrement operators

prefix – ++a or –a
postfix – a++ or a–

#include <iostream>

using namespace std;

int main()
{
    int a=10;
    a++;
    cout << a<<endl;
    a--;
    cout << a <<endl;;

    return 0;

}

 



When the above code is compiled and executed, it produces the following result:

program 8

 

Using assignment operator

 

#include <iostream>
using namespace std;

int main()
{
int a;
int b=3;
a=b;
a+=2;

cout << a;
return 0;

}

 

 

When the above code is compiled and executed, it produces the following result:

 

 

Suggested for you
Karmehavannan

Recent Posts

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

9 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

10 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

10 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

10 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

10 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

10 months ago

This website uses cookies.