Even odd programs in C
×


Even odd programs in C

567

Introduction to Even-Odd Programs in C

Even odd programs in C are fundamental for beginners learning programming logic.

These programs efficiently determine whether a given number is even or odd, showcasing foundational concepts like conditional statements and operators in C.


Algorithm:

Input: Receive a number from the user.

Check: Use the modulo operator (%) to determine if the number is divisible by 2.

Condition: If the remainder is 0, the number is even; otherwise, it's odd.

Output: Display the result to the user.


Logic:

Even numbers are divisible by 2 with no remainder, while odd numbers, by contrast, leave a residue of 1 when divided by 2.


Example:

A simple C program prompts the user for input, checks if the number is even or odd, and then displays the result.

The logic involves using the modulo operator (%) to determine divisibility by 2.

The program then outputs the result to the user, confirming whether the number is even or odd.

// even odd program in C
#include<stdio.h> 

int main() {
    int num;
    
    // Prompt user for input
    printf("Enter a number: ");
    scanf("%d", &num);
    
    // Check if number is even or odd
    if (num % 2 == 0)
        printf("%d is even.\n", num);
    else
        printf("%d is odd.\n", num);
    
    return 0;
}

Output:

Enter a number: 3
3 is odd.

This program efficiently determines whether the entered number is even or odd and provides the result to the user for clarity and understanding.



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments