demolaynyc 0 Report post Posted November 21, 2006 Hey, I need help! Can someone code this problem in C++The instructions are:An interesting problem in number theory is sometimes called the "necklace problem." This problem begins with two single-digit numbers. The next number is obtained by adding the first two numbers together and saving only the ones-digit. This process is repeated until the "necklace" closes by returning to the original two numbers. For example, if the starting numbers are 1 and 8, twelve steps are required to close the "necklace":1 8 9 7 6 3 9 2 1 3 4 7 1 8Here's another program:It says to...modify the program to determine what integers of two, three, or four digits are equal to the sum of the cubes of their digitsHere's the code: /* Chapter 4 Exercise 9 by Albert Villaroman 11-20-06 */#include <iostream.h>int main() { long numValue; //user input cout <<"Enter an integer: "; cin >>numValue; long MaxDigits = 100000000; while (MaxDigits > numValue) { //while loop to get how many digits numValue has MaxDigits /= 10; } long sum = 0; //sum of all digits while (MaxDigits >=1) { long digit = numValue / MaxDigits; //get digit sum += digit*digit*digit; numValue = numValue % MaxDigits; //enter new value for numValue MaxDigits /= 10; //ex: 100 => 10 } cout <<"Sum of the cubes of the digits is " <<sum <<endl; return(0);} Share this post Link to post Share on other sites
pyost 0 Report post Posted November 21, 2006 Since I have never worked in C++, forgive me if there are any errors, since I'm going through a tutorial in order to solve this. #include <iostream>int main (){ // Variables short first; short second; short previous; short current; short help; // Input and communication with the user cout << "Insert the first digit: "; cin >> first; cout << "\n"; cout << "Insert the second digit: "; cin >> second; cout << "\n"; cout << "The necklace is: "; // Preparing for the loop previous = second; current = (first + second)%10; cout >> first; cout >> second; // And not the WHILE loop while ( (previous != first) || (current != second) ) { cout >> current; help = previous; previous = current; current = (help + current)%10; } // Writing the last digit cout >> current; return 0;} So, what are we doing here? First, the user inputs two digits. Next, we create what the second and the third digit would be. After that, it's all in the loop. We create the next number if we haven't reached the first two. When we have, the program won't enter the loop, and will write the last remaining digit (which equals the second digit). Share this post Link to post Share on other sites
demolaynyc 0 Report post Posted November 22, 2006 wow. no experience in c++ yet you have the answer. such skill. Thanks a lot man, ill go try that out. Share this post Link to post Share on other sites
pyost 0 Report post Posted November 22, 2006 It's not all about knowing the language, but knowing how to solve the problem That's what I've been doing in my school for the past year. We are using Pascal, but doesn't stop us from solving extremely hard problems The necklace one is easy Share this post Link to post Share on other sites