[go: up one dir, main page]

0% found this document useful (0 votes)
9 views6 pages

Java Programs

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 6

JAVA PROGRAMS

1. write a java program to print even number and odd number from 1 to
Number
import java.util.Scanner;

public class EvenOddNumbers {


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

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


int number = scanner.nextInt();

System.out.println("Even numbers:");
for (int i = 1; i <= number; i++) {
if (i % 2 == 0) {
System.out.print(i + " ");
}
}

System.out.println("\nOdd numbers:");
for (int i = 1; i <= number; i++) {
if (i % 2 != 0) {
System.out.print(i + " ");
}
}

scanner.close();
}
}

2. write a java program to find largets of 3 numbers


import java.util.Scanner;

public class LargestNumber {


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

System.out.println("Enter three numbers:");


double num1 = scanner.nextDouble();
double num2 = scanner.nextDouble();
double num3 = scanner.nextDouble();

double largest = num1; // Assume num1 is the largest initially

if (num2 > largest) {


largest = num2; // If num2 is greater than current largest, update largest
}

if (num3 > largest) {


largest = num3; // If num3 is greater than current largest, update largest
}

System.out.println("The largest number is: " + largest);

scanner.close();
}
}

3. write a java program to find power of a number


import java.util.Scanner;

public class PowerOfNumber {


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

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


double base = scanner.nextDouble();

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


int exponent = scanner.nextInt();

double result = power(base, exponent);

System.out.println(base + " raised to the power of " + exponent + " is: " + result);

scanner.close();
}

// Method to calculate power


public static double power(double base, int exponent) {
if (exponent == 0) {
return 1;
} else if (exponent > 0) {
double result = 1;
for (int i = 0; i < exponent; i++) {
result *= base;
}
return result;
} else {
double result = 1;
for (int i = 0; i < -exponent; i++) {
result *= base;
}
return 1 / result;
}
}
}

4. write a java program to find square root of a number


import java.util.Scanner;

public class SquareRoot {


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

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


double number = scanner.nextDouble();

double sqrt = squareRoot(number);

System.out.println("Square root of " + number + " is: " + sqrt);

scanner.close();
}

// Method to calculate square root using Newton-Raphson method


public static double squareRoot(double number) {
if (number < 0) {
return Double.NaN; // NaN for negative numbers
}

double x = number; // Initial guess for square root

// Iteratively refine the guess using Newton-Raphson method


while (true) {
double sqrt = 0.5 * (x + number / x);
if (Math.abs(sqrt - x) < 1e-8) { // Check if the difference is within tolerance
break; // If so, break the loop
}
x = sqrt; // Update the guess
}

return x; // Return the calculated square root


}
}

5. write a java program to find gcd of a given number


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

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


int num1 = scanner.nextInt();

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


int num2 = scanner.nextInt();

int gcd = findGCD(num1, num2);

System.out.println("The GCD of " + num1 + " and " + num2 + " is: " + gcd);

scanner.close();
}

// Method to find GCD using the Euclidean algorithm


public static int findGCD(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
}

6. write a ajava program to calculate profit or loss


import java.util.Scanner;

public class ProfitOrLoss {


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

System.out.print("Enter the cost price: ");


double costPrice = scanner.nextDouble();

System.out.print("Enter the selling price: ");


double sellingPrice = scanner.nextDouble();

double profitOrLoss = sellingPrice - costPrice;

if (profitOrLoss > 0) {
System.out.println("You made a profit of: $" + profitOrLoss);
} else if (profitOrLoss < 0) {
System.out.println("You incurred a loss of: $" + (-profitOrLoss));
} else {
System.out.println("You neither made a profit nor incurred a loss.");
}

scanner.close();
}
}

7. write a java program to display alphabet A to Z by using ASCII value


public class PrintAlphabets {
public static void main(String[] args) {
// ASCII value for 'A' is 65 and 'Z' is 90
int asciiValueA = 65;
int asciiValueZ = 90;

System.out.println("Alphabets from A to Z:");

// Loop through ASCII values from A to Z and print corresponding characters


for (int i = asciiValueA; i <= asciiValueZ; i++) {
System.out.print((char) i + " ");
}
}
}

8. write a java program to find factorial of a number


import java.util.Scanner;

public class Factorial {


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

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


int number = scanner.nextInt();

long factorial = calculateFactorial(number);

System.out.println("Factorial of " + number + " is: " + factorial);

scanner.close();
}

// Method to calculate factorial


public static long calculateFactorial(int n) {
if (n < 0) {
return -1; // Factorial is not defined for negative numbers
}
long factorial = 1;
for (int i = 1; i <= n; i++) {
factorial *= i;
}
return factorial;
}
}

You might also like