basics of c++

35
C++ Program Structure: Let us look at a simple code that would print the words Hello World. #include <iostream> // main() is where program execution begins. int main() { cout << "Hello World"; // prints Hello World return 0; } Comments in C++ C++ supports single line and multi-line comments. All characters available inside any comment are ignored by C++ compiler. C++ comments start with /* and end with */. For example: // This is a single line comment /* C++ comments can also * span multiple lines */ #include <iostream> using namespace std; main() { /* C++ comments This Program prints Hello World */ cout << "Hello World"; // prints Hello World return 0; } Data Types in C++ Three types: Built in data type Derived data type User-defined data type Data type is one of the fundamental properties of a variable. 1. Built in Data Types: 2. char a single byte that can hold one character. 3. int an integer 4. float a single precision floating point number. 5. double a double precision floating point number. Qualifiers:- short, long, signed, unsigned are called qualifiers. 1

Upload: dassdass22

Post on 21-Jul-2016

214 views

Category:

Documents


0 download

DESCRIPTION

C++

TRANSCRIPT

Page 1: basics of c++

C++ Program Structure:Let us look at a simple code that would print the words Hello World.#include <iostream>// main() is where program execution begins.int main(){ cout << "Hello World"; // prints Hello World return 0;}

Comments in C++C++ supports single line and multi-line comments. All characters available inside any comment are ignored by C++ compiler.C++ comments start with /* and end with */. For example:// This is a single line comment/* C++ comments can also * span multiple lines */#include <iostream>using namespace std;main(){/* C++ comments This Program prints Hello World */cout << "Hello World"; // prints Hello Worldreturn 0;}

Data Types in C++Three types:

Built in data type Derived data type User-defined data type

Data type is one of the fundamental properties of a variable.

1. Built in Data Types: 2. char a single byte that can hold one character.3. int an integer4. float a single precision floating point number.5. double a double precision floating point number.

Qualifiers:-short, long, signed, unsigned are called qualifiers.

i. Short, long size qualifiersii. Signed, unsigned sign qualifiers

Size qualifiers can not be applied to char, float, data types.Sign qualifiers can not be applied to float, double & long double.

1

Page 2: basics of c++

Name Description Size* Range*

char Character or small integer. 1 byte

signed: -128 to 127unsigned: 0 to 255character: a to z A to Z & Special Characters

short int Short Integer. 2 bytes signed: -32768 to 32767unsigned: 0 to 65535

int Integer. 4 bytes signed: -2147483648 to 2147483647unsigned: 0 to 4294967295

long int Long integer. 4 bytes signed: -2147483648 to 2147483647unsigned: 0 to 4294967295

bool Boolean value. It can take one of two values: true or false. 1 byte true or false

float Floating point number. 4 bytes +/- 3.4e +/- 38 (~7 digits)

double Double precision floating point number. 8 bytes +/- 1.7e +/- 308 (~15 digits)

long double Long double precision floating point number. 8 bytes +/- 1.7e +/- 308 (~15 digits)

2.Derived Datatypes: Arrays Pointers Function Reference

3.User-Defined Data Types: Struct Union Class Enumeration

C++ Operators:An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. C++ is rich in built-in operators and provides following type of operators:

Arithmetic Operators ( +, -, \, *, ++, --) Relational Operators (==, !=, >. <, >=, <=) Logical Operators (&&, ||, ! ) Bitwise Operators (& |, ^, ~, <<, >>) Assignment Operators (=, +=, -=, *=, /=, %=, <<=, >>=, &=, ^=, |=) Misc Operators ( sizeof, & cast, comma, conditional etc.)

Types of operators:1. Assignment operators2. Arithmetic operators3. Relational operators4. Logical operators5. Bitwise operators6. Compound Assignment Operators7. Increment & decrement operators8. Conditional operators9. Special operator

1.Assignment Operators (=)

2

Page 3: basics of c++

General syntax: variable=expression;2.Arithmetic Operators:-

Operator Meaning+ Addition or unary plus.- Subtraction or unary minus.* Multiplication./ Division.

% Modulo division.3.Relational Operators:- It is used to make comparisons between two expressions. All these operators are binary and require two operands.

Operator Meaning< Less than> Greater than

<= Less than equal to>= Greater than equal to= = Equal to!= Not equal to

4.Logical Operators: Logical operators are useful in combining one or more conditions. Any expression that

