Skip to content

Solustion q2 first java #1514

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 7 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();
}
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