You are here:

C/C Programming

Advertisement


Question
Write a program that examines all the numbers from 1 to 999, displaying all those for which the sum of cubes of the digits equals the number itself. For example, 153, 371.

1^3+5^3+3^3=153
3^3+7^3+1^3=371 ans so on....

**************
Sir,
 I am not getting the solution of this question, please make the correction so that I get the answer. Given below the wrong solution :



#include<stdio.h>
main()
{
     int i,rem,z;
     long sum=0;
     printf("\nEnter the mximum number :");
     scanf("%d",&i);
     
        while(i>0)
        {
           rem=i%10;
           sum=sum*10+rem;
           i=i/10;
           z=sum*sum*sum;
           printf("Answer: %d",z);
        }
getch();
}

Answer
Hi, Abhimanyu.

You clearly understand some of the basic concepts needed to solve this, such as the / and % operators, as evidenced by your attempted solution.  There is a bit more to it than what you have, so I've gone ahead and written out a program that will do this for you.  I've tried to explain everything in great detail in the comments in the code, so please, compile and run the code I give you, read over the comments, and if you have any questions at all about why it works or how it's doing what it does, do not hesitate to ask.

#include <stdio.h>

void main()
{
   // Storage for the maximum number and the loop counter.
   int maxNum = 0;
   int i;

   // Request the maximum number from the user
   printf("Enter the maximum number: ");
   scanf("%d",    &maxNum);

   // Loop until we've exhausted all of the numbers
   for (i = 1; i <= maxNum; ++i)
   {
       // The curDigit variable stores the current digit we're processing (IE, in the number 93,
       // there are two digits, 9 and 3.  curDigit will hold the digit we're concerned with at
       // this moment.  The curDigit variable is initialized to hold the 10's digit, so in 93,
       // it's initialized to 3.
       int curDigit = i % 10;

       // The sum variable stores the sum of the cubes.  It is initialized to hold the cube of
       // the first digit.  So in 93, sum is initialized to 27 (3 * 3 * 3).
       int sum = curDigit * curDigit * curDigit;

       // The curTest variable stores the current digit test.  This variable is used to determine
       // if we need to continue testing the number (IE, have we run out of digits to test?) and
       // it's used to determine what the current digit is.  See below.  It's initialized to 10,
       // because the digit in the 10's place has already been included with the two initializers
       // above.
       int curTest = 10;

       // Continue testing while there are more digits to test.
       while (i / curTest)
       {
           // Get the next digit.  We must account for all of the digits we've already processed,
           // which is what the "/ curTest" section does.  The "% 10" simply grabs the digit we
           // are interested in.
           curDigit = (i / curTest) % 10;

           // Now add the current digit's cube to the sum.
           sum += curDigit * curDigit * curDigit;

           // We need to move to the next digit, which, in a base-10 numbering system like ours,
           // means multiplying by 10.
           curTest = curTest * 10;
       }

       // If the sum of the cubes of the digits is equal to the number itself...
       if (sum == i)
           // ... then print the number to screen.
           printf("Sum of Digit Cubes == Number: %d\n", i);
   }
}

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Joseph Moore

Expertise

I've been programming in one form or another since my brother taught me BASIC when I was 6. I've been programing professionally since I was 20, first web development with HTML, JS, DHTML, CSS, etc., then I became a video game developer, writing code in C, C++, C#, SQL, assembly, and various scripting languages. I've even written my own scripting languages, custom designed for the games I was making. I also dabble in Java, PHP, and Perl. I've worked on pretty much every aspect of game development, including graphics, audio, gameplay, tool, UI, input, animation, and physics.

Experience

I've been writing C code for 12 years, both on my own in my spare time and professionally.

Organizations
IGDA

Education/Credentials
Bachelor of Science in Game Design and Development, Full Sail University, Winter Park, FL

Awards and Honors
Salutatorian and Advanced Achiever Awards at Full Sail; Independent Games Festival Student Showcase winner, 2004; Featured article on Gamasutra about an experimental game developed in 2004

©2012 About.com, a part of The New York Times Company. All rights reserved.