0% found this document useful (0 votes)
53 views

Program 1

The document contains 12 programs demonstrating various programming concepts in C++. Program 1 displays the factorial of a number using a for loop. Program 2 displays the Fibonacci series up to a given number using a while loop. Program 3 compares two numbers using an if statement. Program 4 determines if a number is even or odd using an if-else statement.

Uploaded by

ankush
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Program 1

The document contains 12 programs demonstrating various programming concepts in C++. Program 1 displays the factorial of a number using a for loop. Program 2 displays the Fibonacci series up to a given number using a while loop. Program 3 compares two numbers using an if statement. Program 4 determines if a number is even or odd using an if-else statement.

Uploaded by

ankush
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 47

PROGRAM-1

AIM:-Program to display the factorial of a number using FOR loop

#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int x,y=1;

cout<<"enter the number whose factorial is to be found=";

cin>>x;

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

y=y*i;

cout<<"Factorial="<<y;

getch();

Page 1
PROGRAM-2

AIM:- Program to display Fibonacci series up to number using WHILE loop

#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int a=1,b=2,c=0,d;

cout<<"enter the value=";

cin>>d;

cout<<"Fibonacci series:"<<a<<" "<<b<<" ";

while(c<=d)

c=a+b;

cout<<c;

a=b;

b=c;

cout<<" ";

c++;

getch();

Page 2
Page 3
PROGRAM-3

AIM:-Program to find which number is greater using IF statement

#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int x,y;

cout<<"value of x:";

cin>>x;

cout<<"value of y:";

cin>>y;

if(x>y)

cout<<"x is greater than y";

if(y>x)

cout<<"y is greater than x";

getch();

Page 4
PROGRAM-4

AIM:-Program to find whether number is odd or even using IF-ELSE statement.

#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int n;

cout<<"number=";

cin>>n;

if(n%2==0)

cout<<"number is even";

else

cout<<"number is odd";

getch();

Page 5
PROGRAM-5

AIM:-Program to find which number is greater using nested IF-ELSE statement.

#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int x,y,z;

cout<<"value of x:";

cin>>x;

cout<<"value of y:";

cin>>y;

cout<<”value of z:";

cin>>z;

if(x>y&&x>z)

cout<<"x is greater than y and z";

else if(b>a&&b>c)

cout<<"y is greater than x and z";

else

cout<<"z is greater than x and y";

getch();

Page 6
Page 7
PROGRAM-6

AIM:-Program to show the use of operator overloading of operator + on complex numbers

#include<iostream.h>

#include<conio.h>

class complex

int i,r;

public:

void input()

cout<<"real part:";

cin>>r;

cout<<"imaginary part:";

cin>>i;

complex operator +(complex c)

complex t;

t.r=r+c.r;

t.i=i+c.i;

return(t);

void output()

Page 8
cout<<r<<"+i"<<i<<endl;

};

void main()

clrscr();

complex c1,c2,c3;

c1.input();

c2.input();

cout<<"complex c1:";

c1.output();

cout<<"complex c2:";

c2.output();

cout<<"complex c3:";

c3=c1+c2;

c3.output();

getch();

Page 9
PROGRAM-7

AIM:-Program to explain the concept of function overloading.

#include<iostream.h>

#include<conio.h>

class data

public:

int input(int x,int y)

cout<<"value of x:"<<x<<endl;

cout<<"value of y:"<<y<<endl;

long input(long a,long b)

cout<<"value of a:"<<a<<endl;

cout<<"value of b:"<<b<<endl;

cin>>b;

};

void main()

clrscr();

data d;

d.input(3,5);

Page 10
d.input(5656,6554);

getch();

Page 11
PROGRAM-8

AIM:-Program to add two complex numbers using default constructor.

#include<iostream.h>

#include<conio.h>

class complex

int r1,r2,i1,i2,r,i;

public:

complex()

r1=4;

r2=3;

i1=5;

i2=6;

void getdata()

cout<<"Complex 1:-"<<r1<<"+i"<<i1<<endl;

cout<<"Complex 2:-"<<r2<<"+i"<<i2<<endl;

r=r1+r2;

i=i1+i2;

void display()

Page 12
cout<<"Sum "<<r<<"+i"<<i;

};

void main()

clrscr();

complex c1;

c1.getdata();

c1.display();

getch();

Page 13
PROGRAM-9

AIM:-Program to subtract two complex numbers using parameterized constructor.

#include<iostream.h>

#include<conio.h>

class complex

int r1,r2,i1,i2,r,i;

public:

complex(int a,int b,int c,int d)

r1=a;

r2=b;

i1=c;

i2=d;

void getdata()

cout<<"Complex no.1:-"<<r1<<"+i"<<i1<<endl;

cout<<"Complex no.2:-"<<r2<<"+i"<<i2<<endl;

r=r1-r2;

i=i1-i2;

void display()

Page 14
cout<<"difference "<<r<<"+i"<<i;

};

