Coder Profile - Show off your skills, get a coder profile.
 
 
 
The Author
FallingTears
Samuel S.
Send A Message
Rating
5.00
out of 10
( 1 Ratings )
Please login to rate source codes.

Click here to register a free account with us.
General Details
Java
Posted 57 Days Ago
120 Views
Received 1 Rating
More Codes By This Author
Fun with Arrays Part 2
Fun with Arrays
Stock Market Version 2.0
Java: Calculate/Display A...
The Simplest JavaScript M...
Articles By This Author
Java: Obtaining User Inpu...

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.
 
Cinjection     Posted 49 Days Ago
 
 
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!
Page 1 of 1
More "Java" Source Codes By This Author
Recently Posted "Java" Source Codes
Recently Rated "Java" Source Codes
 
 
Latest News About Coder Profile
Coder Profile Poll
What is the next application you would like to be added to your profiles?

Books
Coding Challenges
Project Management
Blog
Tutorials


please login to cast your vote
and see the results of this poll
Latest Coder Profile Changes
Coder Profile was last updated
5 Days Ago
Official Blog :: Make A Donation :: Credits :: Contact Me
Terms & Conditions :: Privacy Policy :: Documents :: Wallpapers
Version 1.46.00
Copyright © 2007 - 2008, Scott Thompson, All Rights Reserved