Vous êtes sur la page 1sur 22

INDEX Enrollment no: Subject code:

SNO NAME OF PRACTICAL WAP to show use of bitwise operator. 1 WAP to find out prime number between 1 to 2 3 4 5 6 7 8
100. WAP to calculate factorial of given number.

Date

Pageno. Remark

WAP to Swap two number using call by value. WAP to swap two number using call by reference. WAP to perform sum of two integers, float number using function overloading. WAP to display Fibonacci series. WAP to print following output using Switch:

1) * * * *

2)

1 1 2 1

3)

1 1 2 1 1 2 3 2 1 1 2 1 1

* * 1 2 3 2 1

WAP to create class of student that have following data: Name, roll no, marks, getdata and result. WAP to implement single inheritance. WAP to implement constructor. WAP to implement virtual function.

10 11 12

//1. WAP to show bitwise operator. 1

#include<conio.h> #include<iostream.h> void main() { clrscr(); int num,num1; cout<<"\nenter any two value"; cin>>num>>num1; cout<<"\n"<<num<<"&"<<num1<<":"<<(num&num1); cout<<"\n"<<num<<"|"<<num1<<":"<<(num|num1); cout<<"\n"<<num<<"<<"<<num1<<":"<<(num<<num1); cout<<"\n"<<num<<">>"<<num1<<":"<<(num>>num1); cout<<"\n"<<"!"<<num<<":"<<!num; getch(); }

OUTPUT: enter any two value4 5 4&5:4 4|5:5 4<<5:128 4>>5:0 !4:0

//2.WAP to display prime number between 100. #include<conio.h> 2

#include<iostream.h> void main() { clrscr(); cout<<"PRIME NUMBERS ARE:"; for(int i=1;i<=100;i++){ int j=2; while(j<i) { if(i%j==0) break; else j++; } if(i==j) cout<<i<<","; } getch();

} OUTPUT: PRIME NUMBERS ARE:2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79, 83,89,97,

//3.WAP to calculate factorial of given number. #include<conio.h> #include<iostream.h> 3

void main() { clrscr(); int num; cout<<"\nenter value:"; cin>>num; cout<<"\nfacorial of "<<num<<":"; int fact=1; while(num>1) fact=fact*(num--); cout<<fact; getch(); } OUTPUT: enter value:5 facorial of 5:120

//4.WAP to swap two value using call by value. #include<conio.h> #include<iostream.h> void main() 4

{ clrscr(); int num1,num2; void swap(int,int); cout<<"enter any two value"; cin>>num1>>num2; cout<<"\nBefore swapping values:"<<"\nnum1="<<num1<<"\nnum2="<<num2; swap(num1,num2); cout<<"\nAfter swapping values:"<<"\nnum1="<<num1<<"\nnum2="<<num2; getch(); } void swap(int num1,int num2) { int num3; num3=num1; num1=num2; num2=num3; } OUTPUT: enter any two value5 6 Before swapping values: After swapping values: num1=5 num2=6 num1=5 num2=6

//5.WAP to swap two value using call by value. #include<conio.h> #include<iostream.h> void main() 5

{ clrscr(); int num1,num2; void swap(int*,int*); cout<<"enter any two value"; cin>>num1>>num2; cout<<"\nBefore swapping values:"<<"\nnum1="<<num1<<"\nnum2="<<num2; swap(&num1,&num2); cout<<"\nAfter swapping values:"<<"\nnum1="<<num1<<"\nnum2="<<num2; getch(); } void swap(int *num1,int *num2) { int num3; num3=*num1; *num1=*num2; *num2=num3; } OUTPUT: enter any two value5 6 Before swapping values: After swapping values: num1=5 num2=6 num1=6 num2=5

//6.WAP to perform sum of two integer,float number using function overloading. #include<iostream.h> #include<conio.h> 6

int add(int ,int ); float add(float ,float ); void main() { clrscr(); int x,y; float x1,y1; cout<<"\nenter two integer vlaue"; cin>>x>>y; cout<<"\nsum of int"<<add(x,y); cout<<"\nenter two float vlaue"; cin>>x1>>y1; cout<<"\nsum of float"<<add(x1,y1); getch(); } int add(int x,int y) { return(x+y); } float add(float x1,float y1) { return(x1+y1); }

OUTPUT:

enter two integer vlaue 5 6 7

sum of int11 enter two float vlaue 5.7 7

sum of float12.7

//7.WAP to display fabonacci series. #include<conio.h> #include<iostream.h> void main() { 8

clrscr(); int num; cout<<"\nenter value:"; cin>>num; int x=1; int y=1; cout<<"fabonacci series of "<<num<<":"<<x; for(int i=1;i<num;i++) { cout<<","<<y; int z=x+y; x=y; y=z; } getch(); }

OUTPUT: enter value:7 fabonacci series of 7:1,1,2,3,5,8,13

/*8. WAP to print following output using switch: 1) * ** *** 2) 1 121 12321 3) 1 121 12321 121 1 9

*/ #include<conio.h> #include<iostream.h> void main() { clrscr(); int ch,num; deep: cout<<".............MENU..........."; cout<<"\n1. Triangle of *"; cout<<"\n2. Triangle of num."; cout<<"\n3. Triangle of num in reverse order"; cout<<"\nenter your choice:"; cin>>ch; switch(ch) { case 1: cout<<"\nenter value:"; cin>>num; cout<<"\nTriangle of * "<<num<<":\n";

