In: Computer Science
Please use C++
1. A Sequence of numbers and a sum is given as input. List all combinations of numbers that can add upto the given Sum.
User input:-
Enter numbers: - 1, 3,7,9,11
Enter a sum :- 20
Output:
11+9 = 20
1+3+7+9 = 20
2. Print absolute prime numbers between a given range:-
Enter Range - 2,99
For eg Prime numbers are:-
Prime numbers ==> 2,3,5,7,11,13,17,19,23
Absolute Prime numbers ==> 2,3,5,7,11,23
3. Write an encoder and decoder program, which should give us options to encode/decode?
Do you want to encode or decode? [e,d]
>>e
Enter string to encode :- "Hello How are you?"
Encoded string: "SDFAF@sfdfrtre"
Do you want to encode or decode? [e,d]
>>d
Enter string to decode:- "SDFAF@sfdfrtre"
Decoded string: "Hello How are you?"
1. #include<bits/stdc++.h>
using namespace std;
void unique (int l, int s, int k, vector<int>& local, vector<int>& a)
{
if (s==k)
{
cout<<"{";
for(int i=0; i< local.size();i++){
if(i!=0)
cout<<", ";
cout<<local[i];
if(i!= local.size()-1)
cout<<" , ";
}
cout<<" }"<<endl;
return;
}
for(int i=1; i < a.size(); i++){
if(s +a[i] > k)
continue;
if(i and a[i] == a[i -1] and i >l)
continue;
local.push_back(a[i]);
unique(i+1, sum + a[i], k, local, a);
local.pop_back();
}
}
void com(vector<int> a, int k)
{
sort(a.begin(), a.end());
vector<int> local;
unique(0,0,k,local,a);
}
int main()
{
vector<int> a={10,1,2,7,6,1,5};
int k= 8;
com(a,k);
return(0);
}
2) #include<upstream.h>
using namespace std;
void prime(int n)
{
int d=0;
for(int i=2; i<= n/2; i++)
{
if(n%i == 0)
d++;
}
if(d==1)
cout<<n<<endl;
}
int main()
{
cout<<" Enter range";
int l,u;
cin>>l>>u;
cout<<" Prime nos between " <<l<<" and "<<" are:"<<endl;
for(int i=l;i<= u; i++)
prime(i);
return 0;
}
3)