You are here:   HomeC Programming (Theory)Control Structures in CC Program for print a name of color that start with a given character
by Dinesh Thakur Category: Control Structures

The program segment given below accepts a character from the keyboard and prints the name of the corresponding color, e. g., if the user enters character R, it prints Red. However, it handles only three colors, namely, red, green and blue.

The character entered from the keyboard is first accepted in variable color (of type char) and is then tested in the switch statement. The program includes three cases corresponding to the colors red, green and blue. Note that the case values are character constants 'R’ ,‘G' and 'B' . Each case includes a printf statement to print the name of the color followed by a break statement. The output of program containing the above code is shown below.

#include <stdio.h>

void main()

{

     char color;

     clrscr();

     printf("Enter character (R/G/B): ");

             color= getchar();

             switch (color)

             {

                       case 'R':

                               printf ("Red") ;

                               break;

                       case 'G':

                               printf("Green");

                               break;

                       case 'B':

                               printf("Blue");

                               break;

             }

                               getch();

}

    C Program for print a name of color that start with a given character



Subscribe To Free Daily Newsletter!

Get Free News Updates Delivered Directly To Your Inbox
About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to contact us.



What's New and Popular





Popular Article