how to convert bolean to int

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 602 Views - Last Post: 30 May 2019 - 08:27 AM Rate Topic: -----

#16 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3635
  • View blog
  • Posts: 16,702
  • Joined: 20-September 08

Re: how to convert bolean to int

Posted 30 May 2019 - 07:30 AM

Quote

... and changed some lines to make it work.

so you haven't tested it ;)
Was This Post Helpful? 0
  • +
  • -

#17 Atspulgs   User is offline

  • D.I.C Addict

Reputation: 100
  • View blog
  • Posts: 537
  • Joined: 29-July 09

Re: how to convert bolean to int

Posted 30 May 2019 - 07:52 AM

You have been given the exact things you need to write here...

---
If n is odd, print Weird
If n is even and in the inclusive range of 2 to 5, print Not Weird
If n is even and in the inclusive range of 6 to 20, print Weird
If n is even and greater than 20, print Not Weird
---

if( n % 2 == 1 )
    //Its odd so print "Weird"
else if( n % 2 == 0 && n >= 2 && n <= 5)
    //Its even   and its between 2 and 5 inclusively therefore print "Not Weird".
//...



So on and on...

Note that I exaggerated whats actually needed to match the text.
For example, you don't need to check for evens if you've already ruled out odds... so you can just check the ranges afterwards...
Of course you can just check for Weird cases only and print Not Weird in all the other cases... but I don't know how much you are allowed to fudge the tasks.

This post has been edited by Atspulgs: 30 May 2019 - 07:53 AM

Was This Post Helpful? 0
  • +
  • -

#18 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 775
  • View blog
  • Posts: 5,836
  • Joined: 25-December 13

Re: how to convert bolean to int

Posted 30 May 2019 - 08:04 AM

What is to be printed for numbers not in the selected ranges? EG 0 and < 0?
Was This Post Helpful? 0
  • +
  • -

#19 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2876
  • View blog
  • Posts: 11,252
  • Joined: 03-December 12

Re: how to convert bolean to int

Posted 30 May 2019 - 08:27 AM

After thinking about this more,

if(N >= 6 && N <= 20) {


That isn't necessary. Running a code sample,

Quote

1 Weird
2 Not Weird
3 Weird
4 Not Weird
5 Weird
6 Weird
7 Weird
8 Weird
9 Weird
10 Weird
11 Weird
12 Weird
13 Weird
14 Weird
15 Weird
16 Weird
17 Weird
18 Weird
19 Weird
20 Weird
21 Weird
22 Not Weird
23 Weird
24 Not Weird
25 Weird


Only 2 and 4 meet the criteria, because it is even only, 2-5 is irrelevant as is 6-20.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2