The information about colours is to be stored in bits of a variable called colour. The bit
number 0 to 7, each represent 8 colours of a rainbow, i.e. bit 1 represents Blue, 2 represents Green,
and so on (see table below). Write a C++ program that asks the user to enter a number and based on
this number, an eight lined rainbow (of asterisks *) is to be displayed, such that, if the bit is ON,
respective colour is displayed otherwise black line is drawn (not visible).
#include<stdio.h>
#include<conio.h>
int main() {
char color;
int n;
printf("Enter a number between 1 and 7: ");
scanf("%d",&n);
switch(n){
case 1:
printf("violet");
break;
case 2:
printf("indigo");
break;
case 3:
printf("blue");
break;
case 4:
printf("green");
break;
case 5:
printf("yellow");
break;
case 6:
printf("orange");
break;
case 7:
printf("Red");
break;
default:
printf("Invalid Input");
}
return 0;
}
Comments
Leave a comment