Monday 23 March 2020

ADDING DIGITS IN AN INTEGER USING JAVA LANGUAGE

Q-17-Write a java program that reads an integer between 0 and 1000 and adds all the digits in the integer. For example, if an integer is 932, the sum of all its digits is 14.

INPUT :
import java.util.Scanner;
public class NCM {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int number;
System.out.println("Enter a number between 0 and 1000:");
number=sc.nextInt();
int a=number%10;
int a1=number/10;
int b=a1%10;
int b1=a1/10;
int c=b1%10;
int sum=a+b+c;
System.out.println("The sum of the digits is "+sum);
}


}

OUTPUT :
Enter a number between 0 and 1000:
932

The sum of the digits is 14

No comments:

Post a Comment