Check Odd or Even Number

Loops - Read Numbers and Check Odd or Even Number


I have given here the code to read set of integers and display whether odd or even number.

Source Code


#include <stdio.h>

int main()
{
    printf("Program to find ODD or Even Number\n");
    while(1)
    {
        int n = 0;
        printf("\nEnter a number(-1 for Exit): ");
        scanf("%d",&n);
 
        if( n == -1)
            break;
 
        if((n % 2) == 0)
        {
            printf("%d is a EVEN number.\n", n);
        }
        else
        {
            printf("%d is a ODD number.\n", n);
        }
    }
    return 0;
}

Output


Program to find ODD or Even Number

Enter a number(-1 for Exit): 1
1 is a ODD number.

Enter a number(-1 for Exit): 2
2 is a EVEN number.

Enter a number(-1 for Exit): 3
3 is a ODD number.

Enter a number(-1 for Exit): 4
4 is a EVEN number.

Enter a number(-1 for Exit): 5
5 is a ODD number.

Enter a number(-1 for Exit): 6
6 is a EVEN number.

Enter a number(-1 for Exit): 7
7 is a ODD number.

Enter a number(-1 for Exit): -1

Press any key to continue . . . 

3 comments:

  1. #include

    int main()
    {
    printf("Program to find ODD or Even Number\n");
    while(1)
    {
    int i = 0;
    printf("\nEnter a number(-1 for Exit): ");
    scanf("%d",&i);

    if( i == -1)
    break;

    if((i % 2) == 0)
    {
    printf("%d is a EVEN number.\n", i);
    }
    else
    {
    printf("%d is a ODD number.\n", i);
    }
    }
    return 0;
    }

    ReplyDelete
  2. ### cheak the number even or odd ###

    #include
    main()
    {
    printf("cheak number even or odd\n");
    while(1)
    {
    int r=0;
    printf("\n enter any number(-1 for exit) : ");
    scanf("%d", & r );

    if ( r== -1)
    Break;

    if((r%2)==0)
    {
    printf("%d is even number.\'n",r);
    }
    else
    {
    printf("%d is odd number.\n",r);
    }
    }
    return 0;
    }

    ReplyDelete
  3. #include

    int main()
    {
    printf("Program to find ODD or Even Number\n");
    while(1)
    {
    int x= 0;
    printf("\nEnter a number(-1 for Exit): ");
    scanf("%d",&x);

    if( x == -1)
    break;

    if((x % 2) == 0)
    {
    printf("%d is a EVEN number.\n", x);
    }
    else
    {
    printf("%d is a ODD number.\n", x);
    }
    }
    return 0;
    }

    ReplyDelete