evaluates to zero denotes a FALSE condition and that evaluating to non-zero value denotes a TRUE condition.Operator Meaning Type

&& Logical AND Binary| | Logical OR! Logical NOT Unary

5.Bitwise Operator:Operator Meaning

& Bitwise AND| Bitwise OR^ Bitwise EX-OR~ Bitwise complement

<< Shift left>> Shift right

6.Compound Assignment Operators:

7. Increment and Decrement Operator:-Operator Syntax

++ variable name ++;++variable name;

-- variable name--;-- variable name;

8.Conditional Operator(TERNARY OPERATOR) ?:

3

Operator Usage Effect+ = a + = exp; a = a + (exp);- = a - = exp; a = a - (exp);* = a * = exp; a = a * (exp);/ = a / = exp; a = a / (exp);

% = a % = exp; a = a % (exp);

Page 4: basics of c++

General syntax: exp1? True : False

7.Special Operators:

Sizeof = returns the size of data type or variable in terms of bytes occupied in memory.

delete = Memory release operator

new = Memory allocation operator

* = Indirection operator.

& = address operator.

, = comma operator.

:: = Scope Resolution operator

C++ Identifiers:

A C++ identifier is a name used to identify a variable, function, class, module, or any other user-defined item.

An identifier starts with a letter A to Z or a to z or an underscore (_) followed by zero or more letters,

underscores, and digits (0 to 9).

C++ does not allow punctuation characters such as @, $, and % within identifiers. C++ is a case-sensitive

programming language. Thus, Manpower and manpower are two different identifiers in C++.

C++ Keywords:

The following list shows the reserved words in C++. These reserved words may not be used as constant or

variable or any other identifier names.

Asm else new this Auto enum operator throw Bool explicit private true Break export protected try Case extern public typedef Catch false register typeid Char float reinterpret_cast typename Class for return union Const friend short unsigned const_cast goto signed using continue If sizeof virtual Default inline Static void Delete int static_cast volatile Do long Struct wchar_t Double mutable Switch while dynamic_cast namespace Template

CONTROL STATEMENT: Conditional Statement

4

Page 5: basics of c++

Looping Statement Breaking Statement

CONDITIONAL STATEMENT:If StatementIf…else StatementSwitch case Statement

