Answer to Question #293949 in C for Prathamesh

Question #293949

Write a menu-driven program that asks the user to select a favorite color from the given colors. Keep asking the user three times for selecting the color from the list of the available colors. Do this with the help of a suitable loop and switch statement. Finally, you have to display the three favorite colors of the user.


1
Expert's answer
2022-02-06T02:16:10-0500
#include <stdio.h>
#include <string>
#include <stdlib.h>


void main(){
	int ch;
	char selectedColors[3][100];
	int count=0;


	do{
		printf("1. Red\n");
		printf("2. Yellow\n");
		printf("3. White\n");
		printf("4. Green\n");
		printf("5. Blue\n");
		printf("6. Black\n");
		printf("7. Orange\n");


		printf("Select color: ");
		scanf("%d",&ch);


		switch(ch){
		case 1:
			strcpy(selectedColors[count],"Red");
			count++;
			break;
		case 2:
			strcpy(selectedColors[count],"Yellow");
			count++;
			break;
		case 3:
			strcpy(selectedColors[count],"White");
			count++;
			break;
		case 4:
			strcpy(selectedColors[count],"Green");
			count++;
			break;
		case 5:
			strcpy(selectedColors[count],"Blue");
			count++;
			break;
		case 6:
			strcpy(selectedColors[count],"Black");
			count++;
			break;
		case 7:
			strcpy(selectedColors[count],"Orange");
			count++;
			break;
		}


	}while(count<3);


	printf("\nSelected colors:\n");
	for(int i=0;i<count;i++){
		printf("%s\n",selectedColors[i]);
	}
	scanf("%d",&ch);
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS