Sign in with
Sign up | Sign in
Your question
Closed

Help me please on this c++ problem

Tags:
Last response: in Work & Education
Share
Anonymous
April 2, 2012 9:57:59 AM

Help me please on this c++ problem

Some numbers like 153, 370, 371, 407, … have the property that the number equals the cube sum of its digits. For example:

153: 13 + 53 + 33 = 1 + 125 + 27 = 153
(the left side of the equation is confusing. He means "1cubed+5cubed+3cubed" but that's not possible to write here correctly)
370: 33 + 73 + 03 = 27 + 343 + 0 = 370
371: 33 + 73 + 13 = 27 + 343 + 1 = 371
407: 43 + 03 + 73 = 64 + 0 + 343 = 407

Write a program that prompts the user to enter two positive integers lower and upper, then finds and prints all numbers in the interval [lower, upper] that have the property that the cube sum of the digits equals the number itself. The program also prints the how many numbers have the cube sum property.

More about : problem

Best solution

July 3, 2012 2:36:06 PM

solution:
#include<iostream>
#include<string>
using namespace std;



int getLastDigit(int & num)
{
int r=num%10;
num=num/10;
return r;
}

bool IsCube(int A[], int num,int count )
{
int sum=0;
while(count>=0)
{
sum=(A[count]*A[count]*A[count])+sum;
count--;
}
if(num==sum)
return true;
return false;
}

void main()
{
int A[100]={0};
int start=0;
int end=0;
int temp=0;
int i=0;
cout<<"first number:";cin>>start;
cout<<"second number:";cin>>end;
if(start>end)
{
cout<<"\nInvalid Interval\n";
exit(0);
}
for(int num=start; num<=end; num++)
{
i=0;
temp=num;
while(1)
{
A=getLastDigit(temp);
if(temp==0)
break;
i++;
}
if(IsCube(A,num,i))
cout<<num<<" is a cubic sum\n";
}
}
Share
July 3, 2012 6:33:46 PM

Best answer selected by Proximon.
Score
0
July 3, 2012 6:33:48 PM

This topic has been closed by Proximon
Score
0
Related resources
!