Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 43

Unsolved Java

Programs
Input in Java
Solutions to Unsolved Java Programs-

Question 1
In an election, there are candidates X and Y. On the election day, 80%
of the voters go for polling, out of which 60% vote for X. Write a
program to take the number of voters as input and calculate:
 number of votes X get
 number of votes Y get
import java.util.Scanner;
public class KboatPolling
{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.print("Enter the number of voters: ");


long totalVoters = in.nextLong();

long votedVoters = Math.round(0.8 * totalVoters);


long xVotes = Math.round(0.6 * votedVoters);
long yVotes = votedVoters - xVotes;

System.out.println("Total Votes: " + votedVoters);


System.out.println("Votes of X: " + xVotes);
System.out.println("Votes of Y: " + yVotes);
}
}
2.A shopkeeper offers 10% discount on the printed price of a mobile phone. However,
a customer has to pay 9% GST on the remaining amount. Write a program in Java to
import calculate the amount to be paid by the customer taking printed price as an input.
java.util.Scanner;
public class KboatInvoice
{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.print("Enter the MRP: ");


double mrp = in.nextDouble();

double priceAfterDiscount = mrp - (0.1 * mrp);


double priceWithGST = priceAfterDiscount + (priceAfterDiscount * 0.09);

System.out.println("Price after 10% discount and 9% GST: " + priceWithGST);


}
}
3. A man spends (1/2) of his salary on food, (1/15) on rent, (1/10) on miscellaneous.
Rest of the salary is his saving. Write a program to calculate and display the
following:

 money spent on food


 money spent on rent
 money spent on miscellaneous
 money saved
 Take the salary as an input.
import java.util.Scanner;

public class KboatSalary


{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.print("Enter the Salary: ");


float salary = in.nextFloat();

float foodSpend = salary / 2;


float rentSpend = salary / 15;
float miscSpend = salary / 10;
float savings = salary - (foodSpend + rentSpend + miscSpend);

System.out.println("Money spent on food: " + foodSpend);


System.out.println("Money spent on rent: " + rentSpend);
System.out.println("Money spent on miscellaneous: " + miscSpend);
System.out.println("Money saved: " + savings);
}
}
Write a program to input time in seconds. Display the time after converting them into
hours, minutes and seconds.
Sample Input: Time in seconds: 5420
importSample Output: 1 Hour 30 Minutes 20 Seconds int hrs = inputTime / 3600;
java.util.Scanner;
int mins = (inputTime % 3600) / 60;
int secs = (inputTime % 3600) % 60;
public class Time
{ System.out.println(hrs
public static void main(String args[]) + " Hours "
+ mins
{ + " Minutes "
+ secs
Scanner in = new Scanner(System.in); + " Seconds");
}
}
System.out.print("Time in seconds: ");
int inputTime = in.nextInt();
Hrs= 5420/3600
Remainingtime=inputtime%3600;
Min=remainingtime/60;
Remainingtime=remainingtime%60;
Seconds=remainingtime;
Question 5
The driver took a drive to a town 240 km at a speed of 60 km/h.
Later in the evening, he drove back at 20 km/h less than the usual
speed. Write a program to calculate:

the total time taken by the driver


