C Program
#include <stdio.h> int main() { int n, sum = 0, r; scanf("%d", &n); int temp = n; while (n > 0) { r = n % 10; sum += r * r * r; n /= 10; } printf("Sum of cube of digits of %d is %d", temp, sum); return 0; }
C Output
Input: 123 Output: Sum of cube of digits of 123 is 36
C++ Program
#include <iostream> using namespace std; int main() { int n, sum = 0, r; cin >> n; int temp = n; while (n > 0) { r = n % 10; sum += r * r * r; n /= 10; } cout << "Sum of cube of digits of " << temp << " is " << sum; }
C++ Output
Input: 456 Output: Sum of cube of digits of 456 is 405
JAVA Program
import java.util.*; class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(), sum = 0, r, temp = n; while (n > 0) { r = n % 10; sum += r * r * r; n /= 10; } System.out.println("Sum of cube of digits of " + temp + " is " + sum); } }
JAVA Output
Input: 789 Output: Sum of cube of digits of 789 is 1344
Python Program
n = int(input()) temp = n sum_cubes = 0 while n > 0: r = n % 10 sum_cubes += r ** 3 n //= 10 print(f"Sum of cube of digits of {temp} is {sum_cubes}")
Python Output
Input: 321 Output: Sum of cube of digits of 321 is 36
In-Depth Learning – Entire Concept in Paragraphs
Social Plugin