Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
51 views2 pages

Scanner: //program 1

This Java code contains two programs. The first program takes user input of numbers, adds them together to calculate the sum, and subtracts them to calculate the subtraction. The second program improves on this by creating a method that takes an array as input, calculates the sum and subtraction of the numbers in the array, and returns a new array with the results. It handles edge cases like an empty array or an array with one number. The main method then calls this addSub method to calculate the sum and subtraction of user input numbers and print the results.

Uploaded by

girish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views2 pages

Scanner: //program 1

This Java code contains two programs. The first program takes user input of numbers, adds them together to calculate the sum, and subtracts them to calculate the subtraction. The second program improves on this by creating a method that takes an array as input, calculates the sum and subtraction of the numbers in the array, and returns a new array with the results. It handles edge cases like an empty array or an array with one number. The main method then calls this addSub method to calculate the sum and subtraction of user input numbers and print the results.

Uploaded by

girish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Test03.

java 2/5/2020 11:59 PM

1 import java.util.Scanner;
2
3 //Program 1
4 class Test02{
5
6 public static void main(String[] args){
7
8 Scanner sc=new Scanner(System.in);
9 System.out.print("How many Number Do you want to Add & Sub::");
10 int[] ia=new int[sc.nextInt()];
11
12 System.out.println();
13
14 for (int i=0;i<ia.length;i++){
15 System.out.print("Enter "+(i+1)+" Value: ");
16 ia[i]=sc.nextInt();
17 }
18
19 int sum=ia[0];
20 int sub=ia[0];
21
22 for (int i=1;i<ia.length;i++){
23 sum=sum+ia[i];
24 sub=sub-ia[i];
25 }
26
27 System.out.println("Sum of Numbers::"+sum);
28 System.out.println("Sub of Numbers::"+sub);
29 }
30 }
31
32
33 //Program 2
34 class Test03{
35
36 static Scanner sc=new Scanner(System.in);
37
38 static int[] addSub(int[] ia){
39
40 for (int i=0;i<ia.length;i++){
41 System.out.print("Enter "+(i+1)+" Value: ");
42 ia[i]=sc.nextInt();
43 }
44
45 if(ia.length==0){
46 System.out.println("\tEnter Atleast one value");
47 return null;

Page 1 of 2
Test03.java 2/5/2020 11:59 PM

48 }else{
49 if(ia.length==1){
50 return new int[]{ia[0],ia[0]};
51 }else{
52 int sum=ia[0];
53 int sub=ia[0];
54
55 for (int i=1;i<ia.length;i++){
56 sum=sum+ia[i];
57 sub=sub-ia[i];
58 }
59
60 return new int[]{sum,sub};
61 }
62
63 }
64
65 }
66
67 public static void main(String[] args){
68 System.out.print("How many Number Do you want to Add & Sub::");
69 int[] ia=new int[sc.nextInt()];
70 System.out.println();
71 int[] ia1=addSub(ia);
72 System.out.println();
73 System.out.println("Addition of given value :: "+ia1[0]);
74 System.out.println("Substruction of given value :: "+ia1[1]);
75 }
76 }
77

Page 2 of 2

You might also like