void main()

clrscr();

complex c(7,3,8,6);

c.getdata();

c.display();

getch();

Page 15
PROGRAM-10

AIM:-Program to display name and roll number of student using single inheritance.

#include<iostream.h>

#include<conio.h>

class student

protected:

char n[10];

public:

void getdata()

cout<<"name of student:";

cin>>n;

};

class rollnumber:public student

long r;

public:

void roll()

cout<<”roll number of "<<n<<":";

cin>>r;

Page 16
void displaydata()

cout<<"Name-"<<n<<endl<<"Roll No.-"<<r;

};

void main()

clrscr();

rollnumber r1;

r1.getdata();

r1.roll();

clrscr();

r1.displaydata();

getch();

Input:

Output:

Page 17
PROGRAM-11

AIM:-Program to show result of a student using multilevel inheritance.

#include<iostream.h>

#include<conio.h>

class student

protected:

char n[10];

public:

void getdata()

cout<<"name of student:";

cin>>n;

};

class marks:public student

protected:

int mar[5],total;

public:

void getmarks();

};

void marks::getmarks()

Page 18
cout<<"marks of "<<name<<":";

total=0;

for(int i=0;i<5;i++)

cin>>mar[i];

total=total+mar[i];

class result:public marks

float avg;

public:

void getresult()

avg=total/5;

cout<<"average marks of "<<name<<" is "<<avg;

};

void main()

clrscr();

result r;

r.getdata();

r.getmarks();

Page 19
r.getresult();

getch();

Page 20
PROGRAM-12

AIM:-Program to add two complex numbers using multiple inheritance.

#include<iostream.h>

#include<conio.h>

class complexb

protected:

int r1,i1;

public:

void getdata()

cout<<"real part of first complex no.:";

cin>>r1;

cout<<"imaginary part of first complex no.:";

cin>>i1;

cout<<"Complex no.1:"<<r1<<"+i"<<i1<<endl;

};

class compd

protected:

int r2,i2;

public:

void get()

Page 21
{

cout<<"real part of second complex no.:";

cin>>r2;

cout<<"imaginary part of second complex no.:";

cin>>i2;

cout<<"Complex no.2:"<<r2<<"+i"<<i2<<endl;

};

class add:public complex,public comp

int r,i;

public:

void display()

r=r1+r2;

i=i1+i2;

cout<<"sum"<<r<<"+i"<<i;

};

void main()

clrscr();

add a;

a.getdata();

Page 22
a.get();

a.display();

getch();

Page 23
PROGRAM-13

AIM:-Program to display the result of any student using multilevel inheritance of virtual base
class.

#include<iostream.h>

#include<conio.h>

class student

protected:

char n[10];

public:

void getdata()

cout<<”name of student:";

cin>>n;

};

class marks:virtual public student

protected:

int m[5],total;

public:

void getmarks();

};

void marks::getmarks()

{
Page 24
cout<<”marks of "<<name<<":";

total=0;

for(int i=0;i<5;i++)

cin>>m[i];

total=total+mar[i];

class result:virtual public marks

float avg;

public:

void getresult()

avg=total/5;

cout<<"average marks of "<<name<<" is "<<avg;

};

void main()

clrscr();

result r;

r.getdata();

r.getmarks();

Page 25
r.getresult();

getch();

PROGRAM-14

Page 26
AIM:-Program to display the result of a student using hierarchal inheritance.

#include<iostream.h>

#include<conio.h>

class student

protected:

char n[10];

public:

void getdata()

cout<<"name of student:";

cin>>n;

};

class rollno:virtual public student

protected:

long r;

public:

void getroll()

cout<<"roll number of student:";

cin>>r;

Page 27
};

class marks: virtual public student

protected:

int m[5],total;

public:

void getmarks();

};

void marks::getmarks()

cout<<"marks of "<<name<<":";

total=0;

for(int i=0;i<5;i++)

cin>>mar[i];

total=total+mar[i];

class result:public marks,public rollno

float avg;

public:

void getresult()

Page 28
avg=total/5;

cout<<"average marks of "<<rn<<" is "<<avg;

};

void main()

clrscr();

result r;

r.getdata();

r.getroll();

r.getmarks();

r.getresult();

getch();

PROGRAM-15

Page 29
Aim:-Program to find address of variable using pointers.

#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int a,*i;

cout<<"Enter the value of a: ";

cin>>a;

i=&a;

cout<<"Memory address of "<<a<<" is: "<<i;

getch();

PROGRAM-16

Page 30
AIM:-Program to add two numbers and copy it to another variable using copy constructor.

#include<iostream.h>

#include<conio.h>

class add

int a,b;

public:

add(int x, int y)

a=x;

b=y;

add(add &i)

a=i.a;

b=i.b;

void showdata();

};

void add::showdata()

cout <<"Value of a:"<<a<<endl<<"Value of b:"<<b<<endl;