SYNTAX: if(condition){//If condition true execute this statement}

if(condition){//True statement}else{//False statement}

switch(variable){case (value1):{}break;case (value2):{}break;default:break;}

LOOPING STATEMENT

for loop while loopdo while loop

SYNTAX: for (intialise;condition;++/--){// Statement}

while (condition){// Statement }

do{// Statement }while(condition);

Break statement: A break construct terminates the execution of loop and the control is transferred to the statement

immediately following to the loop. The term ‘break’ refers to the act of breaking out of a block of code.

Fig:break statements in loops; for(initialize;exp1;exp2) { _______;

if(condition) True break; _________; } statement;

Function:Function Prototyping:returntype functionname(argumentlist);Ex: int add(int a, intb);

5

Page 6: basics of c++

Function Definition:returntype functionname(argumentlist){// Statements}Ex:int add(int a, int b){return(a+b);}Function Call:functionname(argumentvalue);Ex:add(2,4);

Call by Valueint add(int a,int b); // Call by ValueCall by Referrenceint add(int &a, int &b); // Call by Referrenceint add(int *a, int *b); // Call by Referrence

CLASSES AND OBJECTSClass Declarationclass classname{private://Data member//Member Functionpublic://Data member//Member Function};Object DeclarationMethod 1:Void main(){classname obj1,obj2;}Method 2:class classname{//Data member//Member Function}obj1,obj2;

Accessing Class MembersFor Data member

Objectname.datamember;For Member Function

Objectname.memberfunction(argumentlist);

6

Page 7: basics of c++

Defining Member FunctionInside the Classclass classname{returntype functionname(argumentlist){// Statements}};Outside the Classclass classname{returntype functionname(argumentlist);};returntype classname::functionname(argumentlist){// Statements}

PointersDeclaration: datatype *pointer_variable;Retrieve address: pointer_variable = & variable;

C++ PROGRAM

1. C++ PROGRAM TO OUTPUT AN INTEGER, A FLOATING POINT NUMBER AND A CHARACTER

#include <iostream.h>#include <conio.h>void main(){ clrscr(); int x = 10; float y = 10.1; char z = 'a'; cout << "x = " << x << endl; cout << "y = " << y << endl; cout << "z = " << z << endl; getch();}

2. C++ PROGRAM TO FIND THE SUM, DIFFERENCE, PRODUCT AND QUOTIENT OF TWO INTEGERS

#include <iostream.h>#include <conio.h>void main(){ clrscr();

7

Page 8: basics of c++

int x = 10; int y = 2; int sum, difference, product, quotient; sum = x + y; difference = x - y; product = x * y; quotient = x / y; cout << "The sum of " << x << " & " << y << " is " << sum << "." << endl; cout << "The difference of " << x << " & " << "y << is " << difference << "." << endl; cout << "The product of " << x << " & " << y << " is " << product << "." << endl; cout << "The quotient of " << x << " & " << y << " is " << quotient << "." << endl; getch();}

3. C++ PROGRAM TO ENTER TWO INTEGERS AND FIND THEIR SUM AND AVERAGE

#include <iostream.h>#include <iostream.h>#include <conio.h>void main(){clrscr();int x,y,sum;float average;cout << "Enter 2 integers : " << endl;cin>>x>>y;sum=x+y;average=sum/2;cout << "The sum of " << x << " and " << y << " is " << sum << "." << endl;cout << "The average of " << x << " and " << y << " is " << average << "." << endl;getch();}

4. PROGRAM TO ENTER YOUR AGE AND PRINT IF YOU SHOULD BE IN GRADE 10#include <iostream.h>#include <conio.h>void main(){clrscr();int age;cout << "Enter your present age : " << endl;cin>>age;if(age==16){cout << "Your present age is " << age << " years." << endl;cout << "You are of the right age for joining grade 10 !" << endl;}else{cout << "Your present age is " << age << " years." << endl;cout << "You are not of the right age for joining grade 10 !" << endl;}

8

Page 9: basics of c++

getch();}

5. PROGRAM TO ENTER AN INTEGER AND PRINT IF IT IS GREATER OR LESS THAN 100

#include <iostream.h>#include <conio.h>void main(){clrscr();int x;cout << "Enter an integer : " << endl;cin>>x;if(x>100){cout << x << " is greater than 100." << endl;}else{cout << x << " is less than 100." << endl;}getch();}

6. PROGRAM TO SWITCH BETWEEN DIFFERENT CASES#include <iostream.h>#include <conio.h>int main(){clrscr();int choice;cout << "1. Talk" << endl;cout << "2. Eat" << endl;cout << "3. Play" << endl;cout << "4. Sleep" << endl;cout << "Enter your choice : " << endl;cin>>choice;switch(choice){case 1 : cout << "You chose to talk...talking too much is a bad habit." << endl;break;case 2 : cout << "You chose to eat...eating healthy foodstuff is good." << endl;break;case 3 : cout << "You chose to play...playing too much everyday is bad." << endl;break;case 4 : cout << "You chose to sleep...sleeping enough is a good habit." << endl;break;default : cout << "You did not choose anything...so exit this program." << endl;}getch();}

7. PROGRAM TO ENTER AN INTEGER AND PRINT IF IT IS PRIME OR COMPOSITE#include <iostream.h>

9

Page 10: basics of c++

#include <conio.h>#include <process.h>void main(){clrscr();int num1,x;cout << "Enter an integer : " << endl;cin>>num1;for(x=2;x<num1;x++){if(num1%x==0){cout << num1 << " is a composite number." << endl;getch();exit(0);}else{cout << num1 << " is a prime number." << endl;getch();exit(0);}}}

8. PROGRAM TO ENTER AN INTEGER AND OUTPUT THE CUBE OF THAT INTEGER#include <iostream.h>#include <conio.h>int cube(int x); //The prototype.void main(){int a;cout << "Enter an integer : ";cin>>a;cout << "The cube of " << a << " is : " << cube(a) << endl; //Call the function cube(a).getch();}int cube(int x) //Defining the function.{int y;y=x*x*x;return(y);}

9. PROGRAM TO ENTER TWO INTEGERS AND PRINT THE QUOTIENT AND REMAINDER

#include <iostream.h>#include <conio.h>int main(){clrscr();int x,y,quotient,remainder;cout << "Enter 2 integers greater than 0 : ";cin>>x>>y;

10

Page 11: basics of c++

quotient=x/y;remainder=x-(quotient*y);cout << "Quotient of " << x << " & " << y << " = " << quotient << "\n";cout << "Remainder" << " = " << remainder << "\n";getch();return 0;}

10. PROGRAM TO ENTER THREE INTEGERS AND OUTPUT THE BIGGEST INTEGER#include <iostream.h>#include <conio.h>int main(){clrscr();int x,y,z,biggest;cout << "Enter 3 integers : ";cin>>x>>y>>z;biggest=x>y?(x>z?x:z):(y>z?y:z);cout << "The biggest integer out of the 3 integers you typed ";cout << x << ", " << y << " & " << z << " is : " << "\n" << biggest << "\n";getch();return 0;}

11. PROGRAM TO ENTER THREE INTEGERS AND OUTPUT THE BIGGEST INTEGER USING IF

#include <iostream.h>#include <conio.h>int main(){clrscr();int x,y,z,biggest;cout << "Enter 3 integers : ";cin>>x>>y>>z;biggest=x;if(y>biggest)biggest=y;if(z>biggest)biggest=z;cout << "The biggest integer out of the 3 integers you typed ";cout << x << ", " << y << " & " << z << " is : " << "\n" << biggest << "\n";getch();return 0;}

12. PROGRAM TO FIND THE ROOTS OF A QUADRATIC EQUATION#include <iostream.h>#include <conio.h>#include <math.h>int main(){clrscr();float a,b,c,d,root1,root2;cout << "Enter the 3 coefficients a, b, c : " << endl;cin>>a>>b>>c;

11

Page 12: basics of c++

if(!a){if(!b)cout << "Both a and b cannot be 0 in ax^2 + bx + c = 0" << "\n";else{d=-c/b;cout << "The solution of the linear equation is : " << d << endl;}}else{d=b*b-4*a*c;if(d>0)root1=(-b+sqrt(d))/(2*a);root2=(-b-sqrt(d))/(2*a);cout << "The first root = " << root1 << endl;cout << "The second root = " << root2 << endl;}getch();return 0;}

13. PROGRAM TO ENTER AN INTEGER AND OUTPUT IT IN THE REVERSED FORM#include <iostream.h>#include <conio.h>void main(){clrscr();long int num1,num2,rnum=0;cout << "Enter an integer : " << endl;cin>>num1;num2=num1;do{rnum=rnum*10;int digit=num1%10;rnum+=digit;num1/=10;}while(num1);cout << "The integer you typed is " << num2 << "." << endl;cout << "The reversed integer is " << rnum << "." << endl;getch();}

14. PROGRAM TO CONVERT DAYS INTO YEARS AND WEEKS#include <iostream.h>#include <conio.h>void main(){clrscr();int days,years,weeks,num1;

12

Page 13: basics of c++

cout << "Enter the number of days : " << endl;cin>>days;years=days/365;num1=days-(years*365);weeks=days/7;num1=days-(weeks*7);cout << days << " days = " << endl;cout << weeks << " weeks OR " << endl;cout << years << " years." << endl;getch();}

15. PROGRAM TO COMPUTE THE FIBONACCI SERIES#include <iostream.h>#include <conio.h>void main(){clrscr();int a,b,x,y,num1,ct;a=0;b=1;cout << "Enter the number of terms (less than 25) : " << endl;cin>>num1;cout << a << endl;cout << b << endl;for(ct=1;ct<=num1-2;ct++){x=a+b;cout << x << endl;y=a;a=b;b=x;}getch();}

16. PROGRAM TO ENTER A CHARACTER AND OUTPUT ITS ASCII CODE#include <iostream.h>#include <conio.h>

void main(){clrscr();char charac;cout << "Enter the character : " << endl;cin>>charac;int num1=charac;cout << "The ASCII code for " << charac << " is " << num1 << "." << endl;getch();}

13

Page 14: basics of c++

17. PROGRAM TO PRINT THE FIRST 10 LINES OF PASCAL'S TRIANGLE#include <iostream.h>#include <conio.h>#include <iomanip.h>long triangle(int x,int y);int main(){clrscr();const lines=10;for (int i=0;i<lines;i++)for (int j=1;j<lines-i;j++)cout << setw(2) << " ";for (int j=0;j<=i;j++)cout << setw(4) << triangle(i,j);cout << endl;getch();}long triangle(int x,int y){if(x<0||y<0||y>x)return 0;long c=1;for (int i=1;i<=y;i++,x--)c=c*x/i;return c;}

18. PROGRAM TO CONVERT TEMPERATURES FROM CELSIUS TO FAHRENHEIT AND VICE VERSA

#include <iostream.h>#include <conio.h>void main(){clrscr();int choice;float ctemp,ftemp;cout << "1.Celsius to Fahrenheit" << endl;cout << "2.Fahrenheit to Celsius" << endl;cout << "Choose between 1 & 2 : " << endl;cin>>choice;if (choice==1){cout << "Enter the temperature in Celsius : " << endl;cin>>ctemp;ftemp=(1.8*ctemp)+32;cout << "Temperature in Fahrenheit = " << ftemp << endl;}else{cout << "Enter the temperature in Fahrenheit : " << endl;cin>>ftemp;ctemp=(ftemp-32)/1.8;cout << "Temperature in Celsius = " << ctemp << endl;

14

Page 15: basics of c++

}getch();}

19. PROGRAM TO FIND THE SUM OF EITHER OF THE DIAGONALS OF A 4 X 4 MATRIX#include <iostream.h>#include <conio.h>void main(){clrscr();int x;int A[4][4],sum=0; //Reading the matrix.cout << "Enter the elements of the matrix : " << endl;for(int y=0;y<4;y++)for (int x=0;x<4;x++){cout << "Element " << x+1 << ", " << y+1 << " : ";cin>>A[x][y];}//Sum of either of the diagonal elements.for(x=0;x<4;x++)for(y=0;y<4;y++)if(x==y)sum+=A[x][y];else if(y==4-(1+1));sum+=A[x][y];cout << "Sum of either of the diagonal elements is : " << sum;getch();}

20. PROGRAM TO ENTER THREE INTEGERS AND OUTPUT THE SMALLEST INTEGER USING IF

#include <iostream.h>#include <conio.h>int main(){clrscr();int x,y,z,smallest;cout << "Enter 3 integers : ";cin>>x>>y>>z;smallest=x;if(y<smallest)smallest=y;if(z<smallest)smallest=z;cout << "The smallest integer out of the 3 integers you typed ";cout << x << ", " << y << " & " << z << " is : " << "\n" << smallest << "\n";getch();return 0;}

21. PROGRAM TO FIND SUM OF ELEMENTS OF ARRAY USING FUNCTION#include<iostream.h>#include<conio.h>void sum(int a[],int n);

15

Page 16: basics of c++

void main(){clrscr();int n,a[10];cout<<"enter the number of elements in array";cin>>n;cout<<"enter value of elements of array"<<"\n";for(int i=0;i<n;i++){cin>>a[i];}cout<<"entered value of array element is"<<"\n";for(i=0;i<n;i++){cout<<"a["<<i<<"]="<<a[i]<<"\n";}sum(a,n);getch();}void sum(int a[],int n){int sum=0;for(int i=0;i<n;i++){sum=sum+a[i];}cout<<"sum of elements ="<<sum;}

22. PROGRAM TO CALCULATE AVERAGE OF 'n' ELEMENTS USING ARRAY#include<iostream.h>#include<conio.h>void main(){clrscr();int i,n,sum=0,a[20];float avg;cout<<"enter number of elements in array";cin>>n;cout<<"enter value of elements of array"<<"\n";for(i=0;i<n;i++){cin>>a[i];}cout<<"sum of elements of array are"<<"\n";for(i=0;i<n;i++){sum=sum+a[i];}cout<<"sum ="<<sum<<"\n";avg=sum/n;cout<<"average ="<<avg;getch();

16

Page 17: basics of c++

}23. PROGRAM TO FIND THE TRANSPOSE OF GIVEN MATRIX

#include<iostream.h>#include<conio.h>void main(){clrscr();int i,j,m,n,a[10][10],b[10][10];cout<<"enter the number of rows and column of matrix"<<"\n";cin>>m>>n;cout<<"enter the value of elements of matrix";for(i=0;i<m;i++){for(j=0;j<n;j++){cin>>a[i][j];}}cout<<"entered matrix is"<<"\n";for(i=0;i<m;i++){cout<<"\n";for(j=0;j<n;j++){cout<<a[i][j]<<"\t";}}cout<<"\n transpose of matrix is"<<"\n";for(i=0;i<n;i++){for(j=0;j<m;j++){b[i][j]=a[j][i];}}for(i=0;i<n;i++){cout<<"\n";for(j=0;j<m;j++){cout<<b[i][j]<<"\t";}}getch();}

24. PROGRAM OF FUNCTION WITH DEFAULT ARGUMENTS#include<iostream.h>#include<conio.h>#include<iomanip.h>void volume(int l=10,int w=10,int h=10);void main(){

17

Page 18: basics of c++

clrscr();volume(); //equivalent to (10,10,10)//volume(5); //equivalent to (5,10,10)//volume(5,6); //equivalent to (5,6,10)//volume(6,4,5); //equivalent to(6,4,5)//getch();}void volume(int l,int w,int h){cout<<"volume is"<<l*w*h<<"\n";}

25. PROGRAM TO CALCULATE THE SQUAREROOT OF THE GIVEN NUMBER#include<iostream.h>#include<conio.h>#include<math.h>void main(){clrscr();int a;float squareroot,cuberoot;cout<<"enter the number";cin>>a;cout<<"\n"<<"square root of the number is" ;squareroot=sqrt(a);cout<<squareroot;getch();}

26. PROGRAM TO FIND THE SUM USING FUNCTION#include<iostream.h>#include<conio.h>void main(){clrscr();int a,b;void sum(int,int);cout<<"enter two numbers";cin>>a>>b;sum(a,b);getch();}void sum(int x,int y){int z;z=x+y;cout<<"sum="<<z;}

27. PROGRAM FOR SWITCH STATEMENT#include<iostream.h>#include<conio.h>void main(){clrscr();

18

Page 19: basics of c++

int a,b,c;cout<<"enter the value of two numbers";cin>>a>>b;cout<<"1.addition"<<"\n";cout<<"2.multiplication"<<"\n";cout<<"3.division"<<"\n";cout<<"4.subtraction"<<"\n";int choice;cout<<"enter your choice";cin>>choice;switch(choice){case 1:c=a+b;cout<<"sum is"<<c;break;case 2:c=a*b;cout<<"multiplication is"<<c;break;case 3:c=a/b;cout<<"division is"<<c;break;case 4:c=a-b;cout<<"subtraction is"<<c;break;default:cout<<"invalid choice:";}getch();}

