Sei sulla pagina 1di 47

AIM: WRITE A PROGRAM TO ILLUSTRATE THE USE OF COUT

PROGRAM:
#include <iostream>

#include<conio.h>

using namespace std;

int main() {

cout<<"hello this is a program to show the use of cout";

getch();

return 0;

}:

OUTPUT:

Diksha Yaduwanshi Page 1


18100BTBDAI02883
Diksha Yaduwanshi Page 2
18100BTBDAI02883
AIM: WRITE A PROGRAM TO ILLUSTRATE THE USE OF CIN

PROGRAM:

#include <iostream>

#include<conio.h>

using namespace std;

int main() {

int a;

cout<<"write a number";

cin>>a;

cout<<"your enetred number is"<<a;

getch();

return 0;

Diksha Yaduwanshi Page 3


18100BTBDAI02883
OUTPUT:

Diksha Yaduwanshi Page 4


18100BTBDAI02883
AIM: WRITE A PROGRAM TO GET THE SUM OF TWO NUMBERS

PROGRAM:

#include <iostream>

#include<conio.h>

using namespace std;

int main() {

int a,b,c;

cout<<"write two numbers\n";

cin>>a>>b;

c=a+b;

cout<<"your added number is\n"<<c;

getch();

return 0;

Diksha Yaduwanshi Page 5


18100BTBDAI02883
OUTPUT:

Diksha Yaduwanshi Page 6


18100BTBDAI02883
AIM: WRITE A PROGRAM TO GET THE AVERAGE OF N NUMBERS

PROGRAM:
#include<iostream>

#include<conio.h>

using namespace std;

int main()

int i,n,Sum=0,numbers;

float Average;

cout<<"\nPlease Enter How many Number you want?\n";

cin>>n;

cout<<"\nPlease Enter the elements one by one\n";

for(i=0;i<n;++i)

cin>>numbers;

Sum = Sum +numbers;

Average = Sum/n;

cout<<"\nSum of the "<<n<<" Numbers = "<< Sum;

cout<<"\nAverage of the"<<n<<" Numbers = "<< Average;

return 0;

Diksha Yaduwanshi Page 7


18100BTBDAI02883
}

OUTPUT:

Diksha Yaduwanshi Page 8


18100BTBDAI02883
AIM: WRITE A PROGRAM TO SHOW THE USE OF IF ELSE STATEMENTS

PROGRAM:
#include<iostream>

#include<conio.h>

using namespace std;

int main(){

int num;

cout<<"enetr a number";

cin>>num;

if( num < 50 ){

cout<<"num is less than 50";

else {

cout<<"num is greater than or equal 50";

return 0;

Diksha Yaduwanshi Page 9


18100BTBDAI02883
OUTPUT:

Diksha Yaduwanshi Page 10


18100BTBDAI02883
AIM: WRITE A PROGRAM TO SHOW THE USE OF IF ELSE LADDER

PROGRAM:
#include <iostream>

using namespace std;

int main ()

int marks;

cout<<"write your marks\n";

cin>>marks;

if( marks >= 80 )

cout << "U are 1st class !!" << endl;

else if( marks >= 60 && marks < 80)

cout << "U are 2nd class !!" << endl;

else if( marks >= 40 && marks < 60)

{ cout << "U are 3rd class !!" << endl; }

else

cout << "U are fail !!" << endl;

Diksha Yaduwanshi Page 11


18100BTBDAI02883
return 0; }

OUTPUT:

Diksha Yaduwanshi Page 12


18100BTBDAI02883
AIM: WRITE A PROGRAM TO SHOW THE USE OF SWITCH CASE

PROGRAM:
#include <iostream>

#include<conio.h>

using namespace std;

int main()

int ch;

cout<<"Enter any number (1 to 7)";

cin>>ch;

switch(ch)

case 1:

cout<<"Today is Monday";

break;

case 2:

cout<<"Today is Tuesday";

break;

case 3:

Diksha Yaduwanshi Page 13


18100BTBDAI02883
cout<<"Today is Wednesday";

break;

case 4:

cout<<"Today is Thursday";

break;

case 5:

cout<<"Today is Friday";

break;

case 6:

cout<<"Today is Saturday";

break;

case 7:

cout<<"Today is Sunday";

break;

default:

cout<<"Only enter value 1 to 7";

getch();

Diksha Yaduwanshi Page 14


18100BTBDAI02883
OUTPUT:

Diksha Yaduwanshi Page 15


18100BTBDAI02883
AIM: WRITE A PROGRAM TO SHOW THE USE OF FOR LOOP

PROGRAM:

#include <iostream>

using namespace std;

int main()

int i, n, factorial = 1;

cout << "Enter a positive integer: ";

cin >> n;

for (i = 1; i <= n; ++i) {

factorial *= i;

cout<< "Factorial of "<<n<<" = "<<factorial;

return 0;

Diksha Yaduwanshi Page 16


18100BTBDAI02883
OUTPUT:

Diksha Yaduwanshi Page 17


18100BTBDAI02883
AIM: WRITE A PROGRAM TO SHOW THE USE OF WHILE LOOP

PROGRAM:

#include <iostream>

using namespace std;

int main()

int number, i = 1, factorial = 1;

cout << "Enter a positive integer: ";

cin >> number;

while ( i <= number) {

factorial *= i; //factorial = factorial * i;

++i;

cout<<"Factorial of "<< number <<" = "<< factorial;

return 0;

Diksha Yaduwanshi Page 18


18100BTBDAI02883
OUTPUT:

Diksha Yaduwanshi Page 19


18100BTBDAI02883
AIM: WRITE A PROGRAM TO SHOW THE USE OF DO WHILE LOOP

PROGRAM:

#include <iostream>

#include<conio.h>

using namespace std;

int main()

int n,i=1,s=0;

cout <<"Enter n:";

cin >> n;

do

s=s+i;

i++;

}while (i<=n);

cout <<"Sum = "<<s;

getch();

return 0;

Diksha Yaduwanshi Page 20


18100BTBDAI02883
OUTPUT:

Diksha Yaduwanshi Page 21


18100BTBDAI02883
AIM: WRITE A PROGRAM TO PRINT FIBONACCI SERIES

PROGRAM:
#include <iostream>

using namespace std;

int main()

int n, t1 = 0, t2 = 1, nextTerm = 0;

cout << "Enter the number of terms: ";

cin >> n;

cout << "Fibonacci Series: ";

for (int i = 1; i <= n; ++i)

if(i == 1)

cout << " " << t1;

continue;

if(i == 2)

cout << t2 << " ";

continue;

Diksha Yaduwanshi Page 22


18100BTBDAI02883
}

nextTerm = t1 + t2;

t1 = t2;

t2 = nextTerm;

cout << nextTerm << " ";

return 0;

OUTPUT:

Diksha Yaduwanshi Page 23


18100BTBDAI02883
AIM: WRITE A PROGRAM TO CHECK WHETHER A NUMBER IS
PALINDROME

PROGRAM:
#include <iostream>

using namespace std;

int main()

int n, num, digit, rev = 0;

cout << "Enter a positive number: ";

cin >> num;

n = num;

do

digit = num % 10;

rev = (rev * 10) + digit;

num = num / 10;

} while (num != 0);

Cout << " The reverse of the number is: " << rev << endl;

if (n == rev)

cout << " The number is a palindrome.";

else

cout << " The number is not a palindrome.";


Diksha Yaduwanshi Page 24
18100BTBDAI02883
return 0;

OUTPUT:

Diksha Yaduwanshi Page 25


18100BTBDAI02883
AIM: WRITE A PROGRAM TO CHECK ARMSTRONG NUMBER

PROGRAM:
#include <iostream>

using namespace std;

int main()

int origNum, num, rem, sum = 0;

cout << "Enter a positive integer: ";

cin >> origNum;

num = origNum;

while(num != 0)

rem = num % 10;

sum += rem * rem * rem;

num /= 10;

} if(sum == origNum)

cout << origNum << " is an Armstrong number.";

else

cout << origNum << " is not an Armstrong number.";

return 0;

Diksha Yaduwanshi Page 26


18100BTBDAI02883
OUTPUT:

Diksha Yaduwanshi Page 27


18100BTBDAI02883
AIM: WRITE A PROGRAM TO ILLUSTRATE THE USE FUNCTIONS IN C++

PROGRAM:
#include <iostream>

using namespace std;

int add(int, int);

int main()

int num1, num2, sum;

cout<<"Enters two numbers to add: ";

cin >> num1 >> num2;

sum = add(num1, num2);

cout << "Sum = " << sum;

return 0;

int add(int a, int b)

int add;

add = a + b;

return add;

Diksha Yaduwanshi Page 28


18100BTBDAI02883
OUTPUT:

Diksha Yaduwanshi Page 29


18100BTBDAI02883
AIM: WRITE A PROGRAM TO ILLUSTRATE THE USE SINGLE INHERITANCE

PROGRAM:
#include<iostream>

#include<conio.h>

using namespace std;

class shape //base class

public:

void setWidth(int w)

width=w;

void setHeight(int h)

height= h;

protected:

int width;

int height;

};

