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
When the above code is compiled and executed, it produces the following result:
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:
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:
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; }
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:
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; }
When the above code is compiled and executed, it produces the following result:
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:
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:
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: