Recent Posts

Wednesday 11 June 2014

04:43

Find sum of digits of any integer no.

Today we will learn how to find the sum of digits of any integer no. Such question may seem to be little strange but if you think little logically and with patience you will find its answer easily.

CODES:


//Copyright @author Harpreet Sandhu
import java.util.*;
class sumdig
{ public static void main(String s[])
{ int t=0,d=0;
Scanner c= new Scanner(System.in);
t= c.nextInt();
while(t>0)
{ d=d+t%10;
 t=t/10;
 }
 System.out.println("Sum of digits of "+t+" is ="+d);
 }}



OUTPUT: 

e.g  Sum of digits of 351 is = 9

0 comments:

Post a Comment