473,386 Members | 1,842 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

numbers for a certain requirements

There are two sections to this program.
Section one:
Write a C++ program that determines which four digit numbers meet the following conditions:
Add the first two digits as a number to the last two digits as a number. The square of this sum equals the original number.
Example:
Original number is 4321. Determine if 43 + 21 which equals 63, when squared, is 3969 is equal to the original number (4321).
Section two:
The program continues by determining which 2, 3, and 4 digit numbers are equal to the sum of the cubes of their digits.
Example:
If the original number is 234, determine if 2 cubed (8) + 3 cubed (27) + 4 cubed (64) which is 99 is equal to the original 234.

So far:

Expand|Select|Wrap|Line Numbers
  1.  #include <cstring>
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <cmath>
  5. #include <fstream>
  6. #include <string>
  7. #include <ctime>
  8.  
  9. using namespace std;
  10.  
  11. int main ()
  12. {
  13.     int secA;
  14.  
  15.     for(int j=1000; j<9999; j++)
  16.     {
  17.  
  18.         secA =partA(j) +  partB(j);
  19.  
  20.     if ((temp * temp) == j)
  21.  
  22.         cout<<"The number is: "<<j<<endl;
  23.     }
  24.  
  25.     return 0;
  26. }
For now, I'd like for the variables partA & partB, to be the two first and last two digits repectively. That solves section one the problem. I'm gonna need a jump-start on section two of the question. Your assistance is appreciated.
Sep 29 '07 #1
11 1557
Ganon11
3,652 Expert 2GB
You correctly set the variable secA to the sum of partA and partB, but then you check whether the square of temp is equal to j. Where is temp even declared or used?
Sep 29 '07 #2
Expand|Select|Wrap|Line Numbers
  1. #include <cstring>
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <cmath>
  5. #include <fstream>
  6. #include <string>
  7. #include <ctime>
  8.  
  9. using namespace std;
  10.  
  11. int main ()
  12. {
  13.     int secA;
  14.  
  15.     for(int j=1000; j<9999; j++)
  16.     {
  17.  
  18.         secA =partA(j) +  partB(j);
  19.  
  20.     if ((secA * secA) == j)
  21.  
  22.         cout<<"The number is: "<<j<<endl;
  23.     }
  24.  
  25.     return 0;
  26. }
Sep 29 '07 #3
lumpybanana247
134 100+
Well, this would be a simple way, but proably a lot longer.
load the number as a string[string1], sepreate the sring [at pos 1 or 2]
then youd have two strings[string2 and string3]
then you could convert the two strings each to ints (int1 and int2)
then int3=int1+int2
then int4=int3*int3
then convert int4 to a string[string4]
if(string4==string1){THEY ARE EQUAL!}
Sep 29 '07 #4
Ganon11
3,652 Expert 2GB
OK, this looks good so far. Now what ideas fdo you have for writing partA and partB? How can you manipulate a number to get the first 2 digits and the last 2 digits?

HINT: If I take any 4 digit number (say 3954), I can break it down like this:

(3 * 1000) + (9 * 100) + (5 * 10) + (4 * 1)

(3 * 10**3) + (9 * 10**2) + (5 * 10**1) + (4 * 10**0)

where 10**n is 10 raised to the nth power.
Sep 29 '07 #5
i was thinking something like this for section 1:

Expand|Select|Wrap|Line Numbers
  1. #include <cstring>
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <cmath>
  5. #include <fstream>
  6. #include <string>
  7. #include <ctime>
  8.  
  9. using namespace std;
  10.  
  11. int main ()
  12. {
  13.     int secA, partA, partB;
  14.  
  15.     for(int j=1000; j<9999; j++)
  16.     {
  17.         partA = j/100;
  18.         partB = j%100;
  19.  
  20.         secA =partA(j) +  partB(j);
  21.  
  22.     if ((secA * secA) == j)
  23.  
  24.         cout<<"The number is: "<<j<<endl;
  25.     }
  26.  
  27.     return 0;
  28. }
Sep 29 '07 #6
Ganon11
3,652 Expert 2GB
That will work, but the parentheses you have after partA and partB makes the computer think you are using a function. Get rid of those parentheses and you'll be OK.

SIDE NOTE: With your for loop running with (int j = 1000; j < 9999; j++), you're getting values 1000 to 9998, but 9999 is also a 4 digit number. Maybe you should change that loop.
Sep 29 '07 #7
Expand|Select|Wrap|Line Numbers
  1. #include <cstring>
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <cmath>
  5. #include <fstream>
  6. #include <string>
  7. #include <ctime>
  8.  
  9. using namespace std;
  10.  
  11. int main ()
  12. {
  13.     int i, j, k, m, digits;
  14.     int number = 0;
  15.     int sum_cubes=0;
  16.  
  17.     for( int i = 1 ; i < 10 ; ++i )
  18.     for( int j = 0 ; j < 10 ; ++j )
  19.     for( int k = 0 ; k < 10 ; ++k )
  20.     for( int m = 0 ; m < 10 ; ++m )
  21.       {
  22.           int number = i*1000 + j*100 + k*10 + m ;
  23.           int digits = i*10 + j + k*10 + m ;
  24.           if( digits * digits == number )
  25.           cout<<"A # for Section 1 : "<<i<<j<<k<<m<<endl<<endl;
  26.       }
  27.  
  28.    for( int i = 0 ; i < 10 ; ++i )
  29.    for( int j = 0 ; j < 10 ; ++j )
  30.    for( int k = 0 ; k < 10 ; ++k )
  31.    for( int m = 0 ; m < 10 ; ++m )
  32.       {
  33.           int number = i*1000 + j*100 + k*10 + m ;
  34.           if( number < 10 ) continue ;
  35.           int sum_cubes = i*i*i + j*j*j + k*k*k + m*m*m ;
  36.           if( sum_cubes == number )
  37.           cout<<number<<sum_cubes<<endl;
  38.       }
  39.  
  40.  
  41.  
  42.     return 0;
  43. }