cout<<"Sum of a and b is: "<<a+b;

Page 31
void main()

clrscr();

add a(3,6);

add a1(a);

a.showdata();

getch();

Page 32
PROGRAM-17

Aim:- Program to show multiple constructors.

#include<iostream.h>

#include<conio.h>

class add

int a,b;

public:

add()

a=0;

b=0;

add(int x, int y)

a=x;

b=y;

add(add &i)

a=i.a;

b=i.b;

void showdata();

Page 33
};

void add::showdata()

cout<<"Sum is: "<<a+b;

void main()

clrscr();

add a1;

add a2(5,7);

add a3(a2);

a1.showdata();

a2.showdata();

a3.showdata();

getch();

Page 34
PROGRAM-18

AIM:-Program to display employee name and salary using friend function.

#include<iostream.h>

#include<conio.h>

class salary;

class employee

char name[10];

public:

friend void getdata(employee,salary);

};

class salary

int slr;

public:

friend void getdata(employee,salary);

};

void getdata(employee e,salary s1)

cout<<"Enter the name of employee:";

cin>>e.name;

cout<<"Enter the salary of "<<e.name<<":";

cin>>s1.slr;

Page 35
void main()

clrscr();

employee e;

salary s1;

getdata(e,s1);

getch();

Page 36
PROGRAM-19

AIM:- WAP to demonstrate the arithmetic operation after assigning the value of two variable.

#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int a,b,c,d,e,f;

cout<<"Enter the value of a and b:";

cin>>a>>b;

c=a+b;

cout<<"Sum: "<<c<<endl;

d=a*b;

cout<<"Multiplication:"<<d<<endl;

e=a/b;

cout<<"Divide:"<<e<<endl;

f=a-b;

cout<<"Subtract:"<<f;

getch();

Page 37
PROGRAM-20

AIM:-Program to show the concept of destructor.

#include<iostream.h>

#include<conio.h>

class add

int a,b;

public:

add()

x=7;

cout<<”Value of x=”<<x<<endl;

y=3;

cout<<”Value of y=”<<y<<endl;

~add()

x=0;

cout<<”Value of x=”<<x<<endl;

y=0;

cout<<”Value of y=”<<y<<endl;

};

Page 38
void main()

clrscr();

add a;

getch();

Page 39
PROGRAM-21

AIM:-Program to illustrate the concept of friend function,

#include<iostream.h>

#include<conio.h>

class xyz;

class abc

int a;

public:

friend void getdata(abc,xyz);

};

class xyz

int x;

public:

friend void getdata(abc,xyz);

};

void getdata(abc a1,xyz x1)

int temp;

cout<<"Enter the value of a:";

cin>>a1.a;

cout<<"Enter the value of x:";

Page 40
cin>>x1.x;

temp=a1.a+x1.x;

cout<<"Sum="<<temp;

void main()

clrscr();

abc a1;

xyz x1;

getdata(a1,x1);

getch();

Page 41
PROGRAM-22

AIM:-Program to swap two numbers by call by value.

# include<iostream.h>

#include<conio.h>

void swap(int,int);

void main()

clrscr();

int a=2,b=4;

cout<<"BEFORE SWAPPING"<<endl;

cout<<"Value of a:"<<a<<endl<<"Value of b:"<<b<<endl;

cout<<"AFTER SWAPPING"<<endl;

swap(a,b);

cout<<"AFTER SWAPPING"<<endl;

cout<<"Value of a:"<<a<<endl<<"Value of b:"<<b<<endl;

getch();

void swap(int x,int y)

int t;

t=x;

x=y;

y=t;

Page 42
cout<<"Value of a:"<<x<<endl<<"Value of b:"<<y<<endl;

Page 43
PROGRAM-23

AIM:-Program to swap two numbers by call by reference.

# include<iostream.h>

#include<conio.h>

void swap(int&,int&);

void main()

clrscr();

int a=2,b=4;

cout<<"BEFORE SWAPPING"<<endl;

cout<<"Value of a:"<<a<<endl<<"Value of b:"<<b<<endl;

cout<<"AFTER SWAPPING"<<endl;

swap(a,b);

cout<<"AFTER SWAPPING"<<endl;

cout<<"Value of a:"<<a<<endl<<"Value of b:"<<b<<endl;

getch();

void swap(int &x,int &y)

int t;

t=x;

x=y;

y=t;

Page 44
cout<<"Value of a:"<<x<<endl<<"Value of b:"<<y<<endl;

Page 45
PROGRAM-24

AIM:-Program to show the use of this pointer.

#include<iostream.h>

#include<conio.h>

class example

private:

int a;

public:

void getdata(int b)

this->a=b;

void showdata()

cout<<"Value of a="<<this->a<<endl;

cout<<"Memory address of a is "<<this<<endl;

};

void main()

clrscr();

example e;

Page 46
e.getdata(7);

e.showdata();

getch();

Page 47

You might also like