Dremendo Tag Line

Input numbers in 2d array and find the sum of all 2 digit and 3 digit numbers in Java

Two Dimensional Array - Question 2

In this question, we will see how to input numbers in a 3X3 integer matrix (2d array) and find the sum of all 2-digit and 3-digit positive numbers separately in Java programming. To know more about two dimensional array click on the two dimensional array lesson.

Q2) Write a program in Java to input numbers in a 3X3 integer matrix (2d array) and find the sum of all 2-digit and 3-digit positive numbers separately.

Program

import java.util.Scanner;

public class Q2
{
    public static void main(String args[])
    {
        int a[][]=new int[3][3], r,c,s1=0,s2=0;
        Scanner sc=new Scanner(System.in);

        System.out.println("Enter 9 numbers");
        for(r=0; r<3; r++)
        {
            for(c=0; c<3; c++)
            {
                a[r][c]=sc.nextInt();
                if(a[r][c]>=10 && a[r][c]<=99)
                {
                    s1=s1+a[r][c];
                }
                else if(a[r][c]>=100 && a[r][c]<=999)
                {
                    s2=s2+a[r][c];
                }
            }
        }
        System.out.println("Sum of 2 digit positive numbers = "+s1);
        System.out.println("Sum of 3 digit positive numbers = "+s2);
    }
}

Output

Enter 9 numbers
12
4
69
124
36
221
96
5
7
Sum of 2 digit positive numbers = 213
Sum of 3 digit positive numbers = 345
video-poster