Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In java 2. Write a program that calculates 1 + 3 + 5 + 7 + … + 99 (40 pts) (on s

ID: 3883175 • Letter: I

Question

In java 2. Write a program that calculates 1 + 3 + 5 + 7 + … + 99 (40 pts) (on slides page 56)
a. By using for loop
b. By using while loop
c. By using do-while loop
3. Ask the user to enter an integer n (n 1). Write a program that calculates 1! + 2! + 3! + 4! + … + n! (40 pts) (on slides page 57)
a. By using for loop
b. By using while loop
c. By using do-while loop Extra credits:
4. Write a program that displays the multiplication table shown as follows: (20 pts) (on slides page 58)
a. By using for loop
b. By using while loop
c. By using do-while loop In java 2. Write a program that calculates 1 + 3 + 5 + 7 + … + 99 (40 pts) (on slides page 56)
a. By using for loop
b. By using while loop
c. By using do-while loop
3. Ask the user to enter an integer n (n 1). Write a program that calculates 1! + 2! + 3! + 4! + … + n! (40 pts) (on slides page 57)
a. By using for loop
b. By using while loop
c. By using do-while loop Extra credits:
4. Write a program that displays the multiplication table shown as follows: (20 pts) (on slides page 58)
a. By using for loop
b. By using while loop
c. By using do-while loop In java 2. Write a program that calculates 1 + 3 + 5 + 7 + … + 99 (40 pts) (on slides page 56)
a. By using for loop
b. By using while loop
c. By using do-while loop
3. Ask the user to enter an integer n (n 1). Write a program that calculates 1! + 2! + 3! + 4! + … + n! (40 pts) (on slides page 57)
a. By using for loop
b. By using while loop
c. By using do-while loop Extra credits:
4. Write a program that displays the multiplication table shown as follows: (20 pts) (on slides page 58)
a. By using for loop
b. By using while loop
c. By using do-while loop

Explanation / Answer

Write a program that calculates 1 + 3 + 5 + 7 + … + 99.
a. By using for loop

import java.util.*;
import java.lang.*;
import java.io.*;
public class SumOfOddNumbers
{
public static void main(String args[])
{
int sum = 0;
for (int i = 1; i <= 100; i++)
{
if (i % 2 != 0)
{
sum = sum + i;
}
}
System.out.println("The Sum Of 100 Odd Numbers are:" + sum);
}
}

a. By using while loop

import java.util.*;
import java.lang.*;
import java.io.*;
public class SumOfOddNumbers
{
public static void main(String args[])
{
int sum = 0;
int i = 1;
while(i<=100 )
{
if (i % 2 != 0)
{
sum = sum + i;
i=i+2;
}
}
System.out.println("The Sum Of 100 Odd Numbers are:" + sum);
}
}


c. By using do-while loop:

import java.util.*;
import java.lang.*;
import java.io.*;
public class SumOfOddNumbers
{
public static void main(String args[])
{
int sum = 0;
int i = 1;
do
{
if (i % 2 != 0)
{
sum = sum + i;
i=i+2;
}
}while(i<=100 );
System.out.println("The Sum Of 100 Odd Numbers are:" + sum);
}
}

3. Ask the user to enter an integer n (n 1). Write a program that calculates 1! + 2! + 3! + 4! + … + n! (40 pts)

By using for loop:

import java.util.Scanner;

public class SumOfFactorials {
public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
System.out.print("Enter number: ");
int n = sc.nextInt();
int i;
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
fact = fact * j;
}
sum = sum + fact;
fact = 1;
}
System.out.println("Sum: " + total);
}
}

By using while loop:

import java.util.Scanner;

public class SumOfFactorials {
public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
System.out.print("Enter number: ");
int n = sc.nextInt();

int total=0;

int i=1;
while(i <= n) {
int factorial=1;
int j=1;
while(j <= i) {
factorial=factorial*j;
j = j+1;
}
total = total + factorial;
i=i+1;
}
System.out.println("Sum: " + total);
}
}


By using do-while:

import java.util.Scanner;

public class SumOfFactorials {
public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
System.out.print("Enter number: ");
int n = sc.nextInt();

int total=0;

int i=1;
do {
int factorial=1;
int j=1;
do {
factorial=factorial*j;
j = j+1;
}while(j <= i);
total = total + factorial;
i=i+1;
}while(i <= n);
System.out.println("Sum: " + total);
}
}

Write a program that displays the multiplication table shown as follows: (20 pts) (on slides page 58)

a. By using for loop

import java.util.Scanner;

public class Multiplication_Table

{

public static void main(String[] args)

{

Scanner s = new Scanner(System.in);

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

int number=s.nextInt();

for(int i=1; i <= 10; i++)

{

System.out.println(number+" * "+i+" = "+number*i);

}

}

}

b. By using while loop

import java.util.Scanner;

public class Multiplication_Table

{

public static void main(String[] args)

{

Scanner s = new Scanner(System.in);

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

int number=s.nextInt();

int i=1;

while(i<=10)

  

{

System.out.println(number+" * "+i+" = "+number*i);

i++;

}

}

}

c. By using do-while loop

import java.util.Scanner;

public class Multiplication_Table

{

public static void main(String[] args)

{

Scanner s = new Scanner(System.in);

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

int number=s.nextInt();

int i=1;

  

do

{

System.out.println(number+" * "+i+" = "+number*i);

i++;

}while(i<=10);

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote