Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 25

#include <iostream>

using namespace std;


int main()
{
cout << "Hello world!" << endl;
return 0;
}
#include <iostream>
int main()
{
std:: cout << "Hello world!\n" ;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
cout << "This is my first \n";
cout<< "C++ Program" ;
return 0;
}
int main()
{ int a;
a=49;
int b=50;
int sum;
sum=a+b;
cout << "The sum is "<< sum;
return 0;
}
int main()
{
int x=10;
float y=12.9;
double const a=8;
char c='z';
//a=10;
x=45;
cout<< "The value of x="<<x<<endl;
cout<< "The value of y="<<y<<"\n";
cout<< "The value of a="<<a<<endl;
cout<< "The value of c="<<c<<endl;
return 0;
}
int main()
{
int a; //OR int a , b, sum;
int b;
int sum;
cout << “Enter First Number :”;
cin >> a;
cout << “Enter Second Number :”;
cin >> b;
sum=a+b;
cout << “The sum is :” <<sum;
return 0;
}
int main()
{
int a,b,c;
cout<< “Enter Three integers\n”;
cin>>a>>b>>c;
cout<<”a=” <<a<< “b=”<<b<<”c=”<<c;
return 0;
}
#include <iostream>
using namespace std;
int main()
{ float x=40;
float y=3;
//int x=40;
//int y=3;
float ans;
ans=x / y;
cout<< “The ans is “<<ans;
return 0;
}
// At least one operand has to be converted to float to get the ans in float.
#include <iostream>
using namespace std;
int main()
{
double x=40;
float y=3;
float ans;
// ans= x / y+30;
//ans= x / (y+30);
//ans= 10.0/ 4 / 2;
ans=x +y*3.0/2;
cout<< “The ans is “<<ans;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int var1 = 10;
int var2 = -var1;
cout<< “The ans is “<<var2;
return 0;
}
int main()
{
int var = 10; // initialize var
//var=var+1;
var++; // now increment it value of var is now 11
cout<< “The ans is “<<var;
return 0;
}
int main()
{
int n1, n2, n3;
// the value of both n1 and n2 is 6
//n1 = 5;
//n2 = ++n1;
// the value of n1 is 6 but the value of n3 is 5
n1 = 5;
n3 = n1++;
//cout<< “The value of n1 is “<<n1<<endl;
//cout<< “The value of n2 is “<<n2;
cout<< “The value of n1 is “<<n1<<endl;
cout<< “The value of n3 is “<<n3;
return 0;
}
int main()
{
int nVariable=5 ;
//nVariable = nVariable + 2;
nVariable += 2;
//nVariable -= 2;
//nVariable *= 2;
//nVariable %= 2;
cout<< “The value of var is “<<nVariable;
return 0;
}
using namespace std;

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; // 12 = 0000 1100


cout << “Line 1 – Value of c is : “ << c << endl ;

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 ;

c = ~a; // -61 = 1100 0011


cout << “Line 4 – Value of c is: “ << c << endl ;

c = a << 2; // 240 = 1111 0000


cout << “Line 5 – Value of c is: “ << c << endl ;

c = a >> 2; // 15 = 0000 1111


cout << “Line 6 – Value of c is: “ << c << endl ;
int main()
{
if (2==2) // Show with different logical operators
{
cout<< “We are learning C++”;
}
return 0;
}
#include <iostream>
using namespace std;
// Using own datatypes
enum Answer {NO=0, FALSE=0,YES=1, OK=1 };
int main()
{
Answer answer;
answer=OK;
cout<<”Ans is “<<answer;
return 0;
}
int main()
{
//int c;
//char c;
//bool c;
//float c;
double c;
cout<< “\t The storage size in bytes” << sizeof(c);
return 0;
}
int main()
{
// SCOPE OF VAR
int x=99;
{
int x=77;
cout<<"The value of x inside:"<<x<< endl ;
}

cout<<"The value of x outside:"<<x;


return 0;
}
int x=10;//global var
int y=45;//global var
int sum(int x, int y){
int s=x+y; //s is local var
}
int main()
{
cout<< "The sum is ="<< sum(x,y) <<endl;
//cout<<s // local var cannot be accessed
return 0;
}
int main()
{
// TYPE CASTING from int to float
int a=22;
float x=3.14;
x+=a; // a is converted to a=22.0
cout<< “x=”<<x;
return 0;
}
int main()
{
// TYPE CASTING from float to int. IT is truncated not rounded.
Float v=3.14;
int x=int(v) ;
cout<< “x=”<<x;
return 0;
}
//Type Promotion:
Char, short, int, float, double
When one is converted to higher type then type case operator is not needed.

using namespace std;

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>

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;
return 0;
}
Function Overloading
#include <iostream>

using namespace std;

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
}

double volume(double r, int h){


return(3.14*r*r*h); /// cylinder
}

long volume(long l, int b, int h){


return(l*b*h); ///rectangular box
}

IF, nested if, switch


using namespace std;
int a;
int b;
int main()
{
cout<< " Enter two numbers"<<"\n";
cin>>a>>b;
if (a>b){
cout<<"a greater than b"<<endl;
}
else if (a==b){
cout<<"a equal to b"<<endl;
}
else{
cout<<"a less than b"<<endl;
}
return 0;
}
int a;
int main()
{
cout<< " Enter a number between 0 and 39 numbers"<<"\n";
cin>>a;
if (a/10==0){
cout<<"The number between 0 to 9"<<endl;
}
else if (a/10==1){
cout<<"The number between 10 to 19"<<endl;
}
else if (a/10==2){
cout<<"The number between 20 to 29"<<endl;
}
else if (a/10==3){
cout<<"The number between 30 to 39"<<endl;
}
else{
cout<<"The number not in range"<<endl;
}
return 0;
}
using namespace std;
int a;
int b;

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

You might also like