[go: up one dir, main page]

0% found this document useful (0 votes)
31 views6 pages

Array

Java Programs

Uploaded by

sapnadoharey3005
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)
31 views6 pages

Array

Java Programs

Uploaded by

sapnadoharey3005
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/ 6

Resort booking

import java.util.Scanner;

public class Main

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

String s = sc.nextLine();

String str[] = new String[4];

str = s.split(":");

if(Integer.valueOf(str[1])<0){

System.out.println("Invalid input for number of adults");

return;

}else if(Integer.valueOf(str[2])<0){

System.out.println("Invalid input for number of children");

return;

}else if(Integer.valueOf(str[3])<=0){

System.out.println("Invalid input for number of days");

return;

int totalPrice =
(Integer.valueOf(str[1])*1000+Integer.valueOf(str[2])*650)*Integer.valueOf(str[3]);

System.out.println(str[0]+" your booking is confirmed and the total cost is


"+totalPrice);

}
Find the winner

import java.util.Scanner;

public class Main

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the number of teams");

int noOfTeams = sc.nextInt();

if(noOfTeams<=1) {

System.out.println("Invalid input");

return;

System.out.println("Enter the details");

float minTime = Float.MAX_VALUE;

String winningTeamName = "";

while(noOfTeams-->0){

String s = sc.next();

String str[] = new String[5];

str = s.split(":");

float time1 = Float.valueOf(str[1]);

float time2 = Float.valueOf(str[2]);

float time3 = Float.valueOf(str[3]);

float time4 = Float.valueOf(str[4]);

if(time1<1 || time2<1 || time3<1 || time4<1){

System.out.println("Invalid number");

return;

}
float currTime = time1+time2+time3+time4;

if(currTime < minTime){

minTime = currTime;

winningTeamName = str[0];

System.out.printf("%s team wins the race in %.2f minutes",winningTeamName,minTime);

FISHING COMPETITION

import java.util.Scanner;

public class Main

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the details");

String s = sc.next();

String str[] = s.split(":");

int age = Integer.valueOf(str[1]);

int bigPoint = Integer.valueOf(str[2]);

int mediumPoint = Integer.valueOf(str[3]);

int smallPoint = Integer.valueOf(str[4]);


if(age<18){

System.out.println(age+" is an invalid age");

return;

if(bigPoint<0){

System.out.println(bigPoint+" is an invalid input");

return;

if(mediumPoint<0){

System.out.println(mediumPoint+" is an invalid input");

return;

if(smallPoint<0){

System.out.println(smallPoint+" is an invalid input");

return;

int totalPoints = bigPoint*10 + mediumPoint*6 + smallPoint*3;

System.out.println(str[0]+" scored "+totalPoints+" points");

Two arrays game

import java.util.Scanner;
public class Main

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the size for the first array");

int n1 = sc.nextInt();

if(n1<=0){

System.out.println("Invalid array size");

return;

System.out.println("Enter the elements for the first array");

int arr1[] = new int[n1];

for(int i=0;i<n1;i++){

arr1[i] = sc.nextInt();

System.out.println("Enter the size for the second array");

int n2 = sc.nextInt();

if(n2<=0){

System.out.println("Invalid array size");

return;

System.out.println("Enter the elements for the second array");

int arr2[] = new int[n2];

for(int i=0;i<n2;i++){
arr2[i] = sc.nextInt();

if(n1!=n2){

System.out.println("Both array size are not same");

return;

int arr3[] = new int[n1];

for(int i=0;i<n1;i++){

if(i%2==0){

arr3[i] = arr1[i]+arr2[i];

}else{

arr3[i] = arr1[i]-arr2[i];

System.out.println("The elements of third array");

for(int i=0;i<n1;i++){

System.out.println(arr3[i]);

You might also like