# What is cube sum program written in c ?
In cube sum program , we find cube of individual digit of a number and find their sum .for example: 123 = 1^3 + 2 ^3 + 3^3.
Cube of individual digit have to be found for that we have to extract each digit .As we know last digit of number can be extract using modulus division (i.e 123%10 = 3 ) , And if we do integer division of given number by 10 , we can get other digit excluding last digit .
Code of cube sum program:
// Cube sum program of individual digit of number in c start
#include <stdio.h>
#include <conio.h>
int main()
{
int number;
printf("enter a number to find cube sum");
scanf("%d", &number);
int n2,cube,cubesu=0;
while(number!=0)
{
n2 = number%10;
cube = n2*n2*n2;
cubesu = cubesu + cube;
number = number/10;
}
printf("cube sum is %d\t", cubesu);
getch();
return 0 ;
// Cube sum program of individual digit of number in c start
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# How cube sum program works?
To learn this cube sum program we need to know how to explode a given number(i.e 345 = 3 , 4 ,5 ), Here we need modulus division which provide reminder (i.e last digit ) e.g
23%10 = 3 ,so by using modulus division we can extract end digit until one digit remind using loop.If we want to extract first digit we can do integer division e.g
23/10 = 2.By using integer division we can also extract fist digit without last digit e.g 234/10 = 23.
Here for above program:we extract end digit and find cube of end digit and store in a variable(i.e cubsum) after that we extract first numbers beside last digit ,
loop continue until number is equal to one.
At last we print the cube sum.
# Cube sum program using function in c
#include <stdio.h>
#include <conio.h>
int main( )
{
int number;
printf("enter a number to find cube sum");
scanf("%d", &number);
cubesum(number);
}
cubesum(number)
{
int n2,cube,cubesum1=0;
while(number!=0)
{
n2 = number%10;
cube = n2*n2*n2;
cubesum1 = cubesum1 + cube;
number = number/10;
}
printf("cube sum is %d\t", cubesum1);
getch();
return 0 ;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29