0% found this document useful (0 votes)
4 views16 pages

Fraction Sums and Factorials in Java

The document contains a series of Java programs that calculate sums of fractions based on different conditions, such as odd and even denominators, and user-defined inputs. Each part of the document includes code snippets, outputs, and comments by the author, Yashank Rathi. The programs demonstrate various mathematical concepts including factorials, square roots, and series summation.

Uploaded by

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

Fraction Sums and Factorials in Java

The document contains a series of Java programs that calculate sums of fractions based on different conditions, such as odd and even denominators, and user-defined inputs. Each part of the document includes code snippets, outputs, and comments by the author, Yashank Rathi. The programs demonstrate various mathematical concepts including factorials, square roots, and series summation.

Uploaded by

aqua7879635
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Question 1

Part i
package Chapter3;
public class Question1 {
public static void main(String[] args) {
//Yashank Rathi 10/14/24
//printing odd denominator sum question 1 part
1

//declare variables
double fractionSum = 0,num = 1;

//calculate
for(int i = 3; i<=99; i++) {
if(i%2!=0) {
fractionSum += num / i;
[Link]("The sum is " +
fractionSum +"!");
}
}

}
}

Output:
The sum is 0.3333333333333333!
The sum is 0.5333333333333333!
The sum is 0.6761904761904762!
The sum is 0.7873015873015874!
The sum is 0.8782106782106783!
The sum is 0.9551337551337553!
The sum is 1.021800421800422!
The sum is 1.0806239512121867!
The sum is 1.133255530159555!
The sum is 1.1808745777786027!
The sum is 1.224352838648168!
The sum is 1.264352838648168!
The sum is 1.301389875685205!
The sum is 1.3358726343058946!
The sum is 1.3681306988220236!
The sum is 1.398433729125054!
The sum is 1.4270051576964824!
The sum is 1.4540321847235094!
The sum is 1.479673210364535!
The sum is 1.504063454266974!
The sum is 1.5273192682204624!
The sum is 1.5495414904426845!
The sum is 1.5708180861873653!
The sum is 1.5912262494526714!
The sum is 1.6108340925899263!
The sum is 1.6297020171182282!
The sum is 1.6478838353000462!
The sum is 1.665427694949169!
The sum is 1.682376847491542!
The sum is 1.6987702901144928!
The sum is 1.7146433059875086!
The sum is 1.730027921372124!
The sum is 1.7449532945064523!
The sum is 1.7594460481296408!
The sum is 1.7735305551718943!
The sum is 1.7872291853088806!
The sum is 1.800562518642214!
The sum is 1.813549531629227!
The sum is 1.8262077594773283!
The sum is 1.838553438489674!
The sum is 1.8506016312607583!
The sum is 1.8623663371431112!
The sum is 1.8738605900166745!
The sum is 1.8850965450728543!
The sum is 1.8960855560618652!
The sum is 1.9068382442339082!
The sum is 1.917364560023382!
The sum is 1.9276738383738974!
The sum is 1.9377748484749076!

Part ii)
package Chapter3;
public class Question2 {
public static void main(String[] args) {
//Yashank Rathi 10/14/24
//printing even denominator sum question 1 part
1

//declare variables
double fractionSum = 0,num = 1;

//calculate
for(int i = 2; i<=100; i++) {
if(i%2==0) {
fractionSum += num / i;
[Link]("The sum is " +
fractionSum +"!");
}
}
}
}

Output:
The sum is 0.5!
The sum is 0.75!
The sum is 0.9166666666666666!
The sum is 1.0416666666666665!
The sum is 1.1416666666666666!
The sum is 1.2249999999999999!
The sum is 1.2964285714285713!
The sum is 1.3589285714285713!
The sum is 1.4144841269841268!
The sum is 1.4644841269841269!
The sum is 1.5099386724386723!
The sum is 1.551605339105339!
The sum is 1.5900668775668776!
The sum is 1.6257811632811634!
The sum is 1.6591144966144968!
The sum is 1.6903644966144968!
The sum is 1.719776261320379!
The sum is 1.7475540390981568!
The sum is 1.773869828571841!
The sum is 1.798869828571841!
The sum is 1.8226793523813647!
The sum is 1.8454066251086374!
The sum is 1.86714575554342!
The sum is 1.8879790888767534!
The sum is 1.9079790888767534!
The sum is 1.9272098581075225!
The sum is 1.9457283766260411!
The sum is 1.963585519483184!
The sum is 1.9808268987935287!
The sum is 1.9974935654601953!
The sum is 2.01362259771826!
The sum is 2.02924759771826!
The sum is 2.044399112869775!
The sum is 2.0591049952227163!
The sum is 2.0733907095084305!
The sum is 2.0872795983973194!
The sum is 2.100793111910833!
The sum is 2.113951006647675!
The sum is 2.1267715194681878!
The sum is 2.139271519468188!
The sum is 2.1514666414194075!
The sum is 2.1633714033241693!
The sum is 2.1749993103009135!
The sum is 2.1863629466645498!
The sum is 2.197474057775661!
The sum is 2.208343622993052!
The sum is 2.2189819208653923!
The sum is 2.2293985875320588!
The sum is 2.2396026691647117!
The sum is 2.2496026691647115!

