Monday 31 August 2020

Sum Of Digits Of Number

Write a Java Program to find the Sum Of Digits Of Number

Input: 2461
output: 13

Code in Java:

  
import java.util.Scanner;
public class SumOfDigitsOfNumber 
{
	public static void main(String[] args) 
	{
		Scanner scan = new java.util.Scanner(System.in);
		System.out.println("Enter the N: ");
		int N=scan.nextInt();
		int reminder,sum=0;
		 while(N>0)
		 {
			 reminder=N%10;
			 sum=sum+reminder;
			 N=N/10;
		 }
		 System.out.println("Sum of Digits: "+sum);	
	}
}
  
Output:
Enter the N: 
456789
Sum of Digits: 39



Write a JAVA program to Find the Sum Of First N Natural Numbers

No comments:

Post a Comment

If you have any doubts, please let me know

Pattern 31 & 32