Difference between revisions of "Print Out the Alphabet"

From CompSciWiki
Jump to: navigation, search
m
Line 1: Line 1:
 
{{1010PrAD|ProblemName=Print Out the Alphabet
 
{{1010PrAD|ProblemName=Print Out the Alphabet
  
|Problem= Print out the alphabet using only 4 lines of code.
+
|Problem= You task is to print out the lower case letters of the aplhabet. You are only allowed to use one char variable and one println statement inside of a for loop.
 
+
<pre>
+
System.out.println("a");
+
System.out.println("b");
+
System.out.println("c");
+
System.out.println("d");
+
System.out.println("e");
+
System.out.println("f");
+
System.out.println("g");
+
System.out.println("h");
+
System.out.println("i");
+
System.out.println("j");
+
System.out.println("k");
+
System.out.println("l");
+
System.out.println("m");
+
System.out.println("n");
+
System.out.println("o");
+
System.out.println("p");
+
System.out.println("q");
+
System.out.println("r");
+
System.out.println("s");
+
System.out.println("t");
+
System.out.println("u");
+
System.out.println("v");
+
System.out.println("w");
+
System.out.println("x");
+
System.out.println("y");
+
System.out.println("z");
+
</pre>
+
  
  
Line 40: Line 11:
 
An image or By Students section
 
An image or By Students section
  
|Solution=
+
|Solution= To get the next letter in the alphabet you can add 1 each time to the char variable.
  
 
<pre>
 
<pre>
 
int numLetters = 26;
 
int numLetters = 26;
char alphabet[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
+
char letter = 'a';
  
 
for(int i = 0; i < numLetters; i++)
 
for(int i = 0; i < numLetters; i++)
     System.out.println(alphabet[i]);
+
{
 +
     System.out.println(letter);
 +
    letter = letter + 1;
 +
}
 
</pre>
 
</pre>
  
 
}}
 
}}

Revision as of 13:14, 30 March 2010

Back to the Program-A-Day homepage

Problem

You task is to print out the lower case letters of the aplhabet. You are only allowed to 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

SolutionCode goes here. Please DO NOT put your code in <pre> tags!

Back to the Program-A-Day homepage