Cube sum program of individual digit of number in c with description

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
}

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.

Output of  cube sum program in c?

Output of cubesum program c
On screen output of cube sum program

 Download cube sum program of individual digit free



If you have any problem in understanding this cube sum program written in c , please feel free to post in comment about cube sum program .And if you have any other technique to have Cube sum program  of individual digit of number

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 ;
}


If you have any problem in understanding this cube sum program written in c using function , please feel free to post in comment about cube sum program .And if you have any other technique to have Cube sum program  of individual digit of number using function in c

Comments

Popular posts from this blog

Copy Constructor

Print pattern in c - explore new perspectives in pattern printing

C program to check if the supplied year is leap year or not.