The information about colors is to be stored in bits of a char variable called color. The bit number 0 to 6, each represent 7 colors of a rainbow, i.e. bit 0 represents violet, 1 represents indigo, and so on. Write a program that asks the user to enter a number and based on this number it reports which colors in the rainbow does the number represents.
Colours of rainbow are in order of : Red, Orange, Yellow, Green, Blue, Indigo, Violet
void main()
{
int i,input,mask,j;
printf("\nKey in a number\n");
scanf("%d",&input);
for(i=6;i>=0;i--)
{
mask=1< j=mask&input;
if(i==6&&j==64)
printf("Red");
if(i==5&&j==32)
printf(" Orange");
if(i==4&&j==16)
printf(" Yellow");
if(i==3&&j==8)
printf(" Green");
if(i==2&&j==4)
printf(" Blue");
if(i==1&&j==2)
printf(" Indigo");
if(i==0&&j==1)
printf(" Violet");
}
}
Subscribe to:
Post Comments (Atom)
there ia a mistake instead of using , you typed < operator in mask=1<j=mask&output.it must be mask=1,j=mask&output
ReplyDelete#include
ReplyDeletemask=pow(2,i),j=mask&input;
mistake posted by above person is correct but one more thing to be considered is mask should be equal to pow(2,i) and math.h should be included else the output is blank
I'm glad I did it correct yours and mine code almost same
ReplyDelete#include
ReplyDeletecolor(int );
int main()
{
int t;
printf("enter a number\n");
scanf("%d",&t);
printf("it contains ");
color(t);
}
color(int a)
{
if(a&1)printf("violet ");
if(a&2)printf("indigo ");
if(a&4)printf("blue ");
if(a&8)printf("green ");
if(a&16)printf("yellow ");
if(a&32)printf("orange ");
if(a&64)printf("red ");
}
#include
ReplyDelete#include
int main()
{
int bit_number;
printf("Enter the bit number:- ");
scanf("%d" ,&bit_number);
int value=pow(2,bit_number);
switch(value)
{
case 1:
printf("Violet");
break;
case 2:
printf("Indigo");
break;
case 4:
printf("Blue");
break;
case 8:
printf("Green");
break;
case 16:
printf("Yellow");
break;
case 32:
printf("Orange");
break;
case 64:
printf("Red");
break;
}
return 0;
}