Computer Science, asked by throughmylensswas, 7 months ago

Write
a program to use a function int sum (int) which accepts an
Poteger as parameter and return the sum of its digits. Write the
main() to input 10 integers and find the sum of the digits of each number​

Answers

Answered by anindyaadhikari13
6

Question:-

Write a program to use a function int sum(int) which accepts an integer as parameter and return the sum of its digits. Write the main() method to input 10 integers and find the sum of the digits of each number.

Program:-

import java.util.*;

class Number

{

static int sum(int n)

{

int s=0;

for(;n!=0;n/=10)

s+=n%10;

return s;

}

public static void main(String args[])

{

Scanner sc=new Scanner(System.in);

System.out.println("Enter 10 numbers. ");

for(int i=1;i<=10;i++)

{

System.out.print("Enter: ");

int n=sc.nextInt();

System.out.println("Sum of the digits is: "+sum(n)+"\n");

}//end of loop.

}//end of main()

}// end of class.

Similar questions