You have to 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.
#include <stdio.h>
#include <string>
#include <stdlib.h>
void main(){
int ch;
char colors[3][100];
int i=0;
int j;
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(colors[i],"Red");
i++;
break;
case 2:
strcpy(colors[i],"Yellow");
i++;
break;
case 3:
strcpy(colors[i],"White");
i++;
break;
case 4:
strcpy(colors[i],"Green");
i++;
break;
case 5:
strcpy(colors[i],"Blue");
i++;
break;
case 6:
strcpy(colors[i],"Black");
i++;
break;
case 7:
strcpy(colors[i],"Orange");
i++;
break;
}
}while(i<3);
printf("\nSelected colors:\n");
for(j=0;j<i;j++){
printf("%s\n",colors[j]);
}
scanf("%d",&ch);
}
Comments
Leave a comment