Difference between revisions of "Print Out the Alphabet"

From CompSciWiki
Jump to: navigation, search
Line 24: Line 24:
 
</pre>
 
</pre>
  
|SolutionCode=int numLetters = 26;
+
|SolutionCode=
 +
int numLetters = 26;
 
char letter = 'a';
 
char letter = 'a';
  

Revision as of 12:32, 1 April 2010

Back to the Program-A-Day homepage

Problem

Print out the entire lower case letters of the aplhabet. Use one char variable and one println statement inside of a for loop.

 

SideSectionTitle

float
Taken from http://www.flickr.com/photos/daniello/565304023/

An image or By Students section

Solution

To get the next letter in the alphabet you can add 1 each time to the char variable.

int numLetters = 26;
char letter = 'a';

for(int i = 0; i < numLetters; i++)
{
    System.out.println(letter);
    letter = letter + 1;
}

Code

Solution Code

Back to the Program-A-Day homepage