28. PROGRAM TO CALCULATE SQUARE AND CUBE OF A NUMBER#include<iostream.h>#include<conio.h>void main(){clrscr();int n,i,square,cube;cout<<"enter the value of n";cin>>n;cout<<"cube and square of number is"<<"\n";for(i=1;i<=n;i++){square=i*i;cube=i*i*i;cout<<"\t\t"<<i<<"\t\t"<<square<<"\t\t"<<cube<<"\n";}getch();}

29. PROGRAM TO CALCULATE THE SIMPLE INTEREST#include<iostream.h>#include<conio.h>void main(){clrscr();

19

Page 20: basics of c++

float p,r,t,si;cout<<"enter principle,rate,interest";cin>>p>>r>>t;si=(p*r*t)/100;cout<<"simple interest is"<<si;getch();}

30. PROGRAM TO FIND THE SUM OF TWO NUMBER#include<iostream.h>#include<conio.h>void main(){int a,b,c;cout<<"enter the value of two numbers";cin>>a>>b;c=a+b;cout<<"sum of two number is"<<c;getch();}

31. PROGRAM TO MULTIPLY TWO MATRICES#include<iostream.h>#include<conio.h>void main(){clrscr();int i,j,m,n,p,q,a[10][10],b[10][10],c[10][10];cout<<"enter row and column of matrix A";cin>>m>>n;cout<<"\n enter the row and column of matrix B";cin>>p>>q;if(n!=p){cout<<"multiplication not possible";}else{cout<<"enter value of elements of matrix A"<<"\n";for(i=0;i<m;i++){for(j=0;j<n;j++){cin>>a[i][j];}}cout<<"enter values of elements of matrix B"<<"\n";for(i=0;i<p;i++){for(j=0;j<q;j++){cin>>b[i][j];}}

