WAP to find the sum of digit of number in Java

Java Programs

Write a program to find the sum of digit of number in Java

import java.io.*;
class sumdig
{
public static void main(String args[]) throws IOException
{
int num,sum=0,digit;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a number");
num=Integer.parseInt(br.readLine());
while(num>0)
{
digit=num%10;
sum+=digit;
num=num/10;
}
System.out.println("Sum of the digits of a number is="+ sum);
}
}

Post a Comment

Previous Post Next Post