हिंदी

Write a program to enter a number and print the cube of each digit. Print the sum of the cube of the digits.

Advertisements
Advertisements

प्रश्न

Write a program to enter a number and print the cube of each digit. Print the sum of the cube of the digits.

कोड लेखन
Advertisements

उत्तर

import java.util.*;
class clDigits
{
    void main()
    {
        Scanner sc = new Scanner(System.in);
        int N, d, dq, sum = 0;        
        System.out.print("Enter a number : ");
        N = sc.nextInt();        
        int cn = N;
        while(cn > 0)
        {
            d = cn % 10;
            dq = d * d * d;
            System.out.println("cube of digit " + d + ":" + dq);
            sum = sum + dq;
            cn = cn / 10;
        }        
        System.out.println("Sum of cube of digits of " + N + ":" + sum);
    }
}

Output:

Enter a number : 742

cube of digit 2:8

cube of digit 4: 64

cube of digit 7: 343

Sum of cube of digits of 742 : 415

shaalaa.com
  क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
अध्याय 11: Programs Overall - Digit Handling [पृष्ठ २५२]

APPEARS IN

रुपा पंडित Computer Applications [English] Class 10 ICSE
अध्याय 11 Programs Overall
Digit Handling | Q 6. | पृष्ठ २५२
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×