//This program is written to show the function "switch" command. //The program provides an option for the user to choose between defined operations such as calculating the average or multiplication of given numbers. #include #include void main() { int sum,i,x; long int mul; char op,ans; float ave; ave=0; sum=0; i=0; // *** The Main Body *** // begin: clrscr(); printf("Choose the operation that you need from the following list:\n"); printf("1.Sum and Average\n2.Multiplication\n3.Factorial\n"); printf("Please enter the related number to the operation you need:\n"); op=getch(); // scanf("%c",&op); /* we can use the "scanf" command intead of "getch". But this will make some problems in the execution procedure. The scanf" command, bufer the inputs and put them each by each whenever the program needs an entry from the user. You can check these problems simply by replacing all of "getch" commands with "scanf" */ switch(op) { case'1': printf("\nThe Switch is set for calculating sum and average.\n"); printf("Enter your numbers and enter \"0\" when you finished enetering numbers.\n"); sum=0; i=0; do { scanf("%d",&x); sum=sum+x; ++i; } while (x!=0); --i; ave=(float)sum/i; printf("Your sum is: %d and the average of the entered numbers is: %f",sum,ave); break; case'2': printf("\nThe switch is set for calculating the multiplication of your numbers.\n"); printf("Enter your numbers and enter \"1\" when you finished entering numbers.\n"); mul=1; do { scanf("%d",&x); mul=mul*x; } while (x!=1); printf("Your multiplication is %d",mul); break; case'3': printf("Sorry this part of the program is not written yet."); break; default: printf("\nerror! You can only enter a number between 1 and 3"); } //End of Switch answer: printf("\n\ndo you want to continue? (y/n)"); ans=getch(); //scanf("%c",&ans); if(ans=='y') { printf("\n"); goto begin; } if(ans=='n') goto end; else { printf("\nYou must answer with \"y\" or \"n\"\n"); goto answer; } end: clrscr(); gotoxy(27,10); printf("***May Allah protect you***"); getch(); }