Program File
1. How to use Visual Studio:
This is the program file to implement the program of Merge Sort.
We will use Visual Studio to implement JAVA programming.
Visual Studio is an integrated development environment (IDE) developed by
Microsoft. It is used by software developers to write, edit, debug, and
compile code in various programming languages.
We can use a variety of programming languages in Visual Studio.
It consists of various templates and workings to make a program code.
To start working on a new file, we click on the new file button on the
starting screen.
As soon as we click on the new file, a pop up appears in the top of the page.
This pop up gives us the option of working on whatever file type we want to
work on.
After selecting text file, an untitled text file will open in visual studio.
After this, click “select a language” given in the heading text and select Java.
This way all the code done in the studio will be executed as JAVA source
code.
After selecting JAVA, we start our coding procedure.
2. Making the main body of code:
import [Link];
Imports the Scanner class from the Java library so you can read input from
the user via the console.
JAVA Questions
[Link]
public class Factorial {
→ Declares a public class named Factorial.
static int factorial(int n) {
→ Defines a static method factorial that takes an integer n and returns its
factorial.
if (n == 0 || n == 1)
→ Checks if the number is 0 or 1, since the factorial of both is 1.
return 1;
→ Returns 1 for base cases.
return n * factorial(n - 1);
→ Recursively calls factorial(n - 1) and multiplies the result by n.
}
→ Ends the factorial method.
public static void main(String[] args) {
→ Main method: program execution starts here.
Scanner sc = new Scanner([Link]);
→ Creates a Scanner object sc for input.
[Link]("Enter a number: ");
→ Prompts the user to enter a number.
int num = [Link]();
→ Reads the integer input and stores it in num.
[Link]("Factorial of " + num + " is " + factorial(num));
→ Calls the factorial method and prints the result.
}
}
→ Ends the main method and the class.
[Link]
import [Link];
→ Imports the Scanner class, which is used to take input from the user.
public class Armstrong {
→ Declares a public class named Armstrong.
public static void main(String[] args) {
→ Entry point of the Java program.
Scanner sc = new Scanner([Link]);
→ Creates a Scanner object sc for reading user input.
[Link]("Enter a number: ");
→ Prompts the user to enter a number.
int num = [Link]();
→ Reads the number entered by the user and stores it in the variable num.
int original = num, result = 0;
→ Stores the original number in original and initializes result to 0.
int digits = [Link](num).length();
→ Calculates the number of digits in the number using the string length
method.
while (num != 0) {
→ Starts a loop that runs while num is not zero.
int digit = num % 10;
→ Extracts the last digit of num.
result += [Link](digit, digits);
→ Adds the digit raised to the power of the total number of digits to result.
num /= 10;
→ Removes the last digit from num.
}
→ Ends the while loop.
if (result == original)
→ Compares the calculated sum with the original number.
[Link]("Armstrong number");
→ If they match, prints that it's an Armstrong number.
else
[Link]("Not an Armstrong number");
→ If they don't match, prints that it's not an Armstrong number.
}
}
→ Ends the main method and the class.
[Link]
import [Link];
→ Imports the Scanner class to take user input from the console.
public class QuadraticRoots {
→ Declares a public class named QuadraticRoots.
public static void main(String[] args) {
→ Main method: the program starts execution here.
Scanner sc = new Scanner([Link]);
→ Creates a Scanner object named sc to read input from the user.
[Link]("Enter coefficients a, b, c: ");
→ Prompts the user to input the three coefficients of the quadratic equation.
double a = [Link]();
double b = [Link]();
double c = [Link]();
→ Reads and stores the coefficients a, b, and c entered by the user.
double discriminant = b * b - 4 * a * c;
→ Calculates the discriminant (D = b² - 4ac), which determines the nature of
the roots.
if (discriminant > 0) {
→ If the discriminant is positive, there are two real and distinct roots.
double root1 = (-b + [Link](discriminant)) / (2 * a);
double root2 = (-b - [Link](discriminant)) / (2 * a);
→ Calculates both roots using the quadratic formula.
[Link]("Roots are real and different.");
[Link]("Root 1 = " + root1);
[Link]("Root 2 = " + root2);
→ Prints the nature and values of the roots.
} else if (discriminant == 0) {
→ If the discriminant is zero, the roots are real and equal.
double root = -b / (2 * a);
→ Calculates the repeated root.
[Link]("Roots are real and same.");
[Link]("Root = " + root);
→ Prints the nature and value of the root.
} else {
→ If the discriminant is negative, the roots are complex (imaginary).
double realPart = -b / (2 * a);
double imaginaryPart = [Link](-discriminant) / (2 * a);
→ Computes the real and imaginary parts of the complex roots.
[Link]("Roots are complex and different.");
[Link]("Root 1 = " + realPart + " + " + imaginaryPart + "i");
[Link]("Root 2 = " + realPart + " - " + imaginaryPart + "i");
→ Prints the complex roots in standard form.
[Link]
import [Link];
→ Imports the Scanner class to take user input.
public class ReverseNumber {
→ Declares a public class named ReverseNumber.
public static void main(String[] args) {
→ The entry point of the program.
Scanner sc = new Scanner([Link]);
→ Creates a Scanner object sc to read input from the user.
[Link]("Enter a number: ");
→ Prompts the user to enter a number.
int num = [Link]();
→ Reads the number from the user and stores it in the variable num.
int reversed = 0;
→ Declares and initializes a variable reversed to store the reversed number.
while (num != 0) {
→ Loops until num becomes 0.
int digit = num % 10;
→ Extracts the last digit of num using modulus operator.
reversed = reversed * 10 + digit;
→ Appends the extracted digit to the reversed number.
num /= 10;
→ Removes the last digit from num by dividing it by 10.
}
→ Ends the while loop.
[Link]("Reversed number: " + reversed);
→ Prints the reversed number.
}
}
→ Ends the main method and class.
[Link]
import [Link];
→ Imports the Scanner class for reading user input.
public class PalindromeCheck {
→ Declares a public class named PalindromeCheck.
public static void main(String[] args) {
→ Entry point of the program where execution starts.
Scanner sc = new Scanner([Link]);
→ Creates a Scanner object to read input from the user.
[Link]("Enter a number: ");
→ Prompts the user to enter a number.
int num = [Link]();
→ Reads the entered number and stores it in the variable num.
int original = num;
→ Stores the original value of num in a separate variable for later comparison.
int reversed = 0;
→ Initializes a variable to store the reversed number.
while (num != 0) {
→ Starts a loop that continues until all digits of num have been processed.
int digit = num % 10;
→ Extracts the last digit of the number.
reversed = reversed * 10 + digit;
→ Appends the digit to the reversed number.
num /= 10;
→ Removes the last digit from num.
}
→ Ends the loop.
if (original == reversed)
→ Compares the original number with its reversed version.
[Link]("Palindrome number");
→ If they are equal, prints that it is a palindrome.
else
[Link]("Not a palindrome number");
→ If they are not equal, prints that it is not a palindrome.
}
}
→ Closes the main method and the class.