Showing posts with label For Looping Statement. Show all posts
Showing posts with label For Looping Statement. Show all posts

Triangle of Stars - Void Method - Java Sample Program

// Program: Print a triangle of stars
// Given the number of lines, this program prints a triangle of stars.

import java.util.*;
public class TriangleOfStars
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
int numberOfLines;
int numberOfBlanks;
int counter = 1;

System.out.print("Enter the number of star lines " + "(1 to 20) to be printed: ");

numberOfLines = console.nextInt();

System.out.println();

while (numberOfLines < 0 || numberOfLines > 20)
{
System.out.println("The number of star lines should " + "be between 1 and 20");
System.out.print("Enter the number of star lines " + "(1 to 20) to be printed: ");

numberOfLines = console.nextInt();

System.out.println ();
}

numberOfBlanks = 30;
for (counter = 1; counter <= numberOfLines; counter++)
{
printStars(numberOfBlanks, counter);
numberOfBlanks--;
}
} // end main

public static void printStars(int blanks, int starsInLine)
{
int count = 1;

for (count = 1; count <= blanks; count++)
System.out.print(" ");

for (count = 1; count <= starsInLine; count++)
System.out.print(" *");

System.out.println();
} //end printStars
}

Sample Output:

Enter the number of star lines (1 to 20) to be printed: 10
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *

In the method main, the user is first asked to specify how many lines of stars to print. (In
this program, the user is restricted to 20 lines because a triangular grid of up to 20 lines fits
nicely on the screen.) Because the program is restricted to only 20 lines, the while loop
in the method main ensures that the program prints the triangular grid of stars only if the
number of lines is between 1 and 20.

Largest Number (Revised) - Java Sample Program

// Program: Largest number
// This program determines the largest number of a set of 10 numbers.

import java.util.*;
public class LargestNumber
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
double num; //variable to hold the current number
double max; //variable to hold the larger number

int count; //loop control variable

System.out.println("Enter 10 numbers:");
num = console.nextDouble(); //Step 1
max = num; //Step 1

for (count = 1; count < 10; count++) //Step 2
{
num = console.nextDouble(); //Step 2a
max = larger(max, num); //Step 2b
}

System.out.println("The largest number is " + max); //Step 3
}

public static double larger(double x, double y)
{
double max;

if (x >= y)
max = x;
else
max = y;
return max;
}
}

Sample Output:

Enter 10 numbers:
10.5 56.34 73.3 42 22 67 88.55 26 62 11
The largest number is 88.55

Classify Numbers - Java Sample Program

//********************************************************
// Program: Classify Numbers
// This program counts the number of odd and even numbers.
// The program also counts the number of zeros.
// Filename: ClassifyNumbers.java
//********************************************************

import java.util.*;
public class ClassifyNumbers
{
static Scanner console = new Scanner(System.in);
static final int N = 20;
public static void main (String[] args)
{
//Declare the variables
int counter; //loop control variable
int number; //variable to store the new number
int zeros = 0; //Step 1
int odds = 0; //Step 1
int evens = 0; //Step 1

System.out.println("Please enter " + N + " integers, positive, "
+ "negative, or zeros."); //Step 2

for (counter = 1; counter <= N; counter++) //Step 3
{
number = console.nextInt(); //Step 3a
System.out.print(number + " "); //Step 3b
//Step 3c
switch (number % 2)
{
case 0:
evens++;
if (number == 0)
zeros++;
break;
case 1:
case -1:
odds++;
}//end switch
}//end for loop

System.out.println();
//Step 4
System.out.println("There are " + evens + " evens, "
+ "which also includes "
+ zeros + " zeros");
System.out.println("Total number of odds is: " + odds);
}
}
/*
Sample Output: (In this sample run, the user input is shaded.)
********************************************************
Please enter 20 integers, positive, negative, or zeros.
0 0 -2 -3 -5 6 7 8 0 3 0 -23 -8 0 2 9 0 12 67 54
0 0 -2 -3 -5 6 7 8 0 3 0 -23 -8 0 2 9 0 12 67 54
There are 13 evens, which also includes 6 zeros
Total number of odds is: 7
********************************************************

We recommend that you do a walk-through of this program using the above sample
input.
Note that the switch statement in Step 3c can also be written as an if...else
statement as follows:
if (number % 2 == 0)
{
evens++;
if (number == 0)
zeros++;
}
else
odds++;