Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include<cmath>
- using namespace std;
- long long Factorial(int a){
- if(a == 1) return 1;
- else return a * Factorial(a-1);
- }
- long long Prime(int a){
- if(a == 1)return 0;
- for(int i = 2;i<=sqrt(a);i++){
- if(a%i == 0) return 0;
- }
- return 1;
- }
- long long OddEven(int a){
- return (a%2 == 0)?1:0;
- }
- long long Operate(int a, long long (*fp)(int)){
- return (*fp)(a);
- }
- int main()
- {
- int op,in;
- bool flag=true;
- cout << "Function Pointer" <<endl;
- do{
- cin >> op;
- if(op == 4){
- break;
- }else{
- cin >> in;
- switch(op){
- case 1:
- cout << Operate(in,Factorial) << endl;
- break;
- case 2:
- if(Operate(in,Prime) == 0) cout << "Not Prime" <<endl;
- else cout << "Prime" << endl;
- break;
- case 3:
- if(Operate(in,OddEven) == 0) cout << "Odd" << endl;
- else cout << "Even" << endl;
- break;
- }
- }
- }while(flag);
- flag = true;
- cout << "Lambda Expression" << endl;
- do{
- cin >> op;
- if(op == 4){
- return 0;
- }else{
- cin >> in;
- switch(op){
- case 1:{
- auto fact = [in](){
- int res = 1;
- for(int i=2; i<=in; i++){
- res*=i;
- }
- return res;
- };
- cout << "Result : " << fact() << endl;
- break;
- }
- case 2:{
- auto prime = [in](){
- if(in == 1)
- return 0;
- for(int i = 2;i<=sqrt(in);i++){
- if(in%i == 0)
- return 0;
- }
- return 1;
- };
- cout << (prime() == 0 ? "Not Prime" : "Prime") << endl;
- break;
- }
- case 3:{
- auto even = [in](){
- return in % 2 == 0 ? 1 : 0;
- };
- cout << (even() == 1 ? "Even" : "Odd") << endl;
- break;
- }
- }
- }
- }while(flag);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement