Java Lecture 7 (Practice set)
THE ROYAL GROUP OF PAK
Java Lecture 7 (Practice set)
In this Lecture, we are going to check java practically and solve quations problems
This practice set has all thing which we learned till now so watch video and get command on Java
Problems :
Write a Java program to convert temperature from Fahrenheit to Celsius degree.
Write a Java program that reads a number in inches, converts it to meters.
Write a Java program that reads 4 integers between 0 and 1000 and calculate sum.
Write a Java program to convert minutes into a number of years and days.
Write a Java program that accepts two integers from the user and then prints the sum, the difference, the product, the average, the distance (the difference between integer), the maximum (the larger of the two integers), the minimum (smaller of the two integers).
We cover topics in this lecture (Practice set) are about Strings, methods, typecastingm operators, Variables and many more
Watch Lecture 5 now 👇
This is the source code we learn in lecture 5
public class RGP_07_ps {
public static void main(String[] args) {
// Q 1:
/*
Write a Java program to convert temperature from Fahrenheit to Celsius degree.
*/
/* Scanner sc = new Scanner(System.in);
System.out.println("Enter a degree in fahrenheit: ");
double fahrenheit = sc.nextDouble();
double celsius = (( 5 * (fahrenheit - 32.0)) /9.0);
System.out.println(fahrenheit + " is equal to " + celsius + "celsius");
*/
// Q 2:
/*
Write a Java program that reads a number in inches, converts it to meters.
*/
/* Scanner sc = new Scanner(System.in);
System.out.println("Enter a value of inch: ");
double inch = sc.nextDouble();
double meters = inch * 0.00254;
System.out.println(inch + " inches is " + meters + "meters");
*/
// Q 3:
/*
Write a Java program that reads 4 integers between 0 and 1000 and calculate sum.
*/
/* Scanner sc = new Scanner(System.in);
System.out.println("Enter 1st digit");
int num1 = sc.nextInt();
System.out.println("Enter 2nd digit");
int num2 = sc.nextInt();
System.out.println("Enter 3rd digit");
int num3 = sc.nextInt();
System.out.println("Enter 4th digit");
int num4 = sc.nextInt();
int sum = num1 + num2 + num3 + num4;
System.out.println("The sum all numbers is: " + sum);
*/
// Q 4:
/*
Write a Java program to convert minutes into a number of years and days.
*/
/* double minutesInYears = 60 * 24 * 365;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value of minutes");
double min = sc.nextDouble();
long years = (long) (min / minutesInYears);
int days = (int) (min / 60 / 24) % 365;
System.out.println((int) min + " minutes are approx " + years + " years " + days + " days ");
*/
// Q 5:
/*
Write a Java program that accepts two integers from the user and then prints the
sum, the difference, the product, the average, the distance (the difference
between integer), the maximum (the larger of the two integers), the minimum
(smaller of the two integers).
*/
Scanner sc = new Scanner(System.in);
System.out.println("Enter 1st number");
int firstNum = sc.nextInt();
System.out.println("Enter 2nd number");
int secondNum = sc.nextInt();
System.out.printf("The sum of numbers is: %d%n", firstNum + secondNum);
System.out.printf("The difference of numbers is: %d%n", firstNum - secondNum);
System.out.printf("The product of numbers is: %d%n", firstNum * secondNum);
System.out.printf("The average of numbers is: %.2f%n", (double) firstNum + secondNum /2);
System.out.printf("The difference of numbers is: %d%n", Math.abs(firstNum - secondNum));
System.out.printf("The Max number is: %d%n", Math.max(firstNum, secondNum));
System.out.printf("The min number is: %d%n", Math.min(firstNum, secondNum));
}
}
Comments
Post a Comment