write java program create 2 arrays, cars, and prices, and then asks user to enter car name and price of each car. at last the program prints the highest price, lowest price, and summation of prices, and average price

  • برمجة
  • برمجة جافا

write java program to do the following:

create 2 arrays, cars, and prices,  and then asks user to enter car name and price of each car.

at last the program prints the highest price, lowest price, and summation of prices, and average price

الأجوبة

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner input=new Scanner(System.in);
System.out.println("welcome, enter the number of cars:");
String cars[]=new String[input.nextInt()];
int prices[]=new int[cars.length];
int sum=0;
int lowest_price=999999;
int highest_price=0;
for(int i=0;i<cars.length;i++)
{
System.out.println("enter name of car no."+(i+1));
cars[i]=input.next();
System.out.println("enter price of car no."+(i+1));
prices[i]=input.nextInt();
sum=sum+prices[i];
if(prices[i]>highest_price)
highest_price=prices[i];

if(prices[i]<lowest_price)
lowest_price=prices[i];
}

System.out.println("the cars Table:");
System.out.println("----------------------");
for(int j=0;j<cars.length;j++)
{
    System.out.println("car name: "+cars[j]+",   car price: "+prices[j]);
}
System.out.println("the total prices is: "+sum);
System.out.println("the average price is: "+(sum/cars.length));
System.out.println("the lowest price is: "+lowest_price);
System.out.println("the highest price is: "+highest_price);
}
}

 

output:

welcome, enter the number of cars:
3
enter name of car no.1
bmw
enter price of car no.1
30000
enter name of car no.2
toyota
enter price of car no.2
10000
enter name of car no.3
lexus
enter price of car no.3
40000
the cars Table:
----------------------
car name: bmw,   car price: 30000
car name: toyota,   car price: 10000
car name: lexus,   car price: 40000
the total prices is: 80000
the average price is: 26666
the lowest price is: 10000
the highest price is: 40000
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...