WAP in Java to Enter the number a print sum of the digit
import java.util.*;
class loop{
public static void main(String args[])
{
int n,k,sum=0;
Scanner ob=new Scanner(System.in);
System.out.println("enter the nos:");
n=ob.nextInt();
while(n!=0)
{
k=n%10;
sum=sum+k;
n=n/10;
}
System.out.println("sum="+sum);
}
}
input:enter the nos:123
ouput:sum=6
class loop{
public static void main(String args[])
{
int n,k,sum=0;
Scanner ob=new Scanner(System.in);
System.out.println("enter the nos:");
n=ob.nextInt();
while(n!=0)
{
k=n%10;
sum=sum+k;
n=n/10;
}
System.out.println("sum="+sum);
}
}
input:enter the nos:123
ouput:sum=6
Comments
Post a Comment