0% found this document useful (0 votes)
2 views

java 1-2

The document describes two Java programs. The first program calculates the sum of an array's elements while ignoring any numbers between a 6 and a 7, if they appear in succession. The second program is a simple calculator that allows users to perform addition and subtraction, with an option to continue or exit after each operation.

Uploaded by

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

java 1-2

The document describes two Java programs. The first program calculates the sum of an array's elements while ignoring any numbers between a 6 and a 7, if they appear in succession. The second program is a simple calculator that allows users to perform addition and subtraction, with an option to continue or exit after each operation.

Uploaded by

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

Program 1

Design and Implement a Java program to print the sum of the elements of the array with the
given below condition. If the array has 6 and 7 in succeeding orders, ignore 6 and 7 and the
numbers between them for the calculation of sum.
Eg1) Array Elements - 10,3,6,1,2,7,9
O/P: 22
[i.e. 10+3+9]

Eg2) Array Elements - 7,1,2,3,6

O/P:19
Eg3) Array Elements - 1,6,4,7,9
O/P:10

import java.util.Scanner;

public class Lab1 {

public static void main(String args[])


{
//checking testcases
//int[] arr= {1,2,3,4,6,8,9,7,2};
// int[] arr= {1,2,3,6,4,2,8,7,4};
//int[] arr= {1,2,7,1,2,3,6,8};
//int[] arr= {1,2,3};

int n;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of array:");
n = sc.nextInt();
int arr[] = new int[n];
for(int i=0;i<n;i++){
arr[i] = sc.nextInt();}

int sixPos=-1;
int sevenPos=-1;
int sum=0;

for(int i=0;i<arr.length;i++)
{
if(arr[i]==6)
{sixPos=i;
break;}}

for(int i=0;i<arr.length;i++)
{
if(arr[i]==7) sevenPos=i;
}

if(sevenPos==-1) sixPos=-1;

for(int i=0;i<arr.length;i++)
{
if(sixPos!=-1 && i>=sixPos && i<=sevenPos) continue;
sum+=arr[i];
}

System.out.println(sum);

}
}
Output:

Test 1 :

Enter the size of array:


6
Enter the elements of array:
3
4
5
6
8
9
The Result considering all the conditions is = 35

Test 2 :

Enter the size of array:


4
Enter the elements of array:
1
2
3
4
The Result considering all the conditions is = 10
Test 3:

Enter the size of array:


7
Enter the elements of array:
1
2
3
6
4
3
7
The Result considering all the conditions is = 6
Test 4:

Enter the size of array:


7
Enter the elements of array:
6
6
6
7
7
7
7
The Result considering all the conditions is = 0
Program 2

Design and Implement a Java program that displays a menu with options 1. Add 2. Sub,
Based on the options chosen, read 2 numbers and perform the relevant operation. After
performing the operation, the program should ask the user if he wants to continue. If the user
presses y or Y, then the program should continue displaying the menu else the program
should terminate.

package fourd;

import java.util.*;

class calculator
{
public static void main(String a[])
{

int num1=0,num2=0,option,ex;
Scanner sc1 = new Scanner(System.in);
do
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter your choice from the following menu:");
System.out.println("1.Addition 2.Subtraction 3.Exit");
option = sc.nextInt();
if(option!=3){
System.out.println("Enter the first number");
num1=sc.nextInt();
System.out.println("Enter the second number");
num2=sc.nextInt();
}
else
break;
switch(option)
{
case 1:System.out.println("Addition of "+num1+" and
"+num2+" is "+(num1+num2));
break;
case 2:System.out.println("Subtraction of "+num1+" and
"+num2+" is "+(num1-num2));
break;
case 3: break;

default: System.out.println("Invalid choice");


}
System.out.println("Do you want to continue? Press y or Y to continue
or n to exit ");
String input = sc1.nextLine();
if (input.equals("n")) {
break;
}
}while(true);

}
}

Output:

Enter your choice from the following menu:


1.Addition 2.Subtraction 3.Exit
1
Enter the first number
2
Enter the second number
3
Addition of 2 and 3 is 5
Do you want to continue?press Y or y to continue, press No to quit
y
Enter your choice from the following menu:
1.Addition 2.Subtraction 3.Exit
2
Enter the first number
7
Enter the second number
2
Subtraction of 7 and 2 is 5
Do you want to continue?press Y or y to continue, press No to quit
n

You might also like