20

Page 21: basics of c++

cout<<"values of elements of matrix A are"<<"\n";for(i=0;i<m;i++){for(j=0;j<n;j++){cout<<"\t";cout<<a[i][j];}cout<<"\n";}cout<<"values of elements of matrix B are"<<"\n";for(i=0;i<p;i++){for(j=0;j<q;j++){cout<<"\t";cout<<b[i][j];}cout<<"\n";}cout<<"multiplication of two matrix is:";for(i=0;i<m;i++){cout<<"\n";for(j=0;j<q;j++){c[i][j]=0;for(int k=0;k<p;k++){c[i][j]=c[i][j]+a[i][k]*b[k][j];}cout<<c[i][j]<<"\t";}}}getch();}

32. PROGRAM TO SUBTRACT TWO MATRIX#include<iostream.h>#include<conio.h>void main(){clrscr();int i,j,n,a[10][10],b[10][10],c[10][10];cout<<"enter the order of matrix A";cin>>n;cout<<"enter value of elements of matrix A"<<"\n";for(i=0;i<n;i++){for(j=0;j<n;j++){cin>>a[i][j];

21

Page 22: basics of c++

}}cout<<"enter the order of matrix B";cin>>n;cout<<"enter values of elements of matrix B"<<"\n";for(i=0;i<n;i++){for(j=0;j<n;j++){cin>>b[i][j];}}cout<<"values of elements of matrix A are"<<"\n";for(i=0;i<n;i++){for(j=0;j<n;j++){cout<<"\t";cout<<a[i][j];}cout<<"\n";}cout<<"values of elements of matrix B are"<<"\n";for(i=0;i<n;i++){for(j=0;j<n;j++){cout<<"\t";cout<<b[i][j];}cout<<"\n";}cout<<"subtraction of matrix A and B is"<<"\n";for(i=0;i<n;i++){cout<<"\n";for(j=0;j<n;j++){c[i][j]=a[i][j]-b[i][j];cout<<c[i][j]<<"\t";}cout<<"\n";}getch();}

