C Program to add all Special Cube Numbers

Write a C program to add all "special cube numbers" between 2 intervals of numbers specified.

Constraints :
A positive integer is called a "Special cube number" if the sum of cubes of individual digit is equal to that number itself. For example:
153 =1*1*1+ 5*5*5 + 3*3*3 //153 is a "special cube number".
12 is not equal to 1*1*1+2*2*2 // 12 is not a "special cube number".

Sample Input:

100
409

Sample Output:
894

Explanation :
When the user enters 2 integers, lets say 100 and 400. The task of the program is to perform the addition of special cube numbers between 2 these number (
100 and 400 ) and thereby giving the sum: 153 + 370 + 371 = 894



#include <stdio.h>
#include <stdlib.h>

int splcube(int num1, int num2){
        int i, j, total=0;
        int tmp, dig;
        for(i = num1; i< num2; i++){
                j=tmp=i;
                int sum =0;
                while(j > 0){
                        dig = j % 10;
                        sum = sum + dig*dig*dig;
                        j = j/10;
                }
                if(tmp == sum)
                        total = total + sum;
        }
        return total;
}

int main()
{
        int res, num1, num2;
        printf("Enter the first Number : ");
        scanf("%d", &num1);

        printf("Enter the Second Number : ");
        scanf("%d", &num2);
        res = splcube(num1, num2);
        printf("Cube special sum is : %d\n", res);
        return 0;
}

Output :
Enter the first Number : 100
Enter the Second Number : 400
Cube special sum is : 894


Enter the first Number : 100
Enter the Second Number : 200
Cube special sum is : 153

No comments:

Post a Comment