class rectangle:public shape //derived class

Diksha Yaduwanshi Page 30


18100BTBDAI02883
{

public:

int getArea()

return(width*height);

};

int main(void)

rectangle r;

r.setWidth(6);

r.setHeight(8);

cout<<"area of rectangle= "<<r.getArea()<<endl;

getch();

return 0;

Diksha Yaduwanshi Page 31


18100BTBDAI02883
OUTPUT:

Diksha Yaduwanshi Page 32


18100BTBDAI02883
AIM: WRITE A PROGRAM TO ILLUSTRATE THE USE MULTIPLE
INHERITANCE

PROGRAM:
#include<iostream>

#include<conio.h>

using namespace std;

class area

public:

void setLength(int l)

length=l; }

void setBreadth(int b)

breadth=b; }

protected:

int length;

int breadth;

};

class perSQCost

public:
Diksha Yaduwanshi Page 33
18100BTBDAI02883
int getCost(int area)

return area*150; }

};

class room:public area,public perSQCost //derived class

public:

int getArea()

return(length*breadth); }};

int main(void)

room r;

int area;

r.setLength(5);

r.setBreadth(7);

area=r.getArea();

cout<<"area of room= "<<r.getArea()<<endl;

cout<<"cost of room= Rs. "<<r.getCost(area)<<endl;

