Showing posts with label sum-of-all-digits-of-a-number. Show all posts
Showing posts with label sum-of-all-digits-of-a-number. Show all posts

Wednesday, July 8, 2020

Write a program to input a number and find the Sum and Count the all digit of this number.(2784=2+7+8+4=21…no. of digit=4)

44)   Write a program to input a number and find the Sum and Count the all digit of this number.  (2784=2+7+8+4=21…no. of digit=4)

import java.util.*;
public class sum_count_digits
{
   public static void main(String args[ ])
   {
      Scanner sc = new Scanner (System.in);
      System.out.println("Enter a number");
      int a = sc.nextInt();
      int c = 0;
      int s = 0;
      while(a>0)
      {
          int d = a%10;
          c++;
          s=s+d;
          a=a/10;
      }
      System.out.println("SUM of all digits of a entered number = "+s);
      System.out.println("Number of digits in a entered number = "+c);
    }
}
 

 Output : 


 jhvghvgk ;nkjn ,nkjhj