for(int i=1;i<=num;i++) { for(int j=num-i;j>=1;j--) cout<<" "; for(j=i;j>=1;j--) cout<<" *"; cout<<endl; 10

} break; case 2: cout<<"\nenter value:"; cin>>num; cout<<"Triangle of num "<<num<<":\n"; for(i=1;i<=num;i++) { for(int j=num-i;j>=1;j--) cout<<" "; for(j=1;j<=i;j++) cout<<j; for(j=i-1;j>=1;j--) cout<<j; cout<<endl; } break; case 3: cout<<"\nenter value:"; cin>>num;

cout<<"Triangle of num "<<num<<":\n";

for(i=1;i<=num;i++) { for(int j=num-i;j>=1;j--) cout<<" "; for(j=1;j<=i;j++) cout<<j; 11

for(j=i-1;j>=1;j--) cout<<j; cout<<endl; } for(i=1;i<=num-1;i++) { for(int j=1;j<=i;j++) cout<<" "; for(j=1;j<=num-i;j++) cout<<j; for(j=(num-1)-i;j>=1;j--) cout<<j; cout<<endl; } break; default: cout<<"ur choice is invailid select vailid choice"; goto deep; }

getch(); }

OUTPUT: .............MENU........... 1. Triangle of * 2. Triangle of num. 3. Triangle of num in reverse order enter your choice:3 12

enter value:7 Triangle of num 7: 1 121 12321 1234321 123454321 12345654321 1234567654321 12345654321 123454321 1234321 12321 121 1

//9. WAP to create class of student that have following data: Name, roll no, marks, getdata and result. #include<conio.h> #include<iostream.h> class Student { int enroll,marks[3]; char name[15]; public: void getinfor(); 13

void setinfor(); }; void Student:: setinfor() { cout<<"\nenter student name"; cin>>name; cout<<"\nenter rollnumber"; cin>>enroll; for(int i=0;i<3;i++) cin>>marks[i]; } void Student:: getinfor() { cout<<" \nstudent name:"<<name; cout<<"\nrollnumber:"<<enroll; cout<<"\ntotal:";

int sum=0; for(int i=0;i<3;i++) sum=sum+marks[i]; cout<<sum; float avg=sum/3.0; cout<<"\nresult:"<<avg<<"%"; } void main() { clrscr(); Student s1; s1.setinfor(); 14

s1.getinfor(); getch(); } OUTPUT: enter student name anshu enter rollnumber34 35 67 88 student name:anshu rollnumber:34 total:190 result:63.333332%

//10. WAP to implement single inheritance #include<iostream.h> #include<conio.h> class Base { public: int num; void getinput() { cout<<"enter value:"; cin>>num; } }; 15

class Derived:public Base { public: void fact() { int fact=1; for(int i=1;i<=num;i++) fact=fact*i; cout<<"factorial of"<<num<<":"<<fact; } }; void main() { clrscr();

Derived d; d.getinput(); d.fact(); getch(); } OUTPUT: enter value:5 factorial of5:120

16

//11.WAP to implement constructor #include<conio.h> #include<iostream.h> class Student { int enroll,marks[3]; char name[15]; public: Student(); //contructor void getinfor(); }; Student:: Student() //constructor definition { cout<<"\nenter student name"; cin>>name; 17

cout<<"\nenter rollnumber"; cin>>enroll; for(int i=0;i<3;i++) cin>>marks[i]; } void Student:: getinfor() { cout<<" \nstudent name:"<<name; cout<<"\nrollnumber:"<<enroll; cout<<"\ntotal:"; int sum=0; for(int i=0;i<3;i++)

sum=sum+marks[i]; cout<<sum; float avg=sum/3.0; cout<<"\nresult:"<<avg<<"%"; } void main() { clrscr(); Student s1; s1.getinfor(); getch(); } OUTPUT: enter student namedd

enter rollnumber7 18

66 77 77

student name:dd rollnumber:7 total:220 result:73.333336%

//12. WAP to implement virtual function. #include<conio.h> #include<iostream.h> class Base { protected: int num; public: void setdata() { cout<<"\nenter value num"; cin>>num; } virtual void getdata() { cout<<"\n Area of Square:"<<area(); 19

} int area(); }; int Base::area() { return (num*num); } class Derived :public Base { protected:

int num1,num2; public: void setdata() { cout<<"\nenter value num1 and num2"; cin>>num1>>num2; } void getdata() { cout<<"\n Area of Rectangle:"<<area(); } int area(); }; int Derived::area() { return (num1*num2); } void main() 20

{ clrscr(); Base b; Derived d; Base *ptr; ptr=&b;//pointer point to base class. ptr->setdata(); ptr->getdata(); Derived *ptr1;

ptr1=&d;//pointer point to derived class. ptr1->setdata(); ptr1->getdata(); getch(); } OUTPUT:

enter value num 5 Area of Square: 25 enter value num1 and num2 6 8 Area of Rectangle: 48

21

22

Vous aimerez peut-être aussi