Write a program in java
to enter 10 numbers and print the sum of those numbers whose last digit is 5.
Answers
Write a program in java
Write a program in javato enter 10 numbers and print the sum of those numbers whose last digit is 5.
Answer:
import java.util.*;
public class hello
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter 10 Numbers");
int i=0, n, ld, sum=0;
for(n=i; i<10; i++)
{
n=sc.nextInt();
ld=n%10;
if(ld==5)
{
sum=sum+n;
}
}
System.out.println("The sum is="+sum);
}
}
Explanation:
At first you enter the 10 number then by using the first 'for loop' you check that there are 10 numbers only and give them a variable n.
Then inside the loop you find out the last digit of the number you entered(ld=n%10) and after you find it out, you need to check whether the last digit is 5 or not using 'if'. If the last digit is 5 then find the sum of the numbers whose last digit is five(sum=sum+n).
At the end use 'System.out.println' to enter the sum.