the average speed during the whole journey
[Hint: average speed = total distance / total time]
public class Journey
{
public static void main(String args[]) {

int distance = 240;


float speed = 60.0f;
float returnSpeed = speed - 20;

float time2Reach = distance / speed;


float time2Return = distance / returnSpeed;
float totalTime = time2Reach + time2Return;

float avgSpeed = (distance * 2) / totalTime;

System.out.println("Total time: " + totalTime);


System.out.println("Average speed: " + avgSpeed);
}
}
Question 6 : Write a program to input two unequal numbers. Display the numbers after swapping
their values in the variables without using a third variable.
Sample Input: a = 76, b = 65
Sample Output: a = 65, b = 76 System.out.print("Enter the second number: ");
int secondNum = in.nextInt();
import java.util.Scanner;
if (firstNum == secondNum) {
public class NumberSwap System.out.println("Invalid Input. Numbers are
{ equal.");
public static void main(String args[]) { return;
}
Scanner in = new Scanner(System.in);
firstNum = firstNum + secondNum;
secondNum = firstNum - secondNum;
System.out.println("Please provide two
firstNum = firstNum - secondNum;
unequal numbers");
System.out.println("First Number: " + firstNum);
System.out.print("Enter the first number: "); System.out.println("Second Number: " +
int firstNum = in.nextInt(); secondNum);
}}
Question 7
A certain amount of money is invested for 3 years at the rate of 6%, 8% and 10%
per annum compounded annually. Write a program to calculate:

 the amount after 3 years.


 the compound interest after 3 years.
 Accept certain amount of money (Principal) as an input.
 Hint: A = P * (1 + (R1 / 100))T * (1 + (R2 / 100))T * (1 + (R3 / 100))T and CI =
A-P
import java.util.Scanner;
public class CompoundInterest
{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.print("Enter the principal: ");


double principal = in.nextDouble();
System.out.println(principal);

float r1 = 6.0f, r2 = 8.0f, r3 = 10.0f;

double amount = principal * (1 + (r1 / 100)) * (1 + (r2 / 100)) * (1 + (r3 / 100));


double ci = amount - principal;

System.out.println("Amount after 3 years: " + amount);


System.out.println("Compound Interest: " + ci);
}}
Question 8
The co-ordinates of two points A and B on a straight line are given as
(x1,y1) and (x2,y2). Write a program to calculate the slope (m) of the
line by using formula:
 Slope = (y2 - y1) / (x2 - x1)
 Take the co-ordinates (x1,y1) and (x2,y2) as input.
import java.util.Scanner;
public class LineSlope System.out.print("Enter x coordinate
{ of point B: ");
public static void main(String args[]) { int x2 = in.nextInt();

Scanner in = new Scanner(System.in); System.out.print("Enter y coordinate


of point B: ");
System.out.print("Enter x coordinate of int y2 = in.nextInt();
point A: ");
int x1 = in.nextInt(); float lineSlope = (y2 - y1) / (float)(x2 - x1);

System.out.print("Enter y coordinate of System.out.println("Slope of line: " +


point A: "); lineSlope);
int y1 = in.nextInt(); }
}
Mathematical library
functions
Question 1

Write a program to calculate the value of the given expression:


1/a2 + 2/b2 + 3/c2

Take the values of a, b and c as input from the console.


Finally, display the result of the expression to its nearest whole number.
import java.util.Scanner;

public class KboatExpression


{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.print("Enter the value of a: ");


int a = in.nextInt(); System.out.print("Enter the value of c: ");
int c = in.nextInt();
System.out.print("Enter the value of b: ");
int b = in.nextInt(); double exp = (1 / Math.pow(a, 2)) + (2 /
Math.pow(b, 2)) + (3 / Math.pow(c, 2));

long roundedExp = Math.round(exp);

System.out.println("Result rounded to whole


number: " + roundedExp);
}
}
Output
Question 2

For every natural number m>1; 2m, m2-1 and m2+1 form a
Pythagorean triplet. Write a program to input the value of 'm' through
console to display a 'Pythagorean Triplet'.

Sample Input: 3
Then 2m=6, m2-1=8 and m2+1=10
Thus 6, 8, 10 form a 'Pythagorean Triplet'.
import java.util.Scanner;

public class KboatPythagoreanTriplet


{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.print("Enter the value of m: "); int firstTriplet = 2 * m;


int m = in.nextInt(); int secondTriplet = (int)(Math.pow(m, 2) - 1);
int thirdTriplet = (int)(Math.pow(m, 2) + 1);
if (m < 2) {
System.out.println("Invalid Input, m should be System.out.println("Pythagorean Triplets: "
greater than 1"); + firstTriplet
return; + ", "
} + secondTriplet
+ ", "
+ thirdTriplet);

}
}
Output
Question 3

Write a program to input a number. Calculate its square root and cube
root. Finally, display the result by rounding it off.

Sample Input: 5
Square root of 5= 2.2360679
Rounded form= 2
Cube root of 5 = 1.7099759
Rounded form= 2
import java.util.Scanner;

public class KboatRoots


{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.print("Enter the number: ");


int num = in.nextInt();

double sqrtValue = Math.sqrt(num);


double cbrtValue = Math.cbrt(num); System.out.println("Square root of " + num + " = "
long roundSqrt = Math.round(sqrtValue); + sqrtValue);
long roundCbrt = Math.round(cbrtValue); System.out.println("Rounded form = " +
roundSqrt);
System.out.println("Cube root of " + num + " = " +
cbrtValue);
System.out.println("Rounded form = " +
roundCbrt);
}
}
Question 4

The volume of a sphere is calculated by using formula:


v = (4/3)*(22/7)*r3

Write a program to calculate the radius of a sphere by taking its


volume as an input.

Hint: radius = ∛(volume * (3/4) * (7/22))


import java.util.Scanner;

public class KboatSphere


{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.print("Enter volume of sphere: ");


double v = in.nextDouble();

double r = Math.cbrt(v * (3 / 4.0) * (7 / 22.0));


System.out.println("Radius of sphere = " + r);
}
}
Question 5

A trigonometrical expression is given as:


(Tan A - Tan B) / (1 + Tan A * Tan B)

Write a program to calculate the value of the given expression by


taking the values of angles A and B (in degrees) as input.

Hint: radian= (22 / (7 * 180)) * degree


import java.util.Scanner;

public class KboatTrigExp


{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.print("Enter angle A in double angleARad = (22 * angleADeg) / (7 * 180);


degrees: "); double angleBRad = (22 * angleBDeg) / (7 * 180);
double angleADeg = in.nextDouble();
double numerator =
System.out.print("Enter angle B in Math.tan(angleARad) - Math.tan(angleBRad);
degrees: ");
double angleBDeg = in.nextDouble(); double demoninator =
1 + Math.tan(angleARad) * Math.tan(angleBRad);

double trigExp = numerator / demoninator;

System.out.println("tan(A - B) = " + trigExp);


}
}
Question 6

The standard form of quadratic equation is represented as:


ax2 + bx + c = 0
where d= b2 - 4ac, known as 'Discriminant' of the equation.

Write a program to input the values of a, b and c. Calculate the value


of discriminant and display the output to the nearest whole number.
import java.util.Scanner;

public class KboatQuadEq


{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.print("Enter a: ");
int a = in.nextInt();

System.out.print("Enter b: "); System.out.print("Enter c: ");


int b = in.nextInt(); int c = in.nextInt();

double d = Math.pow(b, 2) - (4 * a * c);


long roundedD = Math.round(d);

System.out.println("Discriminant to the nearest


whole number = " + roundedD);
}

}
Conditional statements
in java

You might also like