0% found this document useful (0 votes)
3 views9 pages

Midterm Coding Problems Solved

The document contains multiple Java programs that perform various tasks. These include calculating item totals based on user input, computing distances and slopes between coordinates, solving quadratic equations, generating arithmetic sequences, and calculating sums and averages of even numbers. Each program uses a Scanner for input and implements loops and conditional statements to achieve its functionality.

Uploaded by

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

Midterm Coding Problems Solved

The document contains multiple Java programs that perform various tasks. These include calculating item totals based on user input, computing distances and slopes between coordinates, solving quadratic equations, generating arithmetic sequences, and calculating sums and averages of even numbers. Each program uses a Scanner for input and implements loops and conditional statements to achieve its functionality.

Uploaded by

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

import java.util.

Scanner;

public class prob2{


public static void main (String[]args){
Scanner sc = new Scanner (System.in);
System.out.println("Enter the item code : ");
String code = sc.nextLine();
System.out.println("Enter the how many of the item you want : ");
int n = sc.nextInt();
double total = 0;

if (code.equals("1")){
System.out.println("Item : Hot Dog");
total = n*4.00;

}
if (code.equals("2")){
System.out.println("Item : X Salad");
total = n*4.50;
}
if (code.equals("3")){
System.out.println("Item : X Bacon");
total = n*5.00;
}
if (code.equals("4")){
System.out.println("Item : Toast");
total = n*2.00;
}
if (code.equals("5")){
System.out.println("Item : Soda");
total = n*1.50;
}
System.out.println("Quantity : " + n);
System.out.println("Total : $" + total);

}
}

import java.util.Scanner;

public class prob3{


public static void main (String[]args){
Scanner sc = new Scanner (System.in);
System.out.println("Enter the first x co-ordinate : ");
int x1 = sc.nextInt();
System.out.println("Enter the first y co-ordinate: ");
int y1 = sc.nextInt();
System.out.println("Enter the second x co-ordinate : ");
int x2 = sc.nextInt();
System.out.println("Enter the second y co-ordinate: ");
int y2 = sc.nextInt();

double distance = Math.sqrt((Math.pow( (x2-x1),2))+(Math.pow( (y2-y1),2)));


double slope = (double)(y2-y1)/(x2-x1);
System.out.println ("Distance : " + distance);
System.out.println ("Slope : " + slope);
if (slope>=0){
System.out.println ("Slope is positive " );
}else{
System.out.println ("Slope is negative " );
}

}
}

import java.util.Scanner;

public class prob4{


public static void main (String[]args){
Scanner sc = new Scanner (System.in);
System.out.println("A = ");
float A = sc.nextFloat();
System.out.println("B = ");
float B = sc.nextFloat();
System.out.println("C = ");
float C = sc.nextFloat();

double i = (Math.pow(B,2))-(4*A*C);

double root1 = ((-B)+ (Math.sqrt (i)))/(2*A);


double root2= ((-B)- (Math.sqrt (i)))/(2*A);

if (i<0) {
System.out.println("Impossible to calculate");
}
else if (A==0){
System.out.println("Impossible to calculate");
}

else {
System.out.println("Root#1 = " + root1);
System.out.println("Root#2 = " + root2);
}

}
}

import java.util.Scanner;

public class prob4{


public static void main (String[]args){
Scanner sc = new Scanner (System.in);
System.out.println("A = ");
float A = sc.nextFloat();
System.out.println("B = ");
float B = sc.nextFloat();
System.out.println("C = ");
float C = sc.nextFloat();

double i = (Math.pow(B,2))-(4*A*C);

double root1 = ((-B)+ (Math.sqrt (i)))/(2*A);


double root2= ((-B)- (Math.sqrt (i)))/(2*A);

if (i<0) {
System.out.println("Impossible to calculate");
}
else if (A==0){
System.out.println("Impossible to calculate");
}

else {
System.out.println("Root#1 = " + root1);
System.out.println("Root#2 = " + root2);
}

}
}

//LOOP
import java.util.Scanner;
public class prob5{
public static void main (String[]args){
Scanner sc = new Scanner (System.in);
System.out.println("Enter the first term: ");
int a = sc.nextInt();
System.out.println("Enter the change: ");
int d = sc.nextInt();
System.out.println("Enter the last term: ");
int l = sc.nextInt();
int n = 1;
int term = 0;
while (term<=l){
term = a + (n-1)*d;
if ((term+d)>l){
System.out.print(term);
}else {
System.out.print(term + " , ");
}
n++;
term = a+ (n-1)*d;
}

}
}
//LOOP
import java.util.Scanner;
public class prob6{
public static void main (String[]args){
Scanner sc = new Scanner (System.in);
System.out.println("Enter the value of n: ");
int n = sc.nextInt();
double y = 0;
int counter =1;

while (counter<=n){

if (counter%4 ==0){
y+= (-1.0/counter);

}else {
y+= (1.0/counter);
}

counter++;
}
System.out.println ( "y = " + y);
}
}

