I need a head-up on this program....i really don't know where to start....however for section one, i'm guessing we'll have to use for loop from 1000 to 9999...because of the fact that they show the range of the samllest to largest 4 digit #'s. In section two I guess we'd have to set some value from the lowest 2 digit # to the largest 4 digit #. I'll try to start a code and post as soon as I can. The assignment is as follows:
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.
Thanks for all the help.
- 4 Contributors
- forum15 Replies
- 17 Views
- 11 Years Discussion Span
- comment Latest Post by zandiago
OK. Well for section one you should look into the division and remainder operators and loop ... Could be a long loop so think about it and see if there are any tricks you can use.
Section 2 is very similar.
Look at this code:
int main( void ) {
int num;
std::cout<< "Enter a number: ";
std::cin >> num;
while ( num ) {
std::cout<< num%10 << " ";
num /= 10;
}
return 0;
}
here is a thought:: make one function that returns the last two digits of the number, and another one that returns the first two... Then in a loop add these parts , square them and check if they are equal to the loop counter..
One note:: are you sure you want the algorithm to start from 1000...in my opinion it should start from 0000.....anyhow the algorithm doesn't change...
here is sample code {a.k.a spoiler}:
#include <cmath>
#include <iostream>
int firtHalf(int num)
{
return (num%100);
}
int secondHalf(int num)
{
return (num/100);
}
int main( void )
{
int temp;
for(int i=0; i<9999; ++i)
{
temp = firtHalf(i) + secondHalf(i);
if( pow(temp,2) == i) std::cout<<"found one! : \t\t"<<i<<std::endl;
}
return 0;
}
Thx much n.aggel. It does work, what im going to do, is make a bit of modifications to it and repost. The thing is, even though i know a little bit about the use of different functions, the class hasn't reached that section yet, and some of these professors don't want anything that they haven't covered yet...i appreciate your help.
#include <cstring>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
#include <string>
#include <ctime>
using namespace std;
int main ()
{
int num;
for(int j=0; j<9999; j++)
{
partA = num%10 <<endl;
num /= 10;
num = partA(j) + partB(j);
if ((num * num) == j)
cout<<"The number is: "<<j<<endl;
}
return 0;
}
So pretty much, I'm trying to do the program without the functions that n.aggel had pointed out...the program compiles ok and shows the correct answer, but our professor doesn't want us to use functions as yet (apart from the main function). But n.aggel, if you start at 0, instead of the smallest 4 digit # (and then do the loop), 0's many be included in the output?
// ...
for( int i = 1 ; i < 10 ; ++i )
for( int j = 0 ; j < 10 ; ++j )
for( int k = 0 ; k < 10 ; ++k )
for( int m = 0 ; m < 10 ; ++m )
{
int number = i*1000 + j*100 + k*10 + m ;
int first2_plus_last2 = i*10 + j + k*10 + m ;
if( first2_plus_last2 * first2_plus_last2 == number ) /* ... */ ;
}
for( int i = 0 ; i < 10 ; ++i )
for( int j = 0 ; j < 10 ; ++j )
for( int k = 0 ; k < 10 ; ++k )
for( int m = 0 ; m < 10 ; ++m )
{
int number = i*1000 + j*100 + k*10 + m ;
if( number < 10 ) continue ;
int sum_cubes = i*i*i + j*j*j + k*k*k + m*m*m ;
if( sum_cubes == number ) /* ... */ ;
}
//...
#include <cstring>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
#include <string>
#include <ctime>
using namespace std;
int main ()
{
int i, j, k, m, digits;
int num = 0;
int sum_cubes = 0;
int number = 0;
for( int i = 1 ; i < 10 ; ++i )
for( int j = 0 ; j < 10 ; ++j )
for( int k = 0 ; k < 10 ; ++k )
for( int m = 0 ; m < 10 ; ++m )
{
int number = i*1000 + j*100 + k*10 + m ;
int digits = i*10 + j + k*10 + m ;
if( digits * digits == num )
cout<<i<<j<<k<<m<<endl;
}
int temp;
for(int j=1000; j<9999; ++j)
{
temp = num + num;
if ((temp * temp) == j)
cout<<"Section 1 numbers "<<j<<endl;
}
for( int i = 0 ; i < 10 ; ++i )
for( int j = 0 ; j < 10 ; ++j )
for( int k = 0 ; k < 10 ; ++k )
for( int m = 0 ; m < 10 ; ++m )
{
int number = i*1000 + j*100 + k*10 + m ;
if( number < 10 ) continue ;
int sum_cubes = i*i*i + j*j*j + k*k*k + m*m*m ;
if( sum_cubes == number );
cout<<number<<sum_cubes<<endl;
}
return 0;
}
Based on the inputs from the prevoius posts..this is what i came up with, but is wrong, any help is appreciated....i'll be reposting to see if I can get this...thx in advance.
Also..where I have cout<<"Section 1 numbers "<<j<<endl;...why doesn't that line prints on the screen? The second part doesn't seem to work how I wanit...the first section works ok...but just the second part of the function.
Cause
if ((temp * temp) == j)
returns false?
See it..missed that one...but the answer still outputs on the screen though
#include <cstring>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
#include <string>
#include <ctime>
using namespace std;
int main ()
{
int i, j, k, m, digits;
int number = 0;
for( int i = 1 ; i < 10 ; ++i )
for( int j = 0 ; j < 10 ; ++j )
for( int k = 0 ; k < 10 ; ++k )
for( int m = 0 ; m < 10 ; ++m )
{
int number = i*1000 + j*100 + k*10 + m ;
int digits = i*10 + j + k*10 + m ;
if( digits * digits == number )
cout<<"The # : "<<i<<j<<k<<m<<endl;
}
int temp;
for(int j=1000; j<9999; ++j)
{
temp = number + number;
if ((temp * temp) == j)
cout<<"No such numbers "<<j<<endl;
}
return 0;
}
Thanks for all the help thus far, the first section of teh assignment works good, just the second section: 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.
I made a bit of modifications to what I had and it produced random numbers.
but our professor doesn't want us to use functions as yet (apart from the main function
sorry, i didn't know that....
. But n.aggel, if you start at 0, instead of the smallest 4 digit # (and then do the loop), 0's are included in the output?
yes, this is because the number 0000 satisfies your conditions...
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.
I made a bit of modifications to what I had and it produced random numbers.
i think vijayan answered the second section....{you have to replace the comments part with what you want to output...
// ... for( int i = 0 ; i < 10 ; ++i ) for( int j = 0 ; j < 10 ; ++j ) for( int k = 0 ; k < 10 ; ++k ) for( int m = 0 ; m < 10 ; ++m ) { int number = i*1000 + j*100 + k*10 + m ; if( number < 10 ) continue ; int sum_cubes = i*i*i + j*j*j + k*k*k + m*m*m ; if( sum_cubes == number ) /* ... */ ; } //...
Oh goodness...never thought about the zeros...because '0000' isn't really used.
I'm repost my code...section one is kool, however, i get three digit answers....the assignment was to determine which 2, 3, and 4 digit numbers are equal to the sum of the cubes of their digits.
A qucik observation: one of the actual answers is : 351...however, 153 isn't listed... Can i just sort the individual values?
Ok guys, thx much for the assistance, problem solved. Time to move on my new assignment. Thx again and have a good day.