33. PROGRAM TO ADD TWO MATRIX#include<iostream.h>#include<conio.h>void main(){clrscr();int i,j,n,a[10][10],b[10][10],c[10][10];

22

Page 23: basics of c++

cout<<"enter the order of matrix A";cin>>n;cout<<"enter value of elements of matrix A"<<"\n";for(i=0;i<n;i++){for(j=0;j<n;j++){cin>>a[i][j];}}cout<<"enter the order of matrix B";cin>>n;cout<<"enter values of elements of matrix B"<<"\n";for(i=0;i<n;i++){for(j=0;j<n;j++){cin>>b[i][j];}}cout<<"values of elements of matrix A are"<<"\n";for(i=0;i<n;i++){for(j=0;j<n;j++){cout<<"\t";cout<<a[i][j];}cout<<"\n";}cout<<"values of elements of matrix B are"<<"\n";for(i=0;i<n;i++){for(j=0;j<n;j++){cout<<"\t";cout<<b[i][j];}cout<<"\n";}cout<<"addition of matrix A and B is"<<"\n";for(i=0;i<n;i++){cout<<"\n";for(j=0;j<n;j++){c[i][j]=a[i][j]+b[i][j];cout<<c[i][j]<<"\t";}cout<<"\n";}getch();

23

Page 24: basics of c++

}34. PROGRAM TO CHECK WHETHER THE ENTERED NUMBER IS EVEN OR ODD

