recent posts

Even or odd program in C | C programming






Hi, I'm Golam Rabbani. Today i will discuss about C programming. This is C tutorial.

Greetings! How are you all? Hope everyone is well.

Even and Odd Number.

We will determine whether a number is odd or even by using different methods all are provided with a C language program.

Let’s go…..

Question 1: Even or odd program in C using modulus operator.

#include <stdio.h>
int main(){
    int n;
    printf("Enter integer number: ");
    scanf("%d", &n);
    if(n%2==0){
        printf("%d is even number.", n);
    }
    else{
        printf("%d is odd number.", n);
    }
    return 0;
}
Compiler result:-

Input: 11

Output: 11 is odd number.

Question 2:Even or odd program in C using conditional operator.

#include <stdio.h>
int main(){
    int n;
    printf("Enter integer number: ");
    scanf("%d", &n);
    n%2==0? printf("Even!") : printf("Odd");
    return 0;
}

Compiler result:-
Input: 10
Output: Even!

Question 3:Even or odd program in C using bitwise operator.
#include <stdio.h>
int main(){
    int n;
    printf("Enter integer number: ");
    scanf("%d", &n);
    if(n & 1 == 1){
        printf("Odd!");
    }
    else{
        printf("Even!");
    }
    return 0;
}
Compiler result:-
Input: 9
Output: Odd!

Question 4:Write a program to input a number of n (n = any positive integer number) and print the numbers which are even and odd. You have to print the total numbers of even and odd numbers.
#include <stdio.h>
int main(){
    int i,n,x,y;
    x=0;
    y=0;
    printf("Enter any positive integer number: ");
    scanf("%d", &n);
    if(n < 0){
        printf("Input is not positive number.\n");
        main(); //if you want to again taken input.
    }
    else{
        for(i=0; i < n; i=i+1){
            if(i%2 == 0){ // check all even number
                printf("%d ", i);
                x++;
            }
        }
        printf("\nTotal Even: %d", x);
        printf("\n");
        for(i=0; i < n; i=i+1){
            if(i%2 != 0){ // check all odd number
                printf("%d ", i);
                y++;
            }
        }
        printf("\nTotal Odd: %d", y);
    }
    return 0;
}
Compiler result:-
Input: 15
Output:
0 2 4 6 8 10 12 14
Total Even: 8
1 3 5 7 9 11 13
Total Odd: 7


Even or odd program in C | C programming Even or odd program in C | C programming Reviewed by Golam Rabbani on November 18, 2018 Rating: 5

No comments:

top navigation

Powered by Blogger.