Solved Examples · Example 31
Q.Write a C program to accept the colour code for the colours of the rainbow and display its name. The program should be repeatedly executed as long as user choice is 1.
The textbook presents this worked program, transcribed exactly as printed (including its printed errors):
#include <stdio.h>
void main()
{
int colour_code, choice;
do
{
printf("Enter the colour code\n");
scanf("%d", &colour_code);
switch(colour_code)
{
Case 1: printf("colour; Violet\n");
break ;
Case 2: printf("colour; Indigo\n");
break ;
Case 3: printf("colour; Blue\n");
break ;
Case 4: printf("colour; Green\n");
break ;
Case 5: printf("colour; Yellow\n");
break ;
Case 6: printf("colour; Orange\n");
break ;
Case 7: printf("colour; Red\n");
break ;
default : printf("It is not colour of a rainbow\n");
}
/*End of switch */
printf("press 0 or 1\n");
scanf("%d", &choice);
} while (choice==1);
} /*End of main()*/
Sample output printed in the textbook:
Enter the colour code
4
Colour : Green
Press 0 or 1
1
Enter the colour code
2
Colour : Indigo
Press 0 or 1
0
#include <stdio.h>
void main()
{
int colour_code, choice;
do
{
printf("Enter the colour code\n");
scanf("%d", &colour_code);
switch(colour_code)
{
Case 1: printf("colour; Violet\n");
break ;
Case 2: printf("colour; Indigo\n");
break ;
Case 3: printf("colour; Blue\n");
break ;
Case 4: printf("colour; Green\n");
break ;
Case 5: printf("colour; Yellow\n");
break ;
Case 6: printf("colour; Orange\n");
break ;
Case 7: printf("colour; Red\n");
break ;
default : printf("It is not colour of a rainbow\n");
}
/*End of switch */
printf("press 0 or 1\n");
scanf("%d", &choice);
} while (choice==1);
} /*End of main()*/
Enter the colour code
4
Colour : Green
Press 0 or 1
1
Enter the colour code
2
Colour : Indigo
Press 0 or 1
0
Karnataka PUCTextbookLongImportance★★★★★est
90% · 139/154 Questions
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →[!TLDR]
A switch inside a do-while reports a rainbow colour by its code and repeats while the user enters 1; 4 -> Green, 2 -> Indigo.
Here a switch (multi-way selection on the code) is nested inside a do-while (a menu that repeats). The post-test do-while guarantees the menu is shown at least once, and the trailing prompt reads a continue flag: while choice == 1 the loop runs again, otherwise it ends. The default label catches any code outside 1-7. …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.