i am not geting the correct output can anybody help me whats the pmistake i have done

# include<stdio.h>
# include<conio.h>

void main()
{
  int color;
  clrscr();
  printf("enter any number");
  scanf("%d",&color);
  if(color ==1)
    printf("violet");
  if(color==2)
    printf("indigo");
  if(color==4)
    printf("blue");
  if(color==8)
    printf("green");
  if(color==16)
    printf("yellow");
  if(color==32)
    printf("orange");
  if(color==64)
    printf("red");
  getch();
}

Recommended Answers

All 22 Replies

Perhaps if you explained exactly what you were expecting and how what you got is different...

Perhaps if you explained exactly what you were expecting and how what you got is different...

hi naure in the problem i am getting three answers for just one bit on

>i am getting three answers for just one bit on
Can you give me an example? Most likely I'm testing your code based on how it's written, not how you seem to expect it to work. In other words, I can't reproduce your issue.

It is always better to put the statements in if conditions in brackets.
And start using else if's.

# include<stdio.h>
# include<conio.h>
void main()
{
  int color;
  clrscr();
  printf("Enter any number:\n");
  scanf("%d",&color);
  if(color ==1)
{
    printf("\nviolet");
}
 else if(color==2)
{
    printf("\nindigo");
}
  else if(color==4)
{
    printf("\nblue");
}
 else if(color==8)
{
    printf("\ngreen");
}
  else if(color==16)
{
    printf("\nyellow");
}
  else if(color==32)
{
    printf("\norange");
}
else  if(color==64)
{
    printf("\nred");
}
  getch();
}

Yep,I know that have altered it a bit by putting some \n's, but that would just make your progams better.

Replace this code with the one you gave above.

[B]void main()[/B] is evil, make it [B]int main()[/B] (read this)
#include <conio.h> remember that conio isn't a part of the standard C library :)

BTW, Why don't you use a switch statement for this?