#include<iostream.h>#include<conio.h>void main(){clrscr();int n;cout<<"enter any number";cin>>n;if(n%2==0){cout<<"number is even";}else{cout<<"number is odd";}getch();}

35. PROGRAM TO CHECK WHETHER THE GIVEN NUMBER IS ARMSTRONG OR NOT#include<iostream.h>#include<conio.h>void main(){clrscr();int i,n,m,rem,sum=0;cout<<"enter the required number";cin>>n;m=n;while(m>0){rem=m%10;m=m/10;sum=sum+rem*rem*rem;}if(sum==n){cout<<"number is armstrong";}else{cout<<"number is not armstrong";}getch();}

36. PRIME NUMBER PROGRAM#include<iostream.h>#include<conio.h>#include<process.h>void main(){

24

Page 25: basics of c++

clrscr();int n;char ch;do{cout<<"enter the number";cin>>n;for (int i=2;i<=n/2;i++){if(n%i==0){cout<<"no is not prime";goto start;}}cout<<"no is prime";start:cout<<"u waana further continue(Y/y)";cin>>ch;}while(ch=='y'||ch=='Y');getch();}

37. PROGRAM TO CHECK WHETHER THE ENTERED NUMBER IS PALINDROME OR NOT#include<iostream.h>#include<conio.h>void main(){clrscr();int n,m,i,rem,rev=0;cout<<"enter the required number";cin>>n;m=n;while(m>0){rem=m%10;m=m/10;rev=rev*10+rem;}cout<<"reverse of number is"<<rev<<"\n";if(rev==n){cout<<"number is palindrome";}else{cout<<"number is not palindrome";}getch();}

