find sum of digits of given number
Answers
Answered by
2
Answer:
import java.util.*;
class sumdigits{
public static void main (String ar []){
Scanner sc=new Scanner (System.in);
System.out.println("Enter a number");
int n=sc.nextInt();
int s=0;
for(;n!=0;n/=10)
s+=n%10;
System.out.println("Sum="+s);
}
}//Always go for shorter approach as it reduces the compilation time as well as helps in scoring good in Computer Science!
Check the attachment
Attachments:
Similar questions