Part iii)
package Chapter3;
public class Question3 {
public static void main(String[] args) {
//declare variables
double fractionSum = 0,num = 1;

//calculate
for(int i = 4; i<=100; i++) {
if(i%4==0) {
fractionSum += num / i;
[Link]("The sum is " +
fractionSum +"!");
}
}
}
}

Output:
The sum is 0.25!
The sum is 0.375!
The sum is 0.4583333333333333!
The sum is 0.5208333333333333!
The sum is 0.5708333333333333!
The sum is 0.6124999999999999!
The sum is 0.6482142857142856!
The sum is 0.6794642857142856!
The sum is 0.7072420634920634!
The sum is 0.7322420634920634!
The sum is 0.7549693362193362!
The sum is 0.7758026695526695!
The sum is 0.7950334387834388!
The sum is 0.8128905816405817!
The sum is 0.8295572483072484!
The sum is 0.8451822483072484!
The sum is 0.8598881306601895!
The sum is 0.8737770195490784!
The sum is 0.8869349142859205!
The sum is 0.8994349142859205!
The sum is 0.9113396761906823!
The sum is 0.9227033125543187!
The sum is 0.93357287777171!
The sum is 0.9439895444383767!
The sum is 0.9539895444383767!

Part iv
package Chapter3;
public class Question4 {
public static void main(String[] args) {
//Yashank Rathi 10/14/24
//Writing a program that prints fractions, and
sums them up in the end question1 part 4

//declare variables
double fractionSum=0;

//calculate
for(int i = 0;i<=99;i++) {
fractionSum+=(double)i/(i+1);
}

//print answer
[Link]("The sum of the fraction is:
"+ fractionSum + "!");
}
}

Output:
The sum of the fraction is: 94.81262248236033!

Part v}
package Chapter3;
import [Link];
public class Question5 {
public static void main(String[] args) {
//Yashank Rathi 10/14/24
//Printing the factorial value of the
denomiator.

// Declare variables
double sum = 0.0;
int n, factorial = 1;
Scanner kbd = new Scanner([Link]);

//ask user
[Link]("Hi can you give me a number,
and I will give you the factorial number in fraction or
decimal.");
[Link]("Enter the value of n: ");
n = [Link]();
//calculate
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) { //factorial
calc
factorial *= j;
}
sum += (double) i / factorial;
}
//print answer
[Link]("The sum of the series
fractions is: " + sum);
[Link]();
}
}

Output:
Hi can you give me a number, and I will give you the
factorial number in fraction or decimal.
Enter the value of n: 4
The sum of the series fractions is: 2.263888888888889