//LOOP
import java.util.Scanner;
public class prob7{
public static void main (String[]args){
Scanner sc = new Scanner (System.in);
System.out.println("Enter the value of N: ");
int n = sc.nextInt();
int counter =1;
int y = 0;
int sum2 =0;
int sum1=0;

while (counter<=n) {
y = (2*counter)+1;
if (counter%2 ==0){
sum2+=y;
}else {
sum1+=y;
}
counter++;
}
y= sum1 -sum2;

System.out.println ( "y = " + y);


}
}

//LOOP
import java.util.Scanner;
public class prob8{
public static void main (String[]args){
Scanner sc = new Scanner (System.in);
System.out.println("Enter a number : ");
int n = sc.nextInt();

while (n>1) {
System.out.print( n + " , ");

if (n%2==0){
n/=2;

}else{
n= (3*n + 1);

}
System.out.print (1);

//LOOP
import java.util.Scanner;
public class prob9{
public static void main (String[]args){
Scanner sc = new Scanner (System.in);
System.out.println("Enter a number : ");
int n = sc.nextInt();
int store = n;
int digits = 0 ;
int sum =0;
int power = 0;
int value = 0;

while(n>0){
n/=10;
digits++;
}
while (store>0){
power = (int) (Math.pow(10,(digits-1)));
value = store/power;
sum+=value;
store%=power;
digits--;
}
if (sum%2==0){
System.out.println ("Sum is even");
}else {
System.out.println ("Sum is odd");
}
}

//LOOP
import java.util.Scanner;
public class prob9anotherway{
public static void main (String[]args){
Scanner sc = new Scanner (System.in);
System.out.println("Enter a number : ");
int n = sc.nextInt();
int answer = 0 ;
int sum = 0;

while(n>0){
answer = n%10;
sum+=answer;
n/=10;

}
if (sum%2==0){
System.out.println ("Sum is even");
}else {
System.out.println ("Sum is odd");
}

/*LOOP
import java.util.Scanner;
public class prob10{
public static void main (String[]args){
Scanner sc = new Scanner (System.in);
System.out.println ( "Enter a number: " );
int num = sc.nextInt();
int store = num;
int dig = 0;
int power = 0;
int power2 = 0;
int value=0;
int ans =0;
int sum=0;

while(num>0){
num/=10;
dig++;
}

while (store>0){
power = dig-1;
power2 =(int) Math.pow ( 10,power);
value = store/power2;
ans = ((int)Math.pow (2,power))*value;
sum+=ans;
dig--;

}
System.out.println ( sum);

}*/

import java.util.Scanner;

public class prob10 {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a binary number: ");
int num = sc.nextInt();
int store = num;
int sum = 0;
int power = 0;

while (store > 0) {


int digit = store % 10;
sum += digit *((int) Math.pow(2, power));
store /= 10;
power++;
}

System.out.println("Decimal equivalent: " + sum);

sc.close();
}
}

//LOOP
import java.util.Scanner;
public class prob11{
public static void main (String[]args){
Scanner sc = new Scanner (System.in);
System.out.println("Enter how many numbers you want : ");
int n = sc.nextInt();
int counter = 1;
int max = 0;
int min=0;
int sum =0;
int even=0;

while (counter<=n){
System.out.println("Enter a number : ");
int number = sc.nextInt();

if(number >=0 && number%2==0){


even++;
sum+=number;
if(counter==1){
max= number;
min = number;
}

if(number>max){
max = number;
}
if(number<min){
min = number;
}

}
counter++;
}
System.out.println ("Max : " + max);
System.out.println ("Min : " + min);
double average = sum/even ;
System.out.println ("Average : " + average);

//LOOP
import java.util.Scanner;
public class prob12{
public static void main (String[]args){
Scanner sc = new Scanner (System.in);

int counter = 1;
int max = 0;
int min=0;
int sum =0;
int even=0;

while (true){
System.out.println("Enter a number : ");
int number = sc.nextInt();
if (number==0){
break;
}

if(number >=0 && number%2==0){


even++;
sum+=number;
if(counter==1){
max= number;
min = number;
}

if(number>max){
max = number;
}
if(number<min){
min = number;
}
counter++;
}

}
System.out.println ("Max : " + max);
System.out.println ("Min : " + min);
double average = sum/even ;
System.out.println ("Average : " + average);

You might also like