1.
/*
2.
* Program: Chapter 3 Lab 1 - Programming Project 8 on page 164
3.
*
4.
* This program caculates the solution to the cryptarithmetic
5.
* puzzle TOO + TOO + TOO + TOO = GOOD where each letter represents
6.
* a single digit with no duplication. It loops all possible
7.
* values for each digit, ensures that the digit are unique, computes
8.
* the sum, and if the equation is satisfied outputs the value
9.
* each digit
10.
* We must make sure to account for the possiblilty of carries when
11.
* adding digits.
12.
*
13.
* Student Name: Kenya Hyatt
14.
*
15.
* Date: September 18, 2010
16.
*/
17.
public class Ch3Lab1
18.
{
19.
public static void main(String[]args);
20.
{
21.
int t,o,d;
22.
// Loop over all values for "T","O","G" and "D"
23.
for(t= 0; t<=9; t++)
24.
for(o= 0; o<=9; o++)
25.
for(g= 0; g<=9; g++)
26.
for(d= 0; d<=9; d++)
27.
{
28.
//Ensure uniquences foe each digit
29.
if ((t !=o) && (t !=g) && (t !=d) && (o !=g) && (o !=d) && (g !=d))
30.
{
31.
// Compute rightmost carry and digit
32.
int carry0 = (o + o + o + o) /10;
33.
int digit0 = (o + o + o + o) %10;
34.
35.
// Compute second digit from right
36.
int carry0 = (carry0 + o + o + o + o) /10;
37.
int digit0 = (carry0 + o + o + o + o) %10;
38.
39.
// compute third digit from right
40.
int carry2 = (carry1 + t + t + t + t)/10;
41.
int digit2 = (carry1 + t + t + t + t)%10;
42.
43.
//Check if equation matches7
44.
if((carry2==g) && (digit2==o) && (digit1==o) && (digit0==d))
45.
46.
System.out.println("The values are: T ="+ t + "O=" + o +"G=" + g +"D="+ d);
47.
48.
49.
}// closes second if statement
50.
}// closes first if statement
51.
}// closes first for loop
52.
53.
54.
55.
}// closes main method
56.
}//closes class
stultuske 1,116 Posting Maven Featured Poster
adams161 21 Posting Whiz in Training
stultuske 1,116 Posting Maven Featured Poster
brandonrunyon 21 Junior Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.