Edit:: To the OP: Do you want to pause your program? Then take a look at this :)
Edit:: For the above post (#5): Rubbish code!

commented: "make it int main()" ahh hah! I can blame you for making me use that :P +10

i'm going to use my Psychic Powers on this problem.

Monseiur Jephthah presciently knows that you need to change all of your conditional statements such as if(color ==1) ...

to the following: if(color [B]&[/B]=1) ...

and do NOT, as the post #5 suggest, use "else if" statements. if my Psychic Powers are correct, then you need to only use "if" statements.


.

oops.

i meant to say, change it to if (color [b]&&[/b] 1)

commented: Haha! Your "Physic Powers" weren't working the way it should :P +7

> i meant to say, change it to if (color && 1)
What's the difference between boolean and bitwise ?

commented: Haha, that's a good one :P !! +7

What's the difference between boolean and bitwise ?

well... y'see... dammit. nuthin' i reckon.

(* kicks rock *)

It is always better to put the statements in if conditions in brackets. And start using else if's.

Who says using brackets is better? IMO your code was harder to read than the original code. If it were me, I would have written it like this:

#include <stdio.h>

int main() {
  int color;
  
  printf( "Enter any number:\n> " );
  scanf( "%d", &color );

  switch ( color ) {
    case 1:  printf( "\nviolet" );  break;
    case 2:  printf( "\nindigo" );  break;
    case 4:  printf( "\nblue" );    break;
    case 8:  printf( "\ngreen" );   break;
    case 16: printf( "\nyellow" );  break;
    case 32: printf( "\norange" );  break;
    case 64: printf( "\nred" );     break;
  }

  getchar();
  getchar();
}
commented: Sorry but I disagree with your brackets statement! -4
commented: If you disagree make a relevant post about it. Instead of grading posts like if you were somebody. +17

>Who says using brackets is better?
A lot of people. It's recommended in all coding guidelines I've seen and required in the majority of them. The rationale is that if you add statements to a block without braces and forget to add the braces, you've introduced a difficult to find logic error. I follow the careful coding guideline[1] so it's not an issue which you choose as far as I'm concerned, but you'll find that braces around every compound statement is considered a best practice.

[1] Careful Coding Guideline: Turn your brain on before starting and keep it on while writing code.

commented: Yes +21

It is always better to put the statements in if conditions in brackets.
And start using else if's.

# include<stdio.h>
# include<conio.h>
void main()
{
  int color;
  clrscr();
  printf("Enter any number:\n");
  scanf("%d",&color);
  if(color ==1)
{
    printf("\nviolet");
}
 else if(color==2)
{
    printf("\nindigo");
}
  else if(color==4)
{
    printf("\nblue");
}
 else if(color==8)
{
    printf("\ngreen");
}
  else if(color==16)
{
    printf("\nyellow");
}
  else if(color==32)
{
    printf("\norange");
}
else  if(color==64)
{
    printf("\nred");
}
  getch();
}

Yep,I know that have altered it a bit by putting some \n's, but that would just make your progams better.

Replace this code with the one you gave above.

the master its not working

William, regarding your post (#11): I think that int main( void ) is the preferred way in C :)

(take a look at this, first good answer)

the master its not working

take your original program, and try changing your == comparisons to && comparisons

(and change your main function to read int main (void) to remove everyone's distress :P )

.

#include <stdio.h>

void showcolor(int color)
{
   if ( color &&  1 ) puts("violet");
   if ( color &&  2 ) puts("indigo");
   if ( color &&  4 ) puts("blue");
   if ( color &&  8 ) puts("green");
   if ( color && 16 ) puts("yellow");
   if ( color && 32 ) puts("orange");
   if ( color && 64 ) puts("red");
}

int main()
{
   puts("showcolor(0)");
   showcolor(0);
   puts("showcolor(1)");
   showcolor(1);
   return 0;
}

?

commented: ? :P +7

exactly. because what's the point of having 7 bits of color if you only have seven discrete colors. it seems obvious to me that the bit pattern intends to allow mixing.

e.g.:

showcolor(12)
"bluegreen"

.

>William, regarding your post (#11): I think that int main( void ) is the preferred way in C
I think we're looking into things far too much here. It works now, so let's just leave it at that, OK? :icon_wink:

I'll start using a void parameter from now on.

commented: There's a difference :P +7

if ( color && 1 ) puts("violet");
if ( color && 2 ) puts("indigo");
if ( color && 4 ) puts("blue");
if ( color && 8 ) puts("green");
if ( color && 16 ) puts("yellow");
if ( color && 32 ) puts("orange");
if ( color && 64 ) puts("red");

Shouldn't it be a & and not a &&? Otherwise the && # is pointless.

commented: yes, your're right. and this is embarrassing. :P +10

Shouldn't it be a & and not a &&? Otherwise the && # is pointless.

That's the point. ;)

commented: *smacks forehead* ahh. +10
commented: jeez. smack my forehead, more like it :$ +10

That's the point. ;)

aaah... ooops. dammit, i'm a mess.

i mean to use &... jeez. i almost, but not quite, had it right the first time on page 1 when i said &= . holy crap, this is embarrassing. i need to pay attention. i really do know better :$

this is what i meant:

#include <stdio.h>

void showcolor(int color)
{
   if ( color &  1 ) puts("violet");
   if ( color &  2 ) puts("indigo");
   if ( color &  4 ) puts("blue");
   if ( color &  8 ) puts("green");
   if ( color & 16 ) puts("yellow");
   if ( color & 32 ) puts("orange");
   if ( color & 64 ) puts("red");
}

int main()
{
   puts("showcolor(0)");
   showcolor(0);
   puts("showcolor(1)");
   showcolor(1);
   return 0;
}

.

i'd like to request that we delete all of my posts in this thread, and all posts referring to any of my posts in this thread.

in fact, lets just delete all posts except for the original post, and start over.

i dont have an excuse. i just want a mulligan.

Nah! keep them, I found this thread amusing :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.