Print Out the Alphabet

From CompSciWiki
Revision as of 08:44, 8 April 2010 by EricE (Talk | contribs)

Jump to: navigation, search

Back to the Program-A-Day homepage

Problem

Print out the entire lower case letters of the aplhabet. Use the following guidelines in coming up with a solution:

 

While and For Loops

Wiki loops03.jpg

Solution

When printing out the alphabet you are going to need to know the number of letters to stop the for loop at the right time.

//final indicates that this is a constant
//Uppercase also indicates that this is a constant
final int NUMLETTERS = 26;

For printing out the letters you are going to need a char variable.

//the initial value of letter will be a since we are printing out the lower case alphabet.
char letter = 'a';

Char's and Int's can be manipulated in the same way. For example to get a number + 1, we can just do number++.

//our number variable
int number = 0;
//we are incrementing our number which is exactly the same as number = number + 1
number++;

System.out.println("number = " + number);

Output:
number = 1

We can do exactly the same thing with char's.

char letter = 'a';
//get the next letter in the alphabet
letter++;

System.out.println("letter = " + letter)

Output:
letter = 'b'

Code

Solution Code

Back to the Program-A-Day homepage