{ }codelper
booleansP0217

Four lines of true and false

One line gives the integers a and b separated by one space. Answer the four questions below in order, one line each. ① Is a less than b? ② Are a and b equal? ③ Is a odd?
(A negative number is odd too if it is not divisible by 2.
-3 is odd and 0 is even.) ④ Are ① and ③ both true? Print true in lowercase for true and false in lowercase for false.

Input
-1,000 ≤ a, b ≤ 1,000
Output
Four lines, each true or false
Example 1 · stdin
3 5
stdout
true
false
true
true
Example 2 · stdin
-3 -3
stdout
false
true
true
false
main.py
Input (stdin)

When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).

$ Ready

Open this on a computer to write and run code. On mobile you can read the problem and the solutions.

In other languagesShow solutions by language · 8 languages

A comparison gives a true/false value (a boolean), which you can store in a variable and combine again with && (and in Python).
But printing a boolean as is differs by language.
Java, JS, TS and Craykon print true / false; Python print(x) and C# Console.WriteLine(x) print True / False (capitalized); C++ std::cout << x prints 1 / 0 (true / false with std::boolalpha); in C, bool is an integer, so printf("%d") gives 1 / 0.
So in Python, C# and C, turn it into a word, as in "true" if x else "false" or x ? "true" : "false".
The second trap is the odd test.
In the seven languages other than Python (Craykon included), -3 % 2 is -1, so asking a % 2 == 1 makes negative odd numbers false.
Asking a % 2 != 0 is right in every language (in Python -3 % 2 is 1, so both work).

Pythonmain.py · 8 lines
a, b = map(int, input().split())less = a < bsame = a == bodd = a % 2 != 0   # 음수 홀수도 0이 아니다both = less and odd# print(True)는 True를 찍는다. 소문자로 바꿔 쓴다for v in (less, same, odd, both):    print("true" if v else "false")
JavaScriptmain.js · 7 lines
const [a, b] = require("fs").readFileSync(0, "utf8").trim().split(/\s+/).map(Number);const less = a < b;const same = a === b;const odd = a % 2 !== 0; // -3 % 2는 -1이라 === 1로 물으면 안 된다const both = less && odd;// 불값은 문자열로 바뀌면 "true" / "false"다console.log([less, same, odd, both].join("\n"));
TypeScriptmain.ts · 7 lines
import { readFileSync } from "node:fs";const [a, b]: number[] = readFileSync(0, "utf8").trim().split(/\s+/).map(Number);const less: boolean = a < b;const same: boolean = a === b;const odd: boolean = a % 2 !== 0;const both: boolean = less && odd;console.log([less, same, odd, both].join("\n"));
JavaMain.java · 18 lines
import java.util.Scanner; public class Main {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        int a = sc.nextInt();        int b = sc.nextInt();        boolean less = a < b;        boolean same = a == b;        boolean odd = a % 2 != 0; // -3 % 2 == -1        boolean both = less && odd;        // Java는 boolean을 true / false로 찍는다        System.out.println(less);        System.out.println(same);        System.out.println(odd);        System.out.println(both);    }}
C#Main.cs · 22 lines
using System; class Program{    static string Word(bool v) => v ? "true" : "false";     static void Main()    {        string[] p = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);        int a = int.Parse(p[0]);        int b = int.Parse(p[1]);        bool less = a < b;        bool same = a == b;        bool odd = a % 2 != 0;        bool both = less && odd;        // Console.WriteLine(true)는 True(대문자)를 찍는다        Console.WriteLine(Word(less));        Console.WriteLine(Word(same));        Console.WriteLine(Word(odd));        Console.WriteLine(Word(both));    }}
C++main.cpp · 12 lines
#include <iostream> int main() {    int a, b;    std::cin >> a >> b;    bool less = a < b;    bool same = a == b;    bool odd = a % 2 != 0;    bool both = less && odd;    // std::boolalpha가 없으면 1과 0으로 찍힌다    std::cout << std::boolalpha << less << "\n" << same << "\n" << odd << "\n" << both << "\n";}
Cmain.c · 16 lines
#include <stdbool.h>#include <stdio.h> static const char *word(bool v) { return v ? "true" : "false"; } int main(void) {    int a, b;    scanf("%d %d", &a, &b);    bool less = a < b;    bool same = a == b;    bool odd = a % 2 != 0; /* -3 % 2 == -1 */    bool both = less && odd;    /* C의 bool은 printf로 찍으면 1과 0이다 */    printf("%s\n%s\n%s\n%s\n", word(less), word(same), word(odd), word(both));    return 0;}
Craykonmain.cray · 12 lines
p = Console.ReadLine().Trim().Split(" ");a = p[0].ToNumber();b = p[1].ToNumber();less = a < b;same = a == b;odd = a % 2 != 0; // -3 % 2는 -1이다both = less && odd;// 크레이콘은 참거짓을 true / false로 쓴다Console.WriteLine(less);Console.WriteLine(same);Console.WriteLine(odd);Console.WriteLine(both);