Program To Implement Binary Search in A Linear Array
Program To Implement Binary Search in A Linear Array
Output
Enter the size of array 5 Enter element no. 1 3 Enter element no. 2 2 Enter element no. 3 5 Enter element no. 4 4 Enter element no. 5 1 Sorted array is 12345 Enter the element to be searched 5 No. present at location 3
Output
Enter the size of array 5 Enter element no. 1 3 Enter element no. 2 2 Enter element no. 3 5 Enter element no. 4 4 Enter element no. 5 1 Enter the element to be searched 5 No. present at location 3
3. Program to find the area of circle,rectangle and square using function overloading
#include<iostream.h> #include<conio.h> void area(float r) { cout<<"The area of circle is "<<3.14*r*r<<" sq units\n"; } void area(float l,float b) { cout<<"The area of rectangle is "<<l*b<<" sq units\n"; } void area(double l) { cout<<"The area of square is "<<l*l<<" sq units\n"; } void area(double b,double p) { cout<<"The area of triangle is "<<0.5*b*p<<" sq units\n"; } void area(long double d1,long double d2) { cout<<"The area of rhombus is "<<0.5*d1*d2<<" sq units\n"; } void main() { int choice; float a,b; double c,d; long double e,f; start: clrscr(); cout<<"MENU"<<"\n"; cout<<"1. Circle"<<"\n"; cout<<"2. Reactangle"<<"\n"; cout<<"3. Square"<<"\n"; cout<<"4. Triangle"<<"\n"; cout<<"5. Rhombus"<<"\n"; cout<<"Enter your choice"<<"\n"; cin>>choice; switch(choice) { case 1: cout<<"Enter the radius"<<"\n"; cin>>a; area(a); break; case 2: cout<<"Enter the length and breadth"<<"\n"; cin>>a>>b; area(a,b);
break; case 3: cout<<"Enter the side"<<"\n"; cin>>c; area(c); break; case 4: cout<<"Enter the base and perpendicular"<<"\n"; cin>>c>>d; area(c,d); break; case 5: cout<<"Enter the diagonals 1 and 2"<<"\n"; cin>>e>>f; area(e,f); break; default: goto start; } }
Output
MENU 1. Circle 2. Reactangle 3. Square 4. Triangle 5. Rhombus Enter your choice 1 Enter the radius 3 The area of circle is 28.26 sq units
Output
Enter the size of array 5 Enter element no. 1 3 Enter element no. 2 2 Enter element no. 3 5 Enter element no. 4 4 Enter element no. 5 1 Sorted array is 12345
5. Recursive Program to calculate fatorial of the no. entered by user and print fibonacci series
#include<iostream.h> #include<conio.h> void main() { label: clrscr(); int choice,i,num1,num2,num3,num4,a=0,large; long int fact=1; char ch; label1: cout<<"CALCULATOR"<<"\n"; cout<<"1. Factorial"<<"\n"; cout<<"2. Fibonacci"<<"\n"; cout<<"3. Prime Number"<<"\n"; cout<<"4. Largest"<<"\n"; cout<<"Enter your choice"<<"\n"; cin>>choice; if(choice<1||choice>4) { cout<<"Wrong choice. Try again"<<"\n"; goto label1; } else { switch(choice) { case 1: cout<<"Enter the number"<<"\n"; cin>>num1; for(i=num1;i>0;i--) { fact=fact*i; } cout<<"The factorial of "<<num1<<" is "<<fact<<"\n"; break; case 2: cout<<"Enter the term number upto which the series is to be calculated"<<"\n"; cin>>num1; num2=0; num3=1; num4=0; cout<<num2<<" "<<num3<<" "; for(i=0;i<num1-2;i++) { num4=num2+num3; cout<<num4<<" "; num2=num3; num3=num4; } break;
case 3: cout<<"Enter a number"<<"\n"; cin>>num1; for(i=2;i<=num1/2;i++) { if(num1%i==0) { cout<<"The number isn't prime"<<"\n"; a=1; break; } } if(a==0) cout<<"The number is prime"<<"\n"; break; case 4: cout<<"Enter the three numbers"<<"\n"; cin>>num2>>num3>>num4; if(num2>num3&&num2>num4) large=num2; else if(num3>num2&&num3>num4) large=num3; else large=num4; cout<<"The largest is "<<large<<"\n"; break; } } cout<<"Do you want to continue"<<"\n"; cin>>ch; if(ch=='y'||ch=='Y') goto label; }
Output
CALCULATOR 1. Factorial 2. Fibonacci 3. Prime Number 4. Largest Enter your choice 1 Enter the number 3 Factorial of 3 is 6 Do you want to continue n
Output
Enter the size of array 5 Enter the size of array 5 Enter element no. 1 3 Enter element no. 2 2 Enter element no. 3 5 Enter element no. 4 4 Enter element no. 5 1 The array in increasing order is 12345
Output
Enter the size of array 5 Enter the size of array 5 Enter element no. 1 3 Enter element no. 2 2 Enter element no. 3 5 Enter element no. 4 4 Enter element no. 5 1 The array in increasing order is 12345
Output
m=15 n=16
default: goto label; } cout<<"Do you want to continue"<<"\n"; cin>>ch; if(ch=='y'||ch=='Y') goto label; }
Output
1.PUSH 2.POP Enter your choice 2 The stack is empty. Can't pop Do you want to continue y 1.PUSH 2.POP Enter your choice 1 Enter the element to be pushed 3 Do you want to continue y 1.PUSH 2.POP Enter your choice 2 The popped element is 3 Do you want to continue n
cout<<queue[i]<<' '; } int queue[3]; int rear=-1; int front=-1; void main() { int n,choice; char ch; label: cout<<"MENU"<<"\n"; cout<<"1. Insertion"<<"\n"; cout<<"2. Deletion"<<"\n"; cout<<"3. Display"<<"\n"; cout<<"Enter your choice"<<"\n"; cin>>ch; switch(choice) { case 1: cout<<"Enter the no. to be inserted"<<"\n"; cin>>n; insert(n); break; case 2 : delete(); break; case 3: display(); break; default: goto label; } cout<<"Do you want to continue?"<<"\n"; cin>>ch; if(ch=='y'||ch=='Y') goto label; }
Output
MENU 1. Insertion 2. Deletion 3. Display Enter your choice 2 UNDERFLOW!! Queue is empty Do you want to continue? Y MENU
1. Insertion 2. Deletion 3. Display Enter your choice 1 Enter the no. to be inserted 42 Do you want to continue? Y MENU 1. Insertion 2. Deletion 3. Display Enter your choice 2 42 Deleted Do you want to continue? N