Algorithm
Even, Odd, Positive and Negative
Adapted by Neilor Tonin, URI Brazil
Make a program that reads five integer values. Count how many of these values are even, odd, positive and negative. Print these information like following example.
Input
The input will be 5 integer values.
Output
Print a message like the following example with all letters in lowercase, indicating how many of these values are even, odd, positive and negative.
Input Sample | Output Sample |
-5 |
3 valor(es) par(es) |
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <stdio.h>
int main()
{
int n, i;
int pos = 0, neg = 0, par = 0, im = 0;
for(i = 0; i < 5; ++i)
{
scanf("%d", &n);
if(n > 0) {
pos++;
} else {
if(n != 0){
neg++;
}
}
if(n % 2 == 0) {
par++;
} else {
im++;
}
}
printf("%d valor(es) par(es)n", par);
printf("%d valor(es) impar(es)n", im);
printf("%d valor(es) positivo(s)n", pos);
printf("%d valor(es) negativo(s)n", neg);
return 0;
}
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with C++ Programming
Code -
C++ Programming
#include <iostream>
using namespace std;
int main()
{
int n;
int pos = 0, neg = 0, par = 0, im = 0;
for(int i = 0; i < 5; ++i)
{
cin >> n;
if(n > 0) {
pos++;
} else {
if(n != 0) {
neg++;
}
}
if(n % 2 == 0) {
par++;
} else {
im++;
}
}
cout << par << " valor(es) par(es)" << endl;
cout << im << " valor(es) impar(es)" << endl;
cout << pos << " valor(es) positivo(s)" << endl;
cout << neg << " valor(es) negativo(s)" << endl;
return 0;
}
Copy The Code &
Try With Live Editor
Input
Output
#3 Code Example with Java Programming
Code -
Java Programming
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int X, even = 0,odd = 0,positive = 0,negative = 0;
Scanner input =new Scanner(System.in);
for (int i = 1; i < = 5; i++) {
X = input.nextInt();
if (X % 2 == 0) {
even += 1;
}
if (X % 2 != 0) {
odd += 1;
}
if (X > 0) {
positive += 1;
}
if (X < 0) {
negative += 1;
}
}
System.out.print(even+" valor(es) par(es)n");
System.out.print(odd+" valor(es) impar(es)n");
System.out.print(positive+" valor(es) positivo(s)n");
System.out.print(negative+" valor(es) negativo(s)n");
}
}
Copy The Code &
Try With Live Editor
Input
Output
#4 Code Example with Python Programming
Code -
Python Programming
even=0
odd=0
positive=0
negative=0
for i in range(5):
n = float(input())
if(n % 2 == 0):
even = even+1
else:
odd =odd+1
if (n > 0):
positive =positive+1
elif (n < 0):
negative =negative+1
print(f"{even} valor(es) par(es)")
print(f"{odd} valor(es) impar(es)")
print(f"{positive} valor(es) positivo(s)")
print(f"{negative} valor(es) negativo(s)">
Copy The Code &
Try With Live Editor
Input
Output