38. PROGRAM TO FIND LEAP YEAR#include<iostream.h>

25

Page 26: basics of c++

#include<conio.h>void main(){int n;cout<<"enter the year";cin>>n;if(n%4==0){cout<<"entered year is leap year";}else{cout<<"the entered year is not leap year";}getch();}

39. PROGRAM TO INTERCHANGE TWO NUMBERS#include<iostream.h>#include<conio.h>void main(){clrscr();int a,b;cout<<"enter the value of a and b";cin>>a>>b;a=a+b;b=a-b;a=a-b;cout<<"\n"<<"after interchanging value of"<<"a="<<a<<"\n"<<"b="<<b;getch();}

40. GREATER BETWEEN THREE NUMBERS USING TERNARY OPERATOR#include<iostream.h>#include<conio.h>void main(){clrscr();int a,b,c,d;cout<<"enter three no.";cin>>a>>b>>c;d=((a>b&&a>c)?a:(b>a&&b>c)?b:c);cout<<"greater no is"<<d;getch();}

41. FINDS THE FACTORIAL OF THE GIVEN NUMBER#include<iostream.h>#include<conio.h>void main(){clrscr();int i,n,fact=1;cout<<"enter the no. whose factorial is to be find";

26

Page 27: basics of c++

cin>>n;for(i=1;i<=n;i++){fact=fact*i;}cout<<"factorial of number is"<<fact;getch();}

42. PROGRAM OF FUNCTION OVERLOADING#include<iostream.h>#include<conio.h>#include<math.h>void area(int);void area(int,int);void area(int,int,int);void main(){clrscr();float a,b,c,le,br,side;cout<<"enter the side of square";cin>>side;cout<<"enter length and breadth of rectangle";cin>>le>>br;cout<<"enter parameter a,b,c of traingle";cin>>a>>b>>c;area(side);area(le,br);area(a,b,c);getch();}void area(int x){int a;a=x*x;cout<<"area of square is"<<a<<"\n";}void area(int x,int y){int b;b=x*y;cout<<"area of rectangle is"<<b<<"\n";}void area(int x,int y,int z){float s=(x+y+z)/2;float ar;ar=sqrt(s*(s-x)*(s-y)*(s-z));cout<<"area of triangle using HERO formula is"<<ar;}

43. PROGRAM TO FIND THE FACTORIAL USING THE FUNCTION#include<iostream.h>#include<conio.h>

27

Page 28: basics of c++

void main(){clrscr();int fact(int);int n,i,k;cout<<"enter any number";cin>>n;k=fact(n);cout<<"factorial of number is= "<<k;getch();}int fact(int x){int fact=1;if(x==0||x==1){cout<<"factorial of number is"<<fact;}else{for(int i=2;i<=x;i++){fact=fact*i;}}return(fact);}

44. C++ PROGRAM ARRANGE THE ELEMENTS OF AN ARRAY IN ASCENDING ORDER. #include<iostream.h>#include<conio.h>void sort(int a[],int n);void main(){clrscr();int n,a[10];cout<<"enter number of elements in array";cin>>n;cout<<"\n enter the value of elements of array";for(int i=0;i<n;i++){cin>>a[i];}cout<<"enter value of elements of array are"<<"\n";for(i=0;i<n;i++){cout<<"a["<<i<<"]="<<a[i]<<"\n";}cout<<"elements in ascending order are"<<"\n";sort(a,n);getch();}void sort(int x[],int y)

28

Page 29: basics of c++

{for(int i=0;i<y;i++){for(int j=0;j<y;j++){if(x[i]<x[j]){int temp=x[i];x[i]=x[j];x[j]=temp;}}}for(i=0;i<y;i++){cout<<x[i]<<"\n";}}

29