In this program we will learn how to combine 6 C++ program in a single C++ program and run them as per user desire.
The code is given below
//Code Starts
#include <iostream>
#include<math.h>
#include<conio.h>
using namespace std;
int main()
{
int a,b,num,dig,sum,sub,mul;
float rem,div;
char op;
cout<<" Enter 1 to Perform Methamatical Operation "<<endl;
cout<<" Enter 2 to search a prime number in given range "<<endl;
cout<<" Enter 3 to Perform Binary Seartch in Array"<<endl;
cout<<" Enter 4 for Starric Program"<<endl;
cout<<" Enter 5 for Factorial"<<endl;
cout<<" Enter 6 to Check Even or odd Number"<<endl;
abc: cin>>num;
cout<<"------- Program Number : "<<num<<"-----------------"<<endl;
if(num==1)
{
cout<<"Enter '+' for Sum, or '-' for Subtraction, or '/' for Division, or '*' for Multiplication, or '%'for Remainder "<<endl;
cin>>op;
switch(op)
{
case'+':
cout<<"Enter 1st Number : " ;
cin>>a;
cout<<"Enter 2nd Number : " ;
cin>>b;
sum=a+b;
cout<<"Sum = "<<sum;
break;
case'-':
cout<<"Enter 1st Number : " ;
cin>>a;
cout<<"Enter 2nd Number : " ;
cin>>b;
sub=a-b;
cout<<"Subtraction = "<<sub;
break;
case'*':
cout<<"Enter 1st Number : " ;
cin>>a;
cout<<"Enter 2nd Number : " ;
cin>>b;
mul=a*b;
cout<<"Multiplication = "<<mul;
break;
case'/':
cout<<"Enter 1st Number : " ;
cin>>a;
cout<<"Enter 2nd Number : " ;
cin>>b;
div=a/b;
cout<<"Divide = "<<div;
break;
case'%':
cout<<"Enter 1st Number : " ;
cin>>a;
cout<<"Enter 2nd Number : " ;
cin>>b;
rem=a%b;
cout<<"Remainder = "<<rem;
break;
}
}
else if(num==2)
{
int c,d;
int a=0,b=0;
cout<<"ENTER STARTING RANGE TO CHECK PRIME NUMBER"<<endl;
cin>>c;
cout<<"ENTER ENDING RANGE TO CHECK PRIME NUMBER"<<endl;
cin>>d;
for(int i=c;i<=d;i++)
{
for(int j=2;j<=sqrt(i);j++)
{
if(i%j==0)
a++;
}
if(a==0&&i!=0)
{
b++;
cout<<"PRIME Number => "<<i<<endl;
a=0;
}
a=0;
}
cout<<"RANGE OF PRIME NUMBER IN BETWEEEN "<<c<<" TO "<<d<<" = "<<b;
}
else if(num==3)
{
int a[100],n,i,beg,end,mid,item;
cout<<"\n------------ BINARY SEARCH ------------ \n\n";
cout<<"Enter No. of Elements= ";
cin>>n;
cout<<"\nEnter Elements in Sorted Order=\n";
for(i=1;i<=n;i++)
{
cin>>a[i];
}
cout<<"\nEnter Item you want to Search= ";
cin>>item;
beg=1;
end=n;
mid=(beg+end)/2; // Find Mid Location of Array
while(beg<=end && a[mid]!=item) // Compare Item and Value of Mid
{
if(a[mid]<item)
beg=mid+1;
else
end=mid-1;
mid=(beg+end)/2;
}
if(a[mid]==item)
{
cout<<"\nData is Found at Location : "<<mid;
}
else
{
cout<<"Data is Not Found";
}
}
else if(num==4)
{
int a;
cout<<"Enter anY number"<<endl;
cin>>a;
for(int i=1;i<=a;i++)
{
for(int j=1;j<=i;j++)
{
cout<<"*";
}
cout<<endl;
}
}
else if(num==5)
{
int num,factorial=1;
cout<<" Enter Number To Find Its Factorial: ";
cin>>num;
for(int a=1;a<=num;a++)
{
factorial=factorial*a;
}
cout<<"Factorial of Given Number is ="<<factorial<<endl;
}
else if(num==6)
{
int min,max;
{
cout << "Enter the minimum range: "<<endl;
cin >> min;
cout << "Enter the maximum range: "<<endl;
cin >> max;
cout << "Odd numbers in given range are: "<<endl;
for(int num=min;num <= max;num++)
if(num % 2 !=0)
cout << num<< " "<<endl;
cout<<"Even numbers in given range are: "<<endl;
for(int even=min;even <= max;even++)
if(even % 2 ==0)
cout << even << " "<<endl;
}
}
else
{
cout<<"Invalid Entry enter again with number 1-6 only : "<<endl;
goto abc;
}
getch();
}
//Code Ends
OUTPUT:
The code is given below
//Code Starts
#include <iostream>
#include<math.h>
#include<conio.h>
using namespace std;
int main()
{
int a,b,num,dig,sum,sub,mul;
float rem,div;
char op;
cout<<" Enter 1 to Perform Methamatical Operation "<<endl;
cout<<" Enter 2 to search a prime number in given range "<<endl;
cout<<" Enter 3 to Perform Binary Seartch in Array"<<endl;
cout<<" Enter 4 for Starric Program"<<endl;
cout<<" Enter 5 for Factorial"<<endl;
cout<<" Enter 6 to Check Even or odd Number"<<endl;
abc: cin>>num;
cout<<"------- Program Number : "<<num<<"-----------------"<<endl;
if(num==1)
{
cout<<"Enter '+' for Sum, or '-' for Subtraction, or '/' for Division, or '*' for Multiplication, or '%'for Remainder "<<endl;
cin>>op;
switch(op)
{
case'+':
cout<<"Enter 1st Number : " ;
cin>>a;
cout<<"Enter 2nd Number : " ;
cin>>b;
sum=a+b;
cout<<"Sum = "<<sum;
break;
case'-':
cout<<"Enter 1st Number : " ;
cin>>a;
cout<<"Enter 2nd Number : " ;
cin>>b;
sub=a-b;
cout<<"Subtraction = "<<sub;
break;
case'*':
cout<<"Enter 1st Number : " ;
cin>>a;
cout<<"Enter 2nd Number : " ;
cin>>b;
mul=a*b;
cout<<"Multiplication = "<<mul;
break;
case'/':
cout<<"Enter 1st Number : " ;
cin>>a;
cout<<"Enter 2nd Number : " ;
cin>>b;
div=a/b;
cout<<"Divide = "<<div;
break;
case'%':
cout<<"Enter 1st Number : " ;
cin>>a;
cout<<"Enter 2nd Number : " ;
cin>>b;
rem=a%b;
cout<<"Remainder = "<<rem;
break;
}
}
else if(num==2)
{
int c,d;
int a=0,b=0;
cout<<"ENTER STARTING RANGE TO CHECK PRIME NUMBER"<<endl;
cin>>c;
cout<<"ENTER ENDING RANGE TO CHECK PRIME NUMBER"<<endl;
cin>>d;
for(int i=c;i<=d;i++)
{
for(int j=2;j<=sqrt(i);j++)
{
if(i%j==0)
a++;
}
if(a==0&&i!=0)
{
b++;
cout<<"PRIME Number => "<<i<<endl;
a=0;
}
a=0;
}
cout<<"RANGE OF PRIME NUMBER IN BETWEEEN "<<c<<" TO "<<d<<" = "<<b;
}
else if(num==3)
{
int a[100],n,i,beg,end,mid,item;
cout<<"\n------------ BINARY SEARCH ------------ \n\n";
cout<<"Enter No. of Elements= ";
cin>>n;
cout<<"\nEnter Elements in Sorted Order=\n";
for(i=1;i<=n;i++)
{
cin>>a[i];
}
cout<<"\nEnter Item you want to Search= ";
cin>>item;
beg=1;
end=n;
mid=(beg+end)/2; // Find Mid Location of Array
while(beg<=end && a[mid]!=item) // Compare Item and Value of Mid
{
if(a[mid]<item)
beg=mid+1;
else
end=mid-1;
mid=(beg+end)/2;
}
if(a[mid]==item)
{
cout<<"\nData is Found at Location : "<<mid;
}
else
{
cout<<"Data is Not Found";
}
}
else if(num==4)
{
int a;
cout<<"Enter anY number"<<endl;
cin>>a;
for(int i=1;i<=a;i++)
{
for(int j=1;j<=i;j++)
{
cout<<"*";
}
cout<<endl;
}
}
else if(num==5)
{
int num,factorial=1;
cout<<" Enter Number To Find Its Factorial: ";
cin>>num;
for(int a=1;a<=num;a++)
{
factorial=factorial*a;
}
cout<<"Factorial of Given Number is ="<<factorial<<endl;
}
else if(num==6)
{
int min,max;
{
cout << "Enter the minimum range: "<<endl;
cin >> min;
cout << "Enter the maximum range: "<<endl;
cin >> max;
cout << "Odd numbers in given range are: "<<endl;
for(int num=min;num <= max;num++)
if(num % 2 !=0)
cout << num<< " "<<endl;
cout<<"Even numbers in given range are: "<<endl;
for(int even=min;even <= max;even++)
if(even % 2 ==0)
cout << even << " "<<endl;
}
}
else
{
cout<<"Invalid Entry enter again with number 1-6 only : "<<endl;
goto abc;
}
getch();
}
//Code Ends
OUTPUT:
0 comments:
Post a Comment