Input 10 numbers and find the sum of 2 digit positive numbers using for loop in Java
for Loop - Question 6
In this question, we will see how to input 10 numbers and find the sum of 2 digit, positive numbers only in Java programming using for loop. To know more about for loop click on the for loop lesson.
Q6) Write a program in Java to input 10 numbers and find the sum of 2 digit, positive numbers only using for loop.
Program
import java.util.Scanner;
public class Q6
{
public static void main(String args[])
{
int i,n,s=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter 10 numbers\n");
for(i=1; i<=10; i=i+1)
{
n=sc.nextInt();
if(n>=10 && n<=99)
{
s=s+n;
}
}
System.out.println("Sum of 2 digit positive numbers = " + s);
}
}
Output
Enter 10 numbers 2 4 18 20 1 9 3 5 32 46 Sum of 2 digit positive numbers = 116