Part vi)
package Chapter3;
import [Link];
public class Question6 {
public static void main(String[] args) {
//Yashank Rathi 10/14/24
//Printing the factorial value of the
denomiator.

// Declare variables
double factorSum = 0;
int n, factorial = 1;
Scanner kbd = new Scanner([Link]);

//ask user
[Link]("Hi can you give me a
number, and I will give you the factorial number in
fraction or decimal.");
[Link]("Enter the value of n: ");
n = [Link]();
//calculate
for (int i = 1; i <= n; i+=4) {
for (int j = 3; j <= i; j++) {
//factorial calc
factorial *= j;
}
factorSum += (double) i /
factorial;
}
//print answer
[Link]("The sum of the
series of the fractions is: " + factorSum);
[Link]();
}
}

Output:
Hi can you give me a number, and I will give you the
factorial number in fraction or decimal.
Enter the value of n: 3
The sum of the series of the fractions is: 1.0

Part vii)
package Chapter3;
import [Link];
public class Question7 {
public static void main(String[] args) {
//Yashank Rathi 10/10/24
//Printing the square roots of the denominator
user gives question part 7.

//declare variables
int num,factorSum=0;
Scanner kbd = new Scanner([Link]);

//ask user
[Link]("Hi, can you give me a
number, and I will remove the square of the number you
gave.");
[Link]("Enter a number:");
num=[Link]();

//calculate
for (int i = 1; i<=num;i++){
factorSum+=[Link](i,2);
}

//print answer
[Link]("The sum of the fractions
is:" + factorSum + "!");
[Link]();
}
}

Output:
Hi, can you give me a number, and I will remove the
square of the number you gave.
Enter a number:
5
The sum of the fractions is:25!

Part viii)

package Chapter3;
import [Link];
public class Question8 {
public static void main(String[] args) {
//Yashank Rathi 10/10/24
//Multiplying the values of x given by the user
with exponents increasing by one. Question 1 part 8

//declare variables
int num,x;
double fractionSum=0;
Scanner kbd = new Scanner([Link]);

//ask user
[Link]("Hi,can you give me the
values a the numerators and the x value, and I will
print the sum, after multiplying the denominators
exponents which increase by 1.");
[Link]("Enter a number for the
numerator:");
num=[Link]();
[Link]("Enter a x value:");
x=[Link]();

//calculate
for(int i = 1; i<=num; i++) {
fractionSum+=(double)1/([Link](x, i));
}

//print answer
[Link]("The sum of the fraction is
" + fractionSum + "!");
[Link]();
}
}

Output:
Hi,can you give me the values a the numerators and the
x value, and I will print the sum, after multiplying
the denominators exponents which increase by 1.
Enter a number for the numerator:
2
Enter a x value:
4
The sum of the fraction is 0.3125!
Part ix)
package Chapter3;
import [Link];
public class Question9 {
public static void main(String[] args) {
//Yashank Rathi 10/14/24
//Printing the sum of all odd numbers given by
the user. question 1 part 9

//declare variables
int num = 0,oddSum = 0,userInput;
Scanner kbd = new Scanner([Link]);

//ask user
[Link]("Hi,can you give me a number
which can give me an idea of how many numbers you want
to input.");
[Link]("Enter a number to define
how many time you want to enter odd numbers:");
userInput = [Link]();
[Link]("Now can you provide me the
" + userInput + " odd numbers, and I will add those up
and give you the sum.");
for(int x = 0; x<userInput; x++) {
[Link]("Enter a odd number:");
num = [Link]();
}
//calculate
if(num%2!=0) {
oddSum += num;
}

//print answer
[Link]("The sum of the odd numbers
is: " + oddSum + "!");
[Link]();
}
}

Output:
Hi,can you give me a number which can give me an idea
of how many numbers you want to input.
Enter a number to define how many time you want to
enter odd numbers:
2
Now can you provide me the 2 odd numbers, and I will
add those up and give you the sum.
Enter a odd number:6
Enter a odd number:3
The sum of the odd numbers is: 3!

Hi,can you give me a number which can give me an idea


of how many numbers you want to input.
Enter a number to define how many time you want to
enter odd numbers:
2
Now can you provide me the 2 odd numbers, and I will
add those up and give you the sum.
Enter a odd number:7
Enter a odd number:3
The sum of the odd numbers is: 10!

3.10)
package Chapter3;
import [Link];
public class Question10 {
public static void main(String[] args) {
//Yashank Rathi 10/14/24
//Printing the sum of all even numbers
given by the user. question 1 part 10

//declare variables
int num = 0,evenSum = 0,userInput;
Scanner kbd = new Scanner([Link]);

//ask user
[Link]("Hi,can you give me
a number which can give me an idea of how many numbers
you want to input.");
[Link]("Enter a number to
define how many time you want to enter odd numbers:");
userInput = [Link]();
[Link]("Now can you
provide me the " + userInput + " even numbers, and I
will add those up and give you the sum.");
for(int x = 0; x<userInput; x++) {
[Link]("Enter a even
number:");
num = [Link]();
}
//calculate
if(num%2==0) {
evenSum += num;
}

//print answer
[Link]("The sum of the
even numbers is: " + evenSum + "!");
[Link]();
}
}

Output:
Hi,can you give me a number which can give me an idea
of how many numbers you want to input.
Enter a number to define how many time you want to
enter odd numbers:
2
Now can you provide me the 2 even numbers, and I will
add those up and give you the sum.
Enter a even number:2
Enter a even number:4
The sum of the even numbers is: 6!

3.11)
package Chapter3;
import [Link];
public class Question11 {
public static void main(String[] args) {
//Yashank Rathi 10/14/24
//Printing the sum of the powers of the x given
by the user. question 1 part 11.

//declare variables
int exponent;
double sum=0;
Scanner kbd = new Scanner([Link]);

//ask user
[Link]("Hi,can you give me the
exponent value, and I will print the sum, after
multiplying the denominators exponents which increase
by 1.");
[Link]("Enter an exponent for x
:");
exponent=[Link]();

//calculate
for(int i = 1; i<=exponent; i++) {
sum+=[Link](exponent, i);
}

//print answer
[Link]("The sum of the
exponents is " + sum + "!");
[Link]();
}
}
Output:
Hi,can you give me the exponent value, and I will print
the sum, after multiplying the denominators exponents
which increase by 1.
Enter an exponent for x :
2
The sum of the exponents is 6.0!

You might also like