Computer Science, asked by dikshajadhav51, 7 months ago

write a program to accept a number and find sum of digits a number (in java)
ex.4679=4+6+7+9=26
icse class 10th
i will mark you brainliest and follow you if you give me a correct answer

Answers

Answered by samarthkrv
0

Answer:

import java.util.Scanner;

public class Main

{

public static void main(String[] args) {

 Scanner sc = new Scanner(System.in);

    System.out.print("Enter a number:");

    int n = sc.nextInt();

    int sum = 0;

    int temp = n;

     while(temp!= 0)

     {

     int last = temp%10;

     sum = sum + last;

     temp /= 10;

     }

    System.out.println("The sum of the digits of " + n + " is " + sum);

}

}

Explanation:

Similar questions