I made a revision to the code. A few things tho:
1. The first section of the question works, however section 2: The program continues by determining which 2, 3, and 4 digit numbers are equal to the sum of the cubes of their digits.
Example:
If the original number is 234, determine if 2 cubed (8) + 3 cubed (27) + 4 cubed (64) which is 99 is equal to the original 234.
My current output for section two, shows only 3 digit numbers.
Sep 29 '07 #8
Ganon11
3,652 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. #include <cstring>
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <cmath>
  5. #include <fstream>
  6. #include <string>
  7. #include <ctime>
  8.  
  9. using namespace std;
  10.  
  11. int main ()
  12. {
  13.     int i, j, k, m, digits;
  14.     int number = 0;
  15.     int sum_cubes=0;
  16.  
  17.     for( int i = 1 ; i < 10 ; ++i )
  18.     for( int j = 0 ; j < 10 ; ++j )
  19.     for( int k = 0 ; k < 10 ; ++k )
  20.     for( int m = 0 ; m < 10 ; ++m )
  21.       {
  22.           int number = i*1000 + j*100 + k*10 + m ;
  23.           int digits = i*10 + j + k*10 + m ;
  24.           if( digits * digits == number )
  25.           cout<<"A # for Section 1 : "<<i<<j<<k<<m<<endl<<endl;
  26.       }
  27.  
  28.    for( int i = 0 ; i < 10 ; ++i )
  29.    for( int j = 0 ; j < 10 ; ++j )
  30.    for( int k = 0 ; k < 10 ; ++k )
  31.    for( int m = 0 ; m < 10 ; ++m )
  32.       {
  33.           int number = i*1000 + j*100 + k*10 + m ;
  34.           if( number < 10 ) continue ;
  35.           int sum_cubes = i*i*i + j*j*j + k*k*k + m*m*m ;
  36.           if( sum_cubes == number )
  37.           cout<<number<<sum_cubes<<endl;
  38.       }
  39.  
  40.  
  41.  
  42.     return 0;
  43. }
I made a revision to the code. A few things tho:
1. The first section of the question works, however section 2: The program continues by determining which 2, 3, and 4 digit numbers are equal to the sum of the cubes of their digits.
Example:
If the original number is 234, determine if 2 cubed (8) + 3 cubed (27) + 4 cubed (64) which is 99 is equal to the original 234.
My current output for section two, shows only 3 digit numbers.
This code looks good. Have you considered the possibility that only 3 digit numbers satisfy this requirement?
Sep 29 '07 #9
Well....true, however, one of the actual answers is : 351...however, 153 isn't listed..similarly for section 1.
Sep 29 '07 #10
Ganon11
3,652 Expert 2GB
(3 * 3 * 3) + (5 * 5 * 5) + (1 * 1 * 1) = 27 + 125 + 1 = 153, not 351. Maybe you're getting your number in improper order...Check which of i, j, k, and m you are using for each digit.
Sep 29 '07 #11
(3 * 3 * 3) + (5 * 5 * 5) + (1 * 1 * 1) = 27 + 125 + 1 = 153, not 351. Maybe you're getting your number in improper order...Check which of i, j, k, and m you are using for each digit.
ok kool-thanks so much...problem solved...time to move on to my new post.(C++ shape calculation).
Sep 30 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: John | last post by:
I'm developing an application for medical use that will be used to capture patient background and visit data. The application will have approximately 50 forms, with an average of about 20 fields...
24
by: Stavros Christoforou | last post by:
Hello everyone, I was wondering if someone could help me with an issue I have in C++. I want to select random points within the volume of a sphere. I know how to get random numbers using srand()...
23
by: MConly | last post by:
Can you tell me what happens inside CPU when I rand() ? Where can I find the true rand function implemented ? I have heard that rand() in C/C++ is n't a good one but why it isn't a good one, are...
19
by: Eduardo Bezerra | last post by:
Hi, I'm looking for an efficient way to create the oposite of a list of numbers. For example, suppose I have this list of numbers: 100 200 300
6
by: Johan | last post by:
Dear experts, I am new to c++ (arriving from java) and I am having to implement a data table that contains cells with numbers and cells with text. I would appreciate any general indications...
17
by: Steve Jorgensen | last post by:
If you've ever employed custom error numbers and messages in you programs, you've probably ended up with code similar to what I've ended up with in the past something like... <code> public...
2
by: Sreya | last post by:
Hello..I hope someone can help me with this little that I've been having for a while. I have a form..which has many fields. one of them being a test #. what I need a combo box with only certain...
18
by: n.torrey.pines | last post by:
I understand that with floats, x*y == y*x, for example, might not hold. But what if it's the exact same operation on both sides? I tested this with GCC 3.4.4 (Cygwin), and it prints 0 (compiled...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.