switch with function in C++

इस page पर, हम C ++ में switch with function पर चर्चा करेंगे,

जैसा कि हम जानते हैं कि हमने C ++ में प्रोग्राम साइज़ को कम करने के लिए function का उपयोग किया  जाता है, इस program में switch statement का उपयोग करके menu बनाते हुए देखें

नीचे दिए गए program में, हरेक case statement में एक function defined करते हैं-

int get_sum();  // case 1 
int get_sub();  // case 2
int get_mul();  // case 3

प्रत्येक case statement एक अलग task perform करेगा।

यहाँ नीचे इसका program दिया गया है-

switch with function program in C++

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>  // exit(0)

int num1, num2, result;  // global variable declaration

 int get_sum();
 int get_sub();
 int get_mul();

  int get_sum()
    {
       cout<<"Enter any two number: ";
       cin>>num1>>num2;

       result = num1 + num2;
       return result;
    }
   int get_sub()
    {
       cout<<"Enter any two number: ";
       cin>>num1>>num2;

       result = num1 - num2;
       return result;
    }
   int get_mul()
    {
       cout<<"Enter any two number: ";
       cin>>num1>>num2;

       result = num1 * num2;
       return result;
    }
 void main()
  {
    clrscr();
    int select;
    cout<<"1.Addition\n";
    cout<<"2.Subtraction\n";
    cout<<"3.Multiply\n";
    cout<<"0.Exit";

    cout<<"\nEnter Your Choice: ";
    cin>>select;
    cout<<endl;

 switch(select)
  {
    case 1:
            get_sum();
            cout<<"\nTotal: "<<result;
            break;

    case 2:
            get_sub();
            cout<<"\nSubtraction: "<<result;
            break;

    case 3:
            get_mul();
            cout<<"\nMultiply: "<<result;
            break;

    case 0: exit(0);

    default:
             cout<<"\ninvalid choice....";
  }

getch();
}

OUTPUT

1.Addition 
2.Subtraction 
3.Multiply 
0.Exit 

Enter Your choice: 1 

Enter First Number: 5 
Enter Second Number: 8 

Total: 13

हमेशा को Do Less and Get More follow करें। अर्थात ,

Example of switch with function in C++

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>  // exit(0)

 int get_num1();
 int get_num2();

 int result;

  int get_num1()
    {
       int num1;
       cout<<"\nEnter First Number: "; cin>>num1;

       return num1;
    }

  int get_num2()
    {
       int num2;
       cout<<"\nEnter Second Number: "; cin>>num2;

       return num2;
    }


 void main()
  {
    clrscr();
    int choice;
    cout<<"1.Addition\n";
    cout<<"2.Subtraction\n";
    cout<<"3.Multiply\n";
    cout<<"0.Exit";

    cout<<"\nEnter Your Choice: "; cin>>choice;

 switch(choice)
  {
  case 1:

	 result = get_num1()+get_num2();
	 cout<<"\nTotal: "<<result;
	 break;

  case 2:
	 result = get_num1()-get_num2();
	 cout<<"\nSubtraction: "<<result;
	 break;

  case 3:
	 result = get_num1()*get_num2();
	 cout<<"\nMultiply: "<<result;
	 break;

  case 0:
	 exit(0);

  default:
	  cout<<"\ninvalid choice....";
 }

getch();
}

OUTPUT

1.Addition 
2.Subtraction 
3.Multiply 
0.Exit 

Enter Your choice: 2 
Enter First Number: 5 

Enter Second Number: 8 

Substrection: -3

क्या आपको यहाँ पर explanation की आवश्यकता है ?

Leave a Reply

Your email address will not be published. Required fields are marked *