c++ project file
c++ project file
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,i,count=0;
cout<<"Enter the number\n";
cin>>n;
for(i=2;i<n;i++)
{
if(n%i==0)
{
count++;
}
}
if(count>0)
{
cout<<"Number is not prime.\n";
}
else
{
cout<<"Number is prime.";
}
getch();
}
Output-
RUN-1
Enter the number
3
Number is prime.
RUN-2
Enter the number
8
Number is not prime.
#include<iostream.h>
#include<conio.h>
class complex
{
int a,b;
public:
void setdata(int x,int y)
{
a=x;
b=y;
}
void showdata()
{
cout<<"a = "<<a<<” , ”<<"b = "<<b<<endl;
}
void sum(complex x,complex y)
{
a=x.a+y.a;
b=x.b+y.b;
cout<<"Sum of complex number is:"<<endl;
cout<<"a="<<a<<” , ”<<"b="<<b;
}
};
void main()
{
complex c1,c2,c3;
c1.set.data(2,3);
c1.showdata();
c2.setdata(4,5);
c2.showdata();
c3.sum(c1,c2);
getch();
}
OUTPUT-
a=2, b=3
a=4 , b=5
Sum of complex number is:
a=6, b=8
#include<iostream.h>
#include<conio.h>
class ABC
{
int sum;
public:
int add(int a,int b)
{
sum=a+b;
return sum;
}
int add(int a,int b,int c)
{
sum=a+b+c;
return sum;
}
};
void main()
{
clrscr();
ABC x;
cout<<"Sum of number 2 and 4 is = "<<x.add(2,4)<<endl;
cout<<"Sum of number 1,3 and 5 is = "<<x.add(1,3,5);
getch();
}
OUTPUT-
#include<iostream.h>
#include<conio.h>
class integer
{
int x,y;
public:
void setdata(int a,int b)
{
x=a;
y=b;
}
void showdata()
{
cout<<"the value of x= "<<x<<endl;
cout<<"the value of y= "<<y<<endl;
}
OUTPUT-
the value of x= 9
the value of y= 7
the value of x= 3
the value of y= 4
After subtract :
the value of x= 6
the value of y= 3
#include<iostream.h>
#include<conio.h>
class integer
{
int x,y;
public:
void setdata(int a,int b)
{
x=a;
y=b;
}
void showdata()
{
cout<<"the value of x= "<<x<<endl;
cout<<"the value of y= "<<y<<endl;
}
integer operator +(integer i) //binary operator
{
integer temp;
temp.x=x+i.x;
temp.y=y+i.y;
return(temp);
}
};
void main()
{
clrscr();
integer i1,i2,i3;
i1.setdata(9,7);
i2.setdata(3,4);
i1.showdata();
i2.showdata();
cout<<"After addition : "<<endl;
i3=i1+i2; //binary operator
i3.showdata();
getch();
}
OUTPUT-
the value of x= 9
the value of y= 7
the value of x= 3
the value of y= 4
After addition :
the value of x= 12
the value of y= 11
#include<iostream.h>
#include<conio.h>
class A
{
int a;
public:
A() // default constructor
{
a=0;
cout<<"a = "<<a<<endl;
}
A(int b) //parameterized constructor
{
a=b;
cout<<"a = "<<a<<endl;
}
void main()
{
clrscr();
A x;
A y(2);
A z(y);
getch();
}
OUTPUT-
a=0
a=2
a=2
#include<iostream.h>
#include<conio.h>
class student
{
protected:
int roll;
public:
void get_roll(int a)
{
roll=a;
}
void show_roll()
{
cout<<"Roll no = "<<roll<<endl;
}
};
class test
{
protected:
int sub1,sub2;
public:
void get_marks(int x,int y)
{
sub1=x;
sub2=y;
}
void show_marks()
{
cout<<"Sub1="<<sub1<<endl;
cout<<"Sub2="<<sub2<<endl;
}
};
void main()
{
clrscr();
result student1;
OUTPUT-
Roll no = 1
Sub1=90
Sub2=94
Total=184
#include<iostream.h>
#include<conio.h>
class A
{
protected :
int a1,b1;
public:
virtual void setdata(int x1,int y1) //virtual function
{
a1=x1;
b1=y1;
}
virtual void showdata() //virtual function
{
cout<<"a1="<<a1<<endl<<"b1="<<b1<<endl;
}
};
class B:public A
{
protected :
int a2,b2;
public:
void setdata(int x2,int y2)
{
a2=x2;
b2=y2;
}
void showdata()
{
cout<<"a2="<<a2<<endl<<"b2="<<b2<<endl;
}
};
void main()
{
A *p,obj1;
p=&obj1;
(*p).setdata(3,6);
(*p).showdata();
B obj2;
p=&obj2;
(*p).setdata(13,16);
(*p).showdata();
getch();
}
OUTPUT-
a1=3
b1=6
a2=13
b2=16
#include<iostream.h>
#include<conio.h>
class book
{
int id;
int price;
int page;
public:
book(int i,int pr,int p)
{
id=i;
price=pr;
page=p;
}
~book()
{
cout<<"book id = "<<id<<endl
<<"book price = "<<price<<endl
<<"book pages = "<<page<<endl;
}
};
void main()
{
clrscr();
{
book b(1001,285,4560);
}
getch();
}
OUTPUT-
book id = 1001
book price = 285
book pages = 4560
#include<iostream.h>
#include<conio.h>
class swapping
{
int a,b,temp;
public:
void swap(int &x,int &y)
{
temp=x;
x=y;
y=temp;
}
};
void main()
{
clrscr();
int a=5,b=7;
swapping b1;
cout<<"Before swapping:\n"
<<"a="<<a<<endl
<<"b="<<b<<endl;
b1.swap(a,b);
cout<<"After swapping:\n"
<<"a="<<a<<endl
<<"b="<<b<<endl;
getch();
}
OUTPUT-
Before swapping:
a=5
b=7
After swapping:
a=7
b=5
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int m,n,d,r,sum=0;
cout<<"Enter the number\n";
cin>>n;
m=n;
while(n>0)
{
d=n/10;
r=n%10;
sum=sum+(r*r*r);
n=d;
}
if(m==sum)
{
cout<<"Number is armstrong\n";
}
else
{
cout<<"Number is not armstrong";
}
getch();
}
OUTPUT-
RUN-1
RUN-2
#include<iostream.h>
#include<conio.h>
class student
{
protected:
int roll;
public:
void get_roll(int a)
{
roll=a;
}
void show_roll()
{
cout<<"Roll no = "<<roll<<endl;
}
};
class test:public student
{
protected:
int sub1,sub2;
public:
void get_marks(int x,int y)
{
sub1=x;
sub2=y;
}
void show_marks()
{
cout<<"Sub1="<<sub1<<endl;
cout<<"Sub2="<<sub2<<endl;
}
};
class result:public test
{
int total;
public:
void show_result()
{
total=sub1+sub2;
cout<<"Total="<<total;
}
};
void main()
{
clrscr();
result student1;
student1.get_roll(1);
student1.show_roll();
student1.get_marks(92,97);
student1.show_marks();
student1.show_result();
getch();
}
Roll no = 1
Sub1=92
Sub2=97
Total=189