//Program to demonstrate some for loops import java.util.Scanner; class SpaceCount { public static void main(String [] args) { Scanner input = new Scanner(System.in); //We calculate the factorial of a number. /* The factorial of a number is defined as the product of all the numbers from 1 to the number itself. * For example, the factorial of 5, denoted as 5! = 1*2*3*4*5 = 120 * This loop starts at 1, and keeps a running product. At each iteration, multiply the current product with the current number. */ System.out.println("Enter a number: "); int num = input.nextInt(); int product = 1; for(int i =1;i<=num;i++) //we count up from 1 till the number { product = product * i; } System.out.println("The factorial is "+product); /* This part counts the number of spaces in a piece of text. * We move through the string, reading one character at a time. * If the character we just read in happens to be a space, we increment a counter. * The method getChar(index), of the String class returns the character at the given index. * The method length(), of the String class, returns the length of the String. * If the length of a string is 'k', then the string has characters from index 0 to k-1 */ System.out.println("Enter your text: "); String junk=input.nextLine();//consume the preceding newline String text = input.nextLine(); int count=0; //counter. int length= text.length();// This Java method gets the length of the string /*strings are 0 indexed*/ for(int pos=0;pos