getch();

return 0;}

Diksha Yaduwanshi Page 34


18100BTBDAI02883
OUTPUT:

Diksha Yaduwanshi Page 35


18100BTBDAI02883
AIM: WRITE A PROGRAM TO ILLUSTRATE THE USE MULTI LEVEL
INHERITANCE

PROGRAM:
#include <iostream>

using namespace std;

class base

public:

int x;

void getdata()

cout << "Enter value of x= "; cin >> x;

};

class derive1 : public base

public:

int y;

void readdata()

cout << "\nEnter value of y= "; cin >> y;

}
Diksha Yaduwanshi Page 36
18100BTBDAI02883
};

class derive2 : public derive1

private:

int z;

public:

void indata()

cout << "\nEnter value of z= "; cin >> z;}

void product()

cout << "\nProduct= " << x * y * z;}

};

int main()

derive2 a;

a.getdata();

a.readdata();

a.indata();

a.product();

return 0;}

Diksha Yaduwanshi Page 37


18100BTBDAI02883
OUTPUT:

Diksha Yaduwanshi Page 38


18100BTBDAI02883
AIM: WRITE A PROGRAM TO ILLUSTRATE THE USE HIERARCHICAL
INHERITANCE

PROGRAM:
#include <iostream>

using namespace std;

class A

public:

int x, y;

void getdata()

cout << "\nEnter value of x and y:\n"; cin >> x >> y;

};

class B : public A

public:

void product()

cout << "\nProduct= " << x * y;

}
Diksha Yaduwanshi Page 39
18100BTBDAI02883
};

class C : public A

public:

void sum()

cout << "\nSum= " << x + y;

};

int main()

B obj1;

C obj2;

obj1.getdata();

obj1.product();

obj2.getdata();

obj2.sum();

return 0;

Diksha Yaduwanshi Page 40


18100BTBDAI02883
OUTPUT:

Diksha Yaduwanshi Page 41


18100BTBDAI02883
AIM: WRITE A PROGRAM TO ILLUSTRATE THE USE HYBRID
INHERITANCE

PROGRAM:
#include<iostream>

#include<conio.h>

using namespace std;

class stu{

int id;

char name[20];

public:

void getstu(){

cout << "Enter stuid, name";

cin >> id >> name;}

};

class marks: public stu{

protected:

int m, p, c;

public:

void getmarks(){

cout << "Enter 3 subject marks:";

cin >> m >> p >> c;}

};
Diksha Yaduwanshi Page 42
18100BTBDAI02883
class sports{

protected:

int spmarks;

public:

void getsports(){

cout << "Enter sports marks:";

cin >> spmarks;}

};

class result : public marks, public sports{

int tot;

float avg;

public :

void show(){

tot=m+p+c;

avg=tot/3.0;

cout << "Total=" << tot << endl;

cout << "Average=" << avg << endl;

cout << "Average + Sports marks =" << avg+spmarks;}

};

int main(){

result r;

Diksha Yaduwanshi Page 43


18100BTBDAI02883
r.getstu();

r.getmarks();

r.getsports();

r.show();

getch();

OUTPUT:

Diksha Yaduwanshi Page 44


18100BTBDAI02883
AIM: WRITE A PROGRAM TO ILLUSTRATE THE USE FILE HANDLING

PROGRAM:
#include <iostream>

#include <fstream>

using namespace std;

int main()

fstream file;

file.open("samplee.txt",ios::out);

if(!file)

cout<<"Error in creating file!!!"<<endl;

return 0;

cout<<"File created successfully."<<endl;

file<<"ABCD";

file.close();

file.open("sample.txt",ios::in);

if(!file)

cout<<"Error in opening file!!!"<<endl;

Diksha Yaduwanshi Page 45


18100BTBDAI02883
return 0;

char ch;

cout<<"File content: ";

while(!file.eof())

file>>ch;

cout<<ch;

file.close();

return 0;

Diksha Yaduwanshi Page 46


18100BTBDAI02883
OUTPUT:

Diksha Yaduwanshi Page 47


18100BTBDAI02883

Potrebbero piacerti anche