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

2. Write a program that calculates 1 + 3 + 5 + 7 + … + 99 (40 pts) (on slides pa

ID: 3882878 • Letter: 2

Question

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

NOTE: NOW, I am doing these questions in Java

Solution 2) A program that calculates 1 + 3 + 5 + 7 + … + 99

a) using for loop

public class Sum
{
   public static void main(String args[])
   {
       int sum=0;
       for(int i=1;i<=99;i=i+2)
       {
           sum=sum+i;
       }
       System.out.println("Result is : "+sum);
   }
}

b) using while loop

public class Sum1
{
   public static void main(String args[])
   {
       int sum=0,i=1;
       while(i<=99)
       {
           sum=sum+i;
           i=i+2;
       }
       System.out.println("Result is : "+sum);
   }
}

c) using do-while loop

public class Sum2
{
   public static void main(String args[])
   {
       int sum=0,i=1;
       do
       {
           sum=sum+i;
           i=i+2;
       }while(i<=99);
       System.out.println("Result is : "+sum);
   }
}

Solution 3) A program that calculates 1! + 2! + 3! + 4! + … + n!

a) using for loop

import java.util.Scanner;   //to use nextInt() of Scanner class
public class Fact
{
   public static void main(String args[])
   {
       Scanner obj=new Scanner(System.in);   //creating object of Scanner class
       int n,sum=0,fact=1;
       System.out.println("Enter an Integer :");
       n=obj.nextInt();       //using nextInt() of Scanner class
       if(n>=1)
       {
           for(int i=1;i<=n;i++)
           {      
               fact=fact*i;
               sum=sum+fact;
           }
       }  
       System.out.println("Result is : "+sum);
   }
}

b) using while loop

import java.util.Scanner;       //to use nextInt() of Scanner class
public class Fact1
{
   public static void main(String args[])
   {
       Scanner obj=new Scanner(System.in);   //creating object of Scanner class
       int n,sum=0,fact=1,i=1;
       System.out.println("Enter an Integer :");
       n=obj.nextInt();       //using nextInt() of Scanner class
       if(n>=1)
       {
           while(i<=n)
           {      
               fact=fact*i;
               sum=sum+fact;
               i++;
           }
       }  
       System.out.println("Result is : "+sum);
   }
}

c) using do-while loop

import java.util.Scanner;   //to use nextInt() of Scanner class
public class Fact2
{
   public static void main(String args[])
   {
       Scanner obj=new Scanner(System.in);   //creating object of Scanner class
       int n,sum=0,fact=1,i=1;
       System.out.println("Enter an Integer :");
       n=obj.nextInt();       //using nextInt() of Scanner class
       if(n>=1)
       {
           do
           {      
               fact=fact*i;
               sum=sum+fact;
               i++;
           }while(i<=n);
       }  
       System.out.println("Result is : "+sum);
   }
}

Solution 4) A program that displays the multiplication table

a) using for loop

import java.util.Scanner;
public class Table
{
   public static void main(String args[])
   {
       Scanner obj=new Scanner(System.in);  
       int n;
       System.out.println("Enter a Number for its table :");
       n=obj.nextInt();
       System.out.println();
       for(int i=1;i<=10;i++)
       {
           System.out.println(n+" X "+i+" = "+n*i);         
       }
   }
}

b) using while loop

import java.util.Scanner;
public class Table2
{
   public static void main(String args[])
   {
       Scanner obj=new Scanner(System.in);  
       int n,i=1;
       System.out.println("Enter a Number for its table :");
       n=obj.nextInt();
       System.out.println();
       do
       {
           System.out.println(n+" X "+i+" = "+n*i);
           i++;
       }while(i<=10);
   }
}

c) using do-while loop

import java.util.Scanner;
public class Table1
{
   public static void main(String args[])
   {
       Scanner obj=new Scanner(System.in);  
       int n,i=1;
       System.out.println("Enter a Number for its table :");
       n=obj.nextInt();
       System.out.println();
       while(i<=10)
       {
           System.out.println(n+" X "+i+" = "+n*i);
           i++;
       }
   }
}

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