C Examples

WAP in C to find out positive, negative,even,odd numbers from user

The Program in C to find out how many of them are positive, how many are negative , how many are even and how many are odd when user puts it into is given below:

#include <stdio.h>

int main() {
    int num, i, pos_count = 0, neg_count = 0, even_count = 0, odd_count = 0;

    printf("Enter the numbers (Enter -1 to stop): \n");

    for (i = 0; ; i++) {
        scanf("%d", &num);
        if (num == -1) {
            break;
        }

        if (num > 0) {
            pos_count++;
        } else if (num < 0) {
            neg_count++;
        }

        if (num % 2 == 0) {
            even_count++;
        } else {
            odd_count++;
        }
    }

    printf("Positive numbers: %d\n", pos_count);
    printf("Negative numbers: %d\n", neg_count);
    printf("Even numbers: %d\n", even_count);
    printf("Odd numbers: %d\n", odd_count);

    return 0;
}

Output:

Enter the numbers (Enter -1 to stop):
2
10
19
14
15
-3
-5
-1
Positive numbers: 5
Negative numbers: 2
Even numbers: 3
Odd numbers: 4

Pro-Tips💡

This program uses a for loop to repeatedly read in numbers from the user until -1 is entered.

It uses if else statements to check if the numbers are positive, negative, even or odd and keeps a count of each case.

Finally, it prints out the counts of each category.

Learn C-Sharp ↗

C-sharp covers every topic to learn about C-Sharp thoroughly.

Learn C Programming ↗

C-Programming covers every topic to learn about C-Sharp thoroughly.

Learn C++ Programming↗

C++ covers every topic to learn about C-Sharp thoroughly.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

C# Program to Delete Element at Desired position From Array

The Program in C# Program to Delete Element at Desired position From Array is given below: Output: Hello Codeauri Family, enter the number of elements in the array:4Enter…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X