Professional Documents
Culture Documents
C Files
C Files
int main()
{ int a=1;
cout<< "The value of a was"<< a<<"\n";
cout<< "The value of a++ is"<< a++ <<"\n";
cout<<"The value of a is"<< a<<"\n";
a=1;
cout<< "The value of a was"<< a<<"\n";
cout<< "The value of ++a is"<< ++a <<"\n";
cout<<"The value of a is"<< a<<"\n";
a=1;
cout<< "The value of a was"<< a<<"\n";
cout<< "The value of a-- is"<< a-- <<"\n";
cout<<"The value of a is"<< a<<"\n";
return 0;
}
int main()
{
int n1 = 1;
int n2 = 2;
bool b;
b = (n1 == n2);
cout<< “The value of b is “<<b; //returns 0 for false and 1 for true
return 0;
}
int main()
{
bool b;
float f1 = 10.0;
float f2 = 100 % 30;
//b=(f1 == f2);
b= f1==f2;
cout<< “The value of b is “<<b; //returns 0 for false and 1 for true
return 0;
}
int main()
{
unsigned int a = 60; // 60 = 0011 1100
unsigned int b = 13; // 13 = 0000 1101
int c = 0;
c = a | b; // 61 = 0011 1101
cout << “Line 2 – Value of c is: “ << c << endl ;
c = a ^ b; // 49 = 0011 0001
cout << “Line 3 – Value of c is: “ << c << endl ;
int main()
{ int a=5;
int b=2;
float c;
c=(float)a/b; //we have to typecast any one var a or b. Only float c will give
ans 2
cout<<c;
return 0;
}
Function
#include <iostream>
using namespace std;
int addNum(int x ,int y); //Function Prototyping
int main()
{
cout<< “The sum is” <<addNum(30,79);
return 0;
}
int addNum(int x ,int y){
int sum = x+y;
return sum;
}
#include <iostream>
using namespace std;
//void small(int x, int y); //correct
//void small(int ,int);// correct
int main()
{
int a,b;
void small(int ,int); //correct
cout<< "Enter 2 numbers"<<endl;
cin>>a>>b;
small(a,b);
return 0;
}
void small(int x, int y)
{
if (x<y)
cout<<"The smaller number is " <<x;
if (x>y)
cout<<"The smaller number is "<< y;
}
Inline function
To eliminate the cost of function calls CPP has inline functions. An inline
Function is a func. that is expanded when invoked. Thus the compiler
replaces the corresponding function call with the function code.
#include <iostream>
using namespace std;
inline float mul(float x, float y)
{
return(x*y);
}
int main()
{
float a=2.98;
float b=8.97;
cout<<mul(a,b)<<endl;
}
//remove the inline and check first time execution takes more time…
check the second execution time with inline and third without inline.
Recursion
Function calls itself
#include <iostream>
using namespace std;
long fact(int n){
if (n==0)
return 1;
else
return(n*fact(n-1));
}
int main()
{
int num;
cout<<"Enter a positive integer"<<endl;
cin>>num;
cout<<"The fact is"<<fact(num)<<endl;
}
#include <iostream>
int volume(int);
double volume(double, int);
long volume(long, int, int);
int main()
{
cout<<"The volume of a cube"<<volume(10)<<endl;
cout<<"The volume of a cylinder "<<volume(2.5,8)<<endl;
cout<<"The volume of a rectangular box "<<volume(100,5,8)<<endl;
return 0;
}
int volume(int a){
return(a*a*a); ///cube
}
int main()
{
cout<< " Enter one number between 0 and 39"<<"\n";
cin>>a;
b=a/10;
switch (b)
{
case 0:
cout<<"You have entered a number between 0 and 9";
break;
case 1:
cout<<"You have entered a number between 10 and 19";
break;
case 2:
cout<<"You have entered a number between 20 and 29";
break;
case 3:
cout<<"You have entered a number between 30 and 39";
break;
default:
cout<<"The number is not in the range";
}
return 0;
}
Difference between SWITCH and NESTED IF
If---evaluates expr to be true, for each cond. one if st.
Switch---evaluated as a result of expr., various values are
treated as cases
If ---may or may not be int
Switch—int values.
//Logical operators
#include <iostream>
using namespace std;
int main()
{
int a, b, c;
cout<<"Enter three values";
cin>>a>>b>>c;
if ((a>c)&&(a>b)) // a>c is evaluated first.
{
cout << "a is greatest";
}
else if (b>c){
cout << "b is greatest";
}
else
{
cout << "c is greatest";
}
return 0;
}
if ((a==0 )|| (b==0)|| (c==0)){
cout<<"The product is zero.";
}
return 0;
}
//Loops
While loop:
#include <iostream>
// a>c is evaluated first.
using namespace std;
int main()
{
int x=0;
while (x<10){
cout<<x<<"\n";
x++;
}
return 0;
}
Do While loop:
#include <iostream>
using namespace std;
int main()
{
int x=0;
int y=0;
do{
y=y+x;
cout<<y<<"\n";
x++;
}
while (x<10);
return 0;
}
//For Loop:
#include <iostream>
using namespace std;
int main()
{
for (int x =0; x<=10; x++){
cout<<x<<"\n";
}
return 0;
}
Break and Continue:
int main()
{
int i, no, sum = 0;
for(i= 0; i<= 5; i++){
cin>>no;
if(no<0)
continue; //continue with the next iteration.
else
sum+=no;
}
cout<<"The Sum of positive no is"<<sum;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int i, no, sum = 0;
for(i= 0; i<= 5; i++){
cin>>no;
if(no<0)
break;
else
sum+=no;
}
cout<<"The Sum of positive no is"<<sum;
return 0;
}
Experiment No: 1
1. Convert F to C
2. Take width, length from user and calculate area
3. To draw A, Z by **
4. Find the sq. roots of an eqn.
5. Write a function to find square of a number.
6. Write a function to find cube root of a number.
7. Using if else if find the number which is greater from three numbers
(Take the input from the user)
8. Write a program to check the age of employee between 20-60.
9. Write a program to find a/b + c/d ---a, b, c, d are integers taken from
user. E.g.1,2,3,4
10. Write a program to find a given number even and odd take number
from user.
11. To find the sum of digits of a number.
12. To find the first 15 prime numbers.
13. To print Pascal’s triangle up to four rows.
14. Take a three-digit no. from the user. If the mid digit is sum of the
other two digits, then return an appropriate sentence with the number.
15. To find the divisibility of a number by both 3 and 7.
ARRAYS
POINTERS
CLASSES:
The binding of data and data and functions together in cpp in a single
data type class is called encapsulation.
#include <iostream>
using namespace std;
class myclass{
public:
void myfunction()
{
cout <<" We are happy, we are learning";
}
};
int main()
{
myclass myclassObject;
myclassObject.myfunction();
return 0;
}
#include <iostream>
#include <string>
using namespace std;
class myclass{
public:
void setName(string s){
name=s;
}
string getName(){
return name;
}
private:
string name;
};
int main()
{
myclass myclassObject;
myclassObject.setName("EXTC" );
cout<<myclassObject.getName();
return 0;
}
// C++ program to demonstrate private access modifier
#include<iostream>
using namespace std;
class Circle
{
// private data member
private:
double radius;
// public member function
public:
double compute_area()
{ // member function can access private
// data member radius
return 3.14*radius*radius;
}
};
// main function
int main()
{
// creating object of the class
Circle obj;
// trying to access private data member
// directly outside the class
obj.radius = 1.5;
cout << "Area is:" << obj.compute_area();
return 0;
}
const Member functions
If a member function does not alter any data in the class, then we may
declare it as a const member function as follows:
void mul (int, int) const;
double get_balance ( ) const;
The qualifier const is appended both in function declaration and
definition. The compiler will generate an error message if those functions
try to alter the data values.
Constructors