Skip to content

Solustion q4 java #1517

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions FIRST-java-Q1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//Write a program to print whether a number is even or odd, also take
//input from the user.
import java.util.Scanner;
public class FIRST-java-Q1
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();

if (num % 2 == 0) {
System.out.println(num + " is an even number.");
}
else {
System.out.println(num + " is an odd number.");
}

// Close the scanner to prevent resource leaks
sc.close();
}
29 changes: 29 additions & 0 deletions FIRST_java_Q3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Write a program to input principal, time, and rate (P, T, R) from the user and
//find Simple Interest.

import java.util.Scanner;

public class FIRST_java_Q3 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// Input principal, time, and rate
System.out.print("Enter principal (P): ");
double principal = scanner.nextDouble();

System.out.print("Enter time in years (T): ");
double time = scanner.nextDouble();

System.out.print("Enter rate of interest (R): ");
double rate = scanner.nextDouble();

// Calculate Simple Interest
double simpleInterest = (principal * time * rate) / 100;

// Output the result
System.out.printf("Simple Interest is: %.2f%n", simpleInterest);

// Close the scanner
scanner.close();
}
}
53 changes: 53 additions & 0 deletions FIRST_java_Q4.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//Take in two numbers and an operator (+, -, *, /) and calculate the value.
//(Use if conditions)
import java.util.Scanner;

public class FIRST_java_Q4 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// Input first number
System.out.print("Enter first number: ");
double num1 = scanner.nextDouble();

// Input second number
System.out.print("Enter second number: ");
double num2 = scanner.nextDouble();

// Input operator
System.out.print("Enter operator (+, -, *, /): ");
char operator = scanner.next().charAt(0);

double result;

// Perform calculation based on the operator
if (operator == '+') {
result = num1 + num2;
}
else if (operator == '-') {
result = num1 - num2;
}
else if (operator == '*') {
result = num1 * num2;
}
else if (operator == '/') {
if (num2 != 0) {
result = num1 / num2;
} else {
System.out.println("Error: Division by zero is not allowed.");
scanner.close();
return;
}
} else {
System.out.println("Error: Invalid operator.");
scanner.close();
return;
}

// Output the result
System.out.printf("Result: %.2f%n", result);

// Close the scanner
scanner.close();
}
}
14 changes: 14 additions & 0 deletions FIRSTjava-Q2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Take name as input and print a greeting message for that particular name.
class Q2
{
public static void main(String[] args) {
java.util.Scanner sc = new java.util.Scanner(System.in);
System.out.print("Enter your name: ");
String name = sc.nextLine();

System.out.println("Hello, " + name + "! Welcome to the program.");

// Close the scanner to prevent resource leaks
sc.close();
}
}
18 changes: 18 additions & 0 deletions FLOW_Q3_Krish.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Take a number as input and print the multiplication table for it.
class FLOW_Q3_Krish
{
public static void main(String[] args)
{
java.util.Scanner sc = new java.util.Scanner(System.in);
System.out.print("Enter a number to print its multiplication table: ");
int num = sc.nextInt();

System.out.println("Multiplication table for " + num + ":");
for (int i = 1; i <= 10; i++) {
System.out.println(num + " x " + i + " = " + (num * i));
}
//
// Close the scanner to prevent resource leaks
sc.close();
}
}
30 changes: 30 additions & 0 deletions FLOW_Q4_Krish.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Take 2 numbers as inputs and find their HCF and LCM.
public class FLOW_Q4_Krish
{
public static void main(String[] args)
{
java.util.Scanner sc = new java.util.Scanner(System.in);
System.out.print("Enter first number: ");
int num1 = sc.nextInt();
System.out.print("Enter second number: ");
int num2 = sc.nextInt();

int hcf = findHCF(num1, num2);
int lcm = (num1 * num2) / hcf;

System.out.println("HCF of " + num1 + " and " + num2 + " is: " + hcf);
System.out.println("LCM of " + num1 + " and " + num2 + " is: " + lcm);

// Close the scanner to prevent resource leaks
sc.close();
}

private static int findHCF(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
}
29 changes: 29 additions & 0 deletions FLOW_Q5_Krish.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//Keep taking numbers as inputs till the user enters ‘x’, after that print sum of all.
public class FLOW_Q5_Krish
{
public static void main(String[] args)
{
java.util.Scanner sc = new java.util.Scanner(System.in);
int sum = 0;
String input;

System.out.println("Enter numbers to sum them up. Type 'x' to finish:");
while (true) {
input = sc.nextLine();
if (input.equalsIgnoreCase("x")) {
break;
}
try {
int num = Integer.parseInt(input);
sum += num;
} catch (NumberFormatException e) {
System.out.println("Invalid input, please enter a number or 'x' to exit.");
}
}

System.out.println("The total sum is: " + sum);

// Close the scanner to prevent resource leaks
sc.close();
}
}
18 changes: 18 additions & 0 deletions FLow_Q2_Krish .java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Take two numbers and print the sum of both.
import java.util.Scanner;
class FLow_Q2_Krish
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
int num1 = sc.nextInt();
System.out.print("Enter second number: ");
int num2 = sc.nextInt();

int sum = num1 + num2;
System.out.println("The sum of " + num1 + " and " + num2 + " is: " + sum);
// Close the scanner to prevent resource leaks
sc.close();
}
}
4 changes: 2 additions & 2 deletions lectures/20-trees/code/AVL/.project
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
</natures>
<filteredResources>
<filter>
<id>1679078569810</id>
<id>1752678436405</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
Expand Down
4 changes: 2 additions & 2 deletions lectures/20-trees/code/Questions/.project
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
</natures>
<filteredResources>
<filter>
<id>1679078569810</id>
<id>1752678436432</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
Expand Down