write a program in Java to enter a number and print the average of odd digits example enter a number 1257 output sum= 1+5+7=13 average sum/ 3=12.33
Answers
Answered by
2
Question:-
- Write a program in Java to enter a number and print the average of odd digits.
Program:-
import java.util.*;
class Average
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n, d, s=0, c=0;
double av=0.0;
System.out.print("Enter a number: ");
n=sc.nextInt();
while(n!=0)
{
d=n%10;
if(d%2==1)
{
s+=d;
c++;
}
n/=10;
}
av=s/(double)c;
System.out.println("Sum of odd digits: "+s);
System.out.println("Average of digits: "+av);
}
}
Similar questions