मराठी

Write a program to find and print the sum and product of digits of a number.

Advertisements
Advertisements

प्रश्न

Write a program to find and print the sum and product of digits of a number.

कोड लेखन
Advertisements

उत्तर

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

Output:

Enter a number : 642

Sum of digits of 642: 12

Product of digits of 642: 48

shaalaa.com
  या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
पाठ 11: Programs Overall - Digit Handling [पृष्ठ २५२]

APPEARS IN

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

Englishहिंदीमराठी


      Forgot password?
Use app×