[ Home  |  FAQ-Related Q&As  |  General Q&As  |  Answered Questions ]


    Search the Q&A Archives


1.write a program in C to accept a number print its even or...

<< Back to: comp.lang.c Answers (Abridged) to Frequently Asked Questions (FAQ)

Question by bhavana
Submitted on 5/2/2004
Related FAQ: comp.lang.c Answers (Abridged) to Frequently Asked Questions (FAQ)
Rating: Rate this question: Vote
1.write a program in C to accept a number print its even or odd?
2. Write a program in C to accept a character print its digit or alphabets or special symbols?



Answer by harish
Submitted on 5/4/2004
Rating:  Rate this answer: Vote
# include <stdio.h>
/*logic is when u divide even number by 2 the remaider is always zero**/

int main (void)
{
int num;
printf ("Enter the number\n");
scanf ("%d",&num);
if (num % 2)
   printf ("Number %d is ODD\n",num);
else  printf ("Number %d is EVEN\n",num);

return 0;
}


# include <stdio.h>

int main (void)
{
char ch;
while (( ch = getchar ()) != EOF)
   printf ("%d\t",ch);
}

 

Answer by chandu
Submitted on 8/16/2004
Rating: Not yet rated Rate this answer: Vote
Write a program to check whether the given +ve number is a delmi number.

 

Answer by Santhosh - HRR
Submitted on 10/6/2004
Rating: Not yet rated Rate this answer: Vote
#include<stdio.h>
#include<conio.h>
/*logic is, last bit of every odd number is '1' and last bit of every even number is '0' so check only last bit */
int main ( void )
{
  int Number;
  clrscr();
  printf("\n Enter any integer\n")
  scanf("%d",&Number);
  if ( Number & 0x01 )
     printf("\n Entered number is Odd");
  else
     printf("\n Entered number is Even");
  return(0);
}

 

Answer by M.Praveen Kumar
Submitted on 11/4/2004
Rating: Not yet rated Rate this answer: Vote
#include<stdio.h>
#include<conio.h>
void main()
{
     int n,i;
     clrscr();
     for(i=0;i<11;i++)
     {
          clrscr();        
          printf("\nEnter any number\t");
          scanf("%d",&n);
          if( n % 2 == 0 )
              printf("\nThis is EVEN");
          else
              printf("\nThis is ODD");
          getch();
     }
}

 

Answer by l.gnanasekar
Submitted on 5/20/2005
Rating: Not yet rated Rate this answer: Vote
int main()
{
     char *s[]={"Even","Odd"};
     int n;
     printf("Enter the number:");
     scanf("%d",&n);
     printf("The given number %d is %s",n,s[n%2]);
return 0;
}


int main()
{
    while(putchar(getch())!=EOF);
return 0;
}

 

Answer by Ashok
Submitted on 6/11/2006
Rating: Not yet rated Rate this answer: Vote
Here's an even simpler solution:

#include <stdio.h>

int main() {
    int num;
    printf ("Enter the number\n");
    scanf ("%d",&num);

    return (( num & 1 ) ? printf ("%d is odd\n",num) : printf ("%d is even\n",num));
}
In the binary representation of the number if the last digit is 1, the number is odd, else it is even.

 

Your answer will be published for anyone to see and rate.  Your answer will not be displayed immediately.  If you'd like to get expert points and benefit from positive ratings, please create a new account or login into an existing account below.


Your name or nickname:
If you'd like to create a new account or access your existing account, put in your password here:
Your answer:

FAQS.ORG reserves the right to edit your answer as to improve its clarity.  By submitting your answer you authorize FAQS.ORG to publish your answer on the WWW without any restrictions. You agree to hold harmless and indemnify FAQS.ORG against any claims, costs, or damages resulting from publishing your answer.

 

FAQS.ORG makes no guarantees as to the accuracy of the posts. Each post is the personal opinion of the poster. These posts are not intended to substitute for medical, tax, legal, investment, accounting, or other professional advice. FAQS.ORG does not endorse any opinion or any product or service mentioned mentioned in these posts.

 

<< Back to: comp.lang.c Answers (Abridged) to Frequently Asked Questions (FAQ)


[ Home  |  FAQ-Related Q&As  |  General Q&As  |  Answered Questions ]

© 2008 FAQS.ORG. All rights reserved.