Wednesday, April 17, 2013

Add Numbers inside an Array : JAVA OOP



Write Java Source code: How to Add Numbers inside an Array using For Loop




Sample Output:

Enter the size of the input you want to enter: 5
Enter 5 numbers: 2 5 3 7 25
The sum of the numbers is:42

Source Code:





//java class


public class ArraySum
{
    public int sumOfArray(int[] array)
    {
        int sum = 0;
        for(int i=0; i<array.length; i++)
        {
            sum = sum + array[i];
        }

        return sum;
    }
}

//main class


import java.util.Scanner;

public class Main
{

    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the size of the input you want to enter: ");
        int size = input.nextInt();
        int[] numArr = new int[size];
         
        System.out.print("Enter "+ size +" numbers: ");
        for(int i=0; i<numArr.length; i++)
        {
          numArr[i]=input.nextInt();
          
        }

        ArraySum access = new ArraySum();
        System.out.print("The sum of the numbers is:" + access.sumOfArray(numArr));       

    }
}

8 comments:

  1. Mohammad Ashraful Hasan Sobuj
    My Batch- 46th
    My Department :CSE
    My ID 201420656



    public class ArraySum
    {
    public int sum OfArray(int[] a)
    {
    int sum = 0;
    for(int i=0; i<a.length; i++)
    {
    sum = sum + a[i];
    }
    return sum;
    }
    }
    import java.util.Scanner;
    public class Main
    {
    public static void main(String[] args)
    {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter the size of the input you want to enter: ");
    int size = input.nextInt();
    int[] numA = new int[size];
    System.out.print("Enter "+ size +" numbers: ");
    for(int i=0; i<numA.length; i++)
    {
    numA[i]=input.nextInt();
    }
    ArraySum access = new ArraySum();
    System.out.print("The sum of the numbers is:" + access.sum OfArray(numA));
    }
    }

    ReplyDelete
  2. ID-201420406
    Batch-46th

    //java class

    public class ArraySum
    {
    public int sumOfArray(int[] array)
    {
    int sum = 0;
    for(int i=0; i<array.length; i++)
    {
    sum = sum + array[i];
    }

    return sum;
    }
    }

    //main class

    import java.util.Scanner;

    public class Main
    {

    public static void main(String[] args)
    {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter the size of the input you want to enter: ");
    int size = input.nextInt();
    int[] numArr = new int[size];
    System.out.print("Enter "+ size +" numbers: ");
    for(int i=0; i<numArr.length; i++)
    {
    numArr[i]=input.nextInt();

    }
    ArraySum access = new Array Sum();
    System.out.print("The sum of the numbers is:" + access.sumOfArray(numArr));

    }
    }

    ReplyDelete
  3. sultana razia khan
    My Batch- 46th
    My Department :CSE
    My ID 201420715



    public class ArraySum
    {
    public int sum OfArray(int[] arr)
    {
    int sum = 0;
    for(int i=0; i<arr.length; i++)
    {
    sum = sum + arr[i];
    }
    return sum;
    }
    }
    import java.util.Scanner;
    public class Main
    {
    public static void main(String[] args)
    {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter the size of the input you want to enter: ");
    int size = input.nextInt();
    int[] numArr = new int[size];
    System.out.print("Enter "+ size +" numbers: ");
    for(int i=0; i<numArr.length; i++)
    {
    numArr[i]=input.nextInt();
    }
    ArraySum access = new ArraySum();
    System.out.print("The sum of the numbers is:" + access.sum OfArray(numArr));
    }
    }

    ReplyDelete
  4. Sharmin Akter
    My Batch- 46th
    My Department :CSE
    My ID 201420769



    public class ArraySum
    {
    public int sum OfArray(int[] a)
    {
    int sum = 0;
    for(int i=0; i<a.length; i++)
    {
    sum = sum + a[i];
    }
    return sum;
    }
    }
    import java.util.Scanner;
    public class Main
    {
    public static void main(String[] args)
    {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter the size of the input you want to enter: ");
    int size = input.nextInt();
    int[] numA = new int[size];
    System.out.print("Enter "+ size +" numbers: ");
    for(int i=0; i<numA.length; i++)
    {
    numA[i]=input.nextInt();
    }
    ArraySum access = new ArraySum();
    System.out.print("The sum of the numbers is:" + access.sum OfArray(numA));
    }
    }

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. Name: Rashed Prodhania (Jewel)
    ID:201420763
    Batch: 46th

    public class ArraySum
    {
    public int sumOfArray(int[] array)
    {
    int sum = 0;
    for(int i=0; i<array.length; i++)
    {
    sum = sum + array[i];
    }

    return sum;
    }
    }


    import java.util.Scanner;

    public class Main
    {

    public static void main(String[] args)
    {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter the size of the input you want to enter: ");
    int size = input.nextInt();
    int[] numArr = new int[size];
    System.out.print("Enter "+ size +" numbers: ");
    for(int i=0; i<numArr.length; i++)
    {
    numArr[i]=input.nextInt();

    }
    ArraySum access = new Array Sum();
    System.out.print("The sum of the numbers is:" + access.sumOfArray(numArr));

    }
    }

    ReplyDelete
  7. Name : Jalis mahmud
    ID : 201420762
    Batch : 46

    public class ArraySum
    {
    public int sum OfArray(int[] x)
    {
    int sum = 0;
    for(int i=0; i<x.length; i++)
    {
    sum = sum + x[i];
    }
    return sum;
    }
    }
    import java.util.Scanner;
    public class Main
    {
    public static void main(String[] args)
    {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter the size of the input you want to enter: ");
    int size = input.nextInt();
    int[] numA = new int[size];
    System.out.print("Enter "+ size +" numbers: ");
    for(int i=0; i<numA.length; i++)
    {
    numA[i]=input.nextInt();
    }
    ArraySum access = new ArraySum();
    System.out.print("The sum of the numbers is:" + access.sum OfArray(numA));
    }
    }

    ReplyDelete