|
General Details
|
|
Java
|
|
Posted 57 Days Ago
|
|
120 Views
|
|
Received 1 Rating
|
|
OddNumbers in Java
Description
This program displays all odd numbers between 1 and 49 (including 1 and 49) ... and displays the sum of all those odd numbers at the end. Less than 30 lines!
Technical
JCreator Lite, NetBeans, or any other Java IDE w/compiler, or use console
Source Code
Comments
| Please login to post comments. |
|
|
Nice, but here it is in scheme. Even shorter and better:
(define (output-odd n)
(cond
((zero? n) 0)
((odd? n) (begin (display n) (+ n (output-odd (- n 1)))))
(else (output-odd (- n 1)))))
(output-odd 49)
I can do something similar with even. I can also summarize both in one function. This is the power of scheme:
(define (output kind n)
(cond
((zero? n) 0)
((kind n) (begin (display n) (+ n (output-odd (- n 1)))))
(else (output-odd (- n 1)))))
(output even? 49)
(output odd? 49)
The above 7 lines summarize both those Java programs with one function.
This is why learning other languages is great!
|
More "Java" Source Codes By This Author
Recently Posted "Java" Source Codes
Recently Rated "Java" Source Codes
|