C++ Program Concepts

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 31

C++ Programming

19/01/12

A simple C++ program


A simple C++ program which prints "Hello World!" on the screen #include <iostream> // need this header //file to support the C++ I/O system using namespace std; // telling the //compiler to use namespace "std", // where the entire C++ library is declared.

int main() { // Print out a sentence on the screen. // "<<" causes the expression on its right to // be directed to the device on its left. // "cout" is the standard output device
cout << "Hello World!" << endl; return 0; // returns 0,which indicate the //successful termination of the "main //function }

Program for Variables


#include <iostream> using namespace std; // declaring a constant. It's value cannot be changed. const int CONST_VAL = 5; int main() { int iValue; // An integer variable float fValue; // A floating point variable char cValue; // A character variable

iValue = 1234; // Assigns 1234 to iValue fValue = 1234.56; // Assigns 1234.56 to fValue cValue = 'A'; // Assigns A to cValue
// Now print them out on the screen: cout << "Integer value is: " << iValue << endl; cout << "Float value is: " << fValue << endl; cout << "Character value is: " << cValue << endl; cout << "The constant is: " << CONST_VAL << endl; return 0; }

Program for operation with variables


#include <iostream> using namespace std; int main () { // declaring variables: int a, b; int result;

// process: a = 5; b = 2; a = a + 1; result = a - b; // print out the result: cout << result; // terminate the program: return 0; }

Converts gallons to liters


#include <iostream> using namespace std; int main() { float gallons, liters; cout << "Enter number of gallons: ";

cin >> gallons; // Read the inputs from the user liters = gallons * 3.7854; // convert to liters cout << "Liters: " << liters << endl; return 0; }

Program for Array


#include<iostream> using namespace std; int main () { int age[10]; int i,sum=0, avg=0; int max=0,min=100;

for(i=0;i<10;i++) { cout << "Enter the age of the student " << i+1 <<endl; cin >> age[i]; } for(i=0;i<10;i++) { sum=sum+age[i];

if(age[i]>max) { max=age[i]; } if(age[i]<min) { min=age[i]; }

avg=sum/10; cout << "Average age of the students of the class : " << avg << endl; cout << "Maximum age of the student of the class : " << max << endl; cout << "Minimum age of the student of the class : " << min << endl; return(0); }

Program for calculating length of string


#include<iostream> using namespace std; int main() { char name[15]; int i=0; cout << " Enter your name " << endl; cin >> name;

while(name[i]!='\0') { i++; } cout << "Lenght of the name is : " << i << endl; return(0); }

Program for Display a triangle using for loop


for (int i = 0; i<n; i++) { for(int j = n-i- 1 ; j >= 0 ; j--) { cout << ; } for (int k = 0 ; k< = ((2*i+1)-1) ; k++) { cout <<* ; } Cout << endl ; } return 0 ; }

calculates the average of the elements of the row of the two dimensional array
#include<iostream> using namespace std;
int main () { int age[2][5]= { {12,13,14,15,15}, { 12,16,17,13,12}}; int i,j; int sum=0,avg=0;

for(i=0;i<2;i++) { for(j=0;j<5;j++) { sum=sum+age[i][j]; } avg=sum/5; cout << "Average of the elements of the row " << i+1 << " is " << avg << endl; sum=0; } return(0); }

A program which illustrates the working of pointer arithmetic


#include<iostream> using namespace std; Int main () { int *x; int *p,*q; int c=100,a; x=&c; p=x+2; q=x-2; a=p-q;

cout << "The address of x : " << x << endl; cout << "The address of p after incrementing x by 2 : " << p << endl; cout << "The address of q after decrementing x by 2 : " << q << endl; cout << " The no of elements between p and q :" << a << endl; return 0; }

A program which illustrates the working of arrays and pointers


#include<iostream> using namespace std; int main() { int age[5]; int *p; int sum=0,i; char yes='y'; p=age;

for(i=0;i<5;i++) { cout << "Enter the age of a student" << endl; cin >> *p;
sum=sum+*p; p++; } p=age; cout << "The sum of the ages" << sum << endl; cout << "The age of the last student is : " << *(p + 4) << endl; return 0; }

Program for Function Prototuping


#include<iostream> using namespace std; int factorial(int n); int main () { int n1,fact; cout <<"Enter the number whose factorial has to be calculated" << endl; cin >> n1;

fact=factorial(n1); cout << "The factorial of " << n1 << " is : " << fact << endl; return 0 ; } int factorial(int n) { int i=0,fact=1; if(n<=1) { return(1); }

else {

for(i=1;i<=n;i++) { fact=fact*i; } return(fact); }


}

Parameter passing mechanism


Pass by value Pass by reference

Pass by value
#include<iostream> using namespace std; int add(int n); int main() { int number,result; number=5; cout << " The initial value of number : " << number << endl; result=add(number);

cout << " The final value of number : " << number << endl; cout << " The result is : " << result << endl; return(0); } int add(int number) { number=number+100; return(number); }

Pass by reference
#include<iostream> using namespace std; int add(int &number); int main () { int number; int result; number=5; cout << "The value of the variable number before calling the function : " << number << endl;

result=add(&number); cout << "The value of the variable number after the function is returned : " << number << endl; cout << "The value of result : " << result << endl; return(0); } int add(int &p) { *p=*p+100; return(*p); }

Thats all . Thanks !!

You might also like