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

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.