Sie sind auf Seite 1von 8

(A)

Q1:
Code:
public static void main(String[] args) {
Scanner input= new Scanner(System.in);
int s,e,sum=0;
System.out.println("Enter Starting Number");
s=input.nextInt();
System.out.println("Enter Ending Number");
e=input.nextInt();
if(s<e){
for(int i=s;i<=e;i++)
{
if(i%2!=0)
{
sum=0;
System.out.println(sum);
for(int j=1;j<=i;j++){
sum+=j;
}
System.out.println("Sum till "+i+" is "+sum);
}
}
}
else
System.out.println("Starting Number is greater than ending Number");
}
Output:

Q2:
Code:

public static void main(String[] args) {

Scanner input= new Scanner(System.in);


int pictureLength, pictureWidth, pictureSquareInchSize, picturePerimiter,
numberOfStars;
String frameType="r";
int frameColour;
double totalPrice=0;

double FRAME_COLOURING_PRICE = 1.10*2;


double REGULAR_FRAME_PRICE = 8.15*2;
double FANCY_FRAME_PRICE = 9.25*2;
//values below represent cost per square inch
double CARDBOARD_PRICE = 2.02;
double GLASS_PRICE = 9.07;

double STARS_PRICE = 19.35;

System.out.println("Please enter the length & width of the picture: ");


pictureLength= input.nextInt();
pictureWidth=input.nextInt();
pictureSquareInchSize = pictureLength * pictureWidth;
picturePerimiter = (pictureLength * 2) + (pictureWidth * 2);
System.out.println("Please select your frame type (r for regular & f for fancy): ");
frameType=input.next();

// Calculate cost of the frame type


switch (frameType) {
case "r":
case "R":
totalPrice = picturePerimiter * REGULAR_FRAME_PRICE;
break;
case "f":
case "F":
totalPrice = picturePerimiter * FANCY_FRAME_PRICE;
break;
default:
System.out.println( "You entered an invalid fram type" );
}
System.out.println( "Do you want to color or not (Note: white is default): ");
System.out.println( "1 for Yes and 2 for No ");
frameColour=input.nextInt();

// Only charge for colouring if they want something other than the default
if (frameColour==1) {
totalPrice += FRAME_COLOURING_PRICE * picturePerimiter;
}
System.out.println( "Please enter the total number of stars you wish to have on your frame: ");
numberOfStars=input.nextInt();

if (numberOfStars > 0) {
totalPrice += numberOfStars * STARS_PRICE;
}

//Calculate costs for glass and cardboard dependent on picture size


totalPrice += (GLASS_PRICE * pictureSquareInchSize) + (CARDBOARD_PRICE *
pictureSquareInchSize);
System.out.println( "Your total bill for your frame comes to: $" +totalPrice) ;
}
}
Output:
(B)
Q1:
Code:
public static void main(String[] args) {
Scanner input= new Scanner(System.in);
int i, n, sum = 0;
System.out.println(" Find the sum of the series (1*1) + (2*2) + (3*3) + (4*4) + (5*5) + ... +
(n*n):");
System.out.println(" Input the value for nth term: ");
n=input.nextInt();

for (i = 1; i <= n; i++)


{
sum += i * i;
System.out.println( i + "*" + i + " = " + i * i) ;
}
System.out.println( " The sum of the above series is: " + sum );

// TODO code application logic here


}
Output:

Q2:
Code:
public static void main(String[] args) {
Scanner input= new Scanner(System.in);
int accountNumber;
float minimumBalance, currentBalance;
String accountType;

double SAVINGS_SERVICE_CHARGE = 10.00;


double CHECKING_SERVICE_CHARGE = 25.00;
double SAVINGS_INTEREST_RATE = 0.04;
double CHECKING_LOW_INTEREST_RATE = 0.03;
double CHECKING_DEFAULT_INTEREST_RATE = 0.05;

System.out.println("Please enter the account details: ");


accountNumber=input.nextInt();
accountType=input.next();
minimumBalance=input.nextInt();
currentBalance=input.nextInt();;

switch (accountType) {
case "s":
case "S":
System.out.println("Account Number: " + accountNumber);

System.out.println( "Account Type: Savings");


System.out.println("Minimum Balance: $" + minimumBalance);
System.out.println( "Current Balance: $" + currentBalance);

if (currentBalance < minimumBalance) {


System.out.println( "Service Fee: $" + SAVINGS_SERVICE_CHARGE);
} else {
System.out.println( "Interest Earned: $" +currentBalance * SAVINGS_INTEREST_RATE+
" at " + SAVINGS_INTEREST_RATE*100 + "% p.a" );
}
break;
case "c":
case "C":
System.out.println( "Account Number: " + accountNumber );
System.out.println("Account Type: Checking" );
System.out.println("Minimum Balance: $" + minimumBalance );
System.out.println( "Current Balance: $" + currentBalance );

if (currentBalance < minimumBalance) {


System.out.println( "Service Fee: $" + CHECKING_SERVICE_CHARGE );
} else if (currentBalance <= (minimumBalance + 5000.00)) {
System.out.println( "Interest Earned: $" + currentBalance *
CHECKING_LOW_INTEREST_RATE + " at " + CHECKING_LOW_INTEREST_RATE*100 + "% p.a" );
} else {
System.out.println( "Interest Earned: $" + currentBalance *
CHECKING_DEFAULT_INTEREST_RATE + " at " + CHECKING_DEFAULT_INTEREST_RATE*100 + "%
p.a" );
}
break;
default:
System.out.println("There was an error with your input" );

}
}

}
Output:

Das könnte Ihnen auch gefallen