Professional Documents
Culture Documents
C++ Program Concepts
C++ Program Concepts
C++ Program Concepts
19/01/12
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 }
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; }
// process: a = 5; b = 2; a = a + 1; result = a - b; // print out the result: cout << result; // terminate the program: return 0; }
cin >> gallons; // Read the inputs from the user liters = gallons * 3.7854; // convert to liters cout << "Liters: " << liters << endl; return 0; }
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];
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); }
while(name[i]!='\0') { i++; } cout << "Lenght of the name is : " << i << 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); }
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; }
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; }
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 {
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); }