0% found this document useful (0 votes)
67 views2 pages

Write A Java Program To Compute A Specified Formula. Specified Expression: 4.0 (1 - (1.0/3) + (1.0/5) - (1.0/7) + (1.0/9) - (1.0/11) )

The document contains code for two Java programs. The first program calculates a specified formula using terms with alternating signs and prints the result. The second program calculates a number raised to a power using a while loop, taking the base and exponent as input from the user and printing the result.

Uploaded by

Ahmad ALi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views2 pages

Write A Java Program To Compute A Specified Formula. Specified Expression: 4.0 (1 - (1.0/3) + (1.0/5) - (1.0/7) + (1.0/9) - (1.0/11) )

The document contains code for two Java programs. The first program calculates a specified formula using terms with alternating signs and prints the result. The second program calculates a number raised to a power using a while loop, taking the base and exponent as input from the user and printing the result.

Uploaded by

Ahmad ALi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1- Write a Java program to compute a specified formula. Specified Expression: 4.

0 * (1 -
(1.0/3) + (1.0/5) - (1.0/7) + (1.0/9) - (1.0/11))

public class Main {


public static void main(String[] args) {
double formulaResult = 4.0 * (1 - (1.0/3) + (1.0/5) - (1.0/7) + (1.0/9) - (1.0/11));
System.out.println("Result of the specified formula: " + formulaResult);

int base = 2, exponent = 10;


double powerResult = 1;
int i = 1;
while (i <= exponent) {
powerResult *= base;
i++;
}
System.out.println(base + "^" + exponent + " = " + powerResult);
}
}
2- Write a Java Program to Calculate Power of a Number using a while loop.

import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter base: ");
int base = input.nextInt();
System.out.print("Enter exponent: ");
int exponent = input.nextInt();
input.close();

double result = 1;
int i = 1;
while (i <= exponent) {
result *= base;
i++;
}
System.out.println(base + "^" + exponent + " = " + result);
}
}

You might also like