11 Replies - 844 Views - Last Post: 10 July 2012 - 09:32 AM Rate Topic: -----

#1 Aoua  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 07-July 12

use a nested loop or loop with a mathematical method.

Posted 09 July 2012 - 12:51 PM

is my code wrong! in writing the program?
the program is to use nested loop or loop with a mathematical method.
to find factorial R= 1! + 2! +3! + 4! + 6! + …….. + 9! + 10!
import java.util.Scanner;
public class q3 { 
    public static void main (String[ ] args) { 
        System.out.println("Enter the number for factorial calculations : ");
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();  
        double fact= 1;    
        System.out.println("Factorial of " +a+ ":");  
        for (int i= 1; i<=a; i++){       
            fact=fact*i;     
        }      System.out.println(fact);  
    }
}


how to convert it to nested loop or math method

Is This A Good Question/Topic? 0
  • +

Replies To: use a nested loop or loop with a mathematical method.

#2 GregBrannon  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1973
  • View blog
  • Posts: 4,810
  • Joined: 10-September 10

Re: use a nested loop or loop with a mathematical method.

Posted 09 July 2012 - 01:03 PM

What's the problem? The equation for R you included is a sum of factorials rather than just the factorial as your program currently calculates. For the input 5, your program does:

Enter the number for factorial calculations :
5
Factorial of 5:
120.0

Is it supposed to instead calculate R = 1! + 2! + 3! + 4! + 5! and then output R?

Are you familiar with recursion?
Was This Post Helpful? 0
  • +
  • -

#3 GregBrannon  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1973
  • View blog
  • Posts: 4,810
  • Joined: 10-September 10

Re: use a nested loop or loop with a mathematical method.

Posted 09 July 2012 - 01:52 PM

Don't PM me, don't start duplicate threads. Continue the discussion in this thread. Answer my questions.
Was This Post Helpful? 0
  • +
  • -

#4 Aoua  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 07-July 12

Re: use a nested loop or loop with a mathematical method.

Posted 09 July 2012 - 01:59 PM

the question is

Write a program to find the result of the following equation:

R= 1! + 2! +3! + 4! + 6! + …….. + 9! + 10!

Where ! Is the factorial function defined as following, 0! = 1,
And n! = 1*2*3*…….*(n-1)*n.
You need to you use looping statement, think of nested loop, or a loop with a mathematical method.

im not sure about my code so need more explain..
thanks
Was This Post Helpful? 0
  • +
  • -

#5 GregBrannon  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1973
  • View blog
  • Posts: 4,810
  • Joined: 10-September 10

Re: use a nested loop or loop with a mathematical method.

Posted 09 July 2012 - 02:10 PM

I'll assume you don't know about recursion, so I won't include it in the discussion.

Your code already calculates the factorial of the number entered by the user, say 5. How could you modify your code with a nested loop to find the factorials of 4, 3, 2, and 1 (unnecessary) and add those to the first result 5? In pseudo code:
get user's input
loop:
while user's input > 1,
   find factorial of user's input
      ( the line above could be the mathematical method)
add resulting factorial to total
decrement user's input
:end loop.


Can you code that?
Was This Post Helpful? 0
  • +
  • -

#6 Aoua  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 07-July 12

Re: use a nested loop or loop with a mathematical method.

Posted 09 July 2012 - 06:26 PM

Write a program to find the result of the following equation:

R= 1! + 2! +3! + 4! + 6! + …….. + 9! + 10!

Where ! Is the factorial function defined as following, 0! = 1,
And n! = 1*2*3*…….*(n-1)*n.
You need to you use looping statement, think of nested loop, or a loop with a mathematical method.



i got confused in question what its means by R= 1! + 2! +3! + 4! + 6! + …….. + 9! + 10!
how to use it in code !
its an easy question but dont know the steps to solve!
Was This Post Helpful? 0
  • +
  • -

#7 no2pencil  Icon User is offline

  • Original Digital Gansta
  • member icon

Reputation: 4462
  • View blog
  • Posts: 24,905
  • Joined: 10-May 07

Re: use a nested loop or loop with a mathematical method.

Posted 09 July 2012 - 06:28 PM

As most topics are questions about code that has an error, I renamed the title to be more descriptive of the discussion. Please use meaningful topics when creating topics.
Was This Post Helpful? 0
  • +
  • -

#8 Aoua  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 07-July 12

Re: use a nested loop or loop with a mathematical method.

Posted 09 July 2012 - 08:35 PM

Write a program to find the result of the following equation:

R= 1! + 2! +3! + 4! + 6! + …….. + 9! + 10!
Where ! Is the factorial function defined as following, 0! = 1,
And n! = 1*2*3*…….*(n-1)*n.
You need to you use looping statement, think of nested loop, or a loop with a mathematical method.



what its means by R= 1! + 2! +3! + 4! + 6! + …….. + 9! + 10! how to do it in code !

this mylast code need help to solve it please

 import java.util.Scanner;
public class t { 
    public static void main (String[ ] args) { 
        System.out.println("Enter the number for factorial calculations : ");
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();  
        int fact= 1;
        
        System.out.println("Factorial of " +a+ ":");  
        for (int i= 1; i<=a; i++){       
            fact=fact*i;
              }
        int t=0;
            for(int x=1;x<a;x++){
                t=t+x;
                System.out.println("R!= "+t);
            }System.out.println("factorial!= "+fact); 
        }     
    }

Was This Post Helpful? 0
  • +
  • -

#9 Aoua  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 07-July 12

Re: use a nested loop or loop with a mathematical method.

Posted 09 July 2012 - 11:40 PM

View PostAoua, on 09 July 2012 - 08:35 PM, said:

Write a program to find the result of the following equation:

R= 1! + 2! +3! + 4! + 6! + …….. + 9! + 10!
Where ! Is the factorial function defined as following, 0! = 1,
And n! = 1*2*3*…….*(n-1)*n.
You need to you use looping statement, think of nested loop, or a loop with a mathematical method.



what its means by R= 1! + 2! +3! + 4! + 6! + …….. + 9! + 10! how to do it in code !

this mylast code need help to solve it please

 import java.util.Scanner;
public class t { 
    public static void main (String[ ] args) { 
        System.out.println("Enter the number for factorial calculations : ");
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();  
        int fact= 1;
        
        System.out.println("Factorial of " +a+ ":");  
        for (int i= 1; i<=a; i++){       
            fact=fact*i;
              }
        int t=0;
            for(int x=1;x<a;x++){
                t=t+x;
                System.out.println("R!= "+t);
            }System.out.println("factorial!= "+fact); 
        }     
    }

any help :dontgetit: :helpsmilie:
Was This Post Helpful? 0
  • +
  • -

#10 no2pencil  Icon User is offline

  • Original Digital Gansta
  • member icon

Reputation: 4462
  • View blog
  • Posts: 24,905
  • Joined: 10-May 07

Re: use a nested loop or loop with a mathematical method.

Posted 09 July 2012 - 11:44 PM

Please don't bump your topic. It's 2:30am EST, & with most of our members being in the United States, most people are offline.
Was This Post Helpful? 0
  • +
  • -

#11 GregBrannon  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1973
  • View blog
  • Posts: 4,810
  • Joined: 10-September 10

Re: use a nested loop or loop with a mathematical method.

Posted 10 July 2012 - 03:53 AM

I'm unable to run the code below, so there may be minor errors that my eyeball syntax checker is not catching. Please read my comments in the code and understand what is being done. Also, it's not done yet: There's cleanup to do on the print statements, and I'm not sure it fulfills the assignment's requirements for a "nested loop or a loop with a mathematical method." A nested loop is not required (in my opinion), but you could write a method that returns the factorial of a value passed to it. Such a method would simplify the code in the for loop.

Let us know if you have any questions, and I apologize for any errors I didn't catch.

And please come back with specific questions, not pleas for answers.
import java.util.Scanner;

public class t // GB: a lousy name for a class
{ 
    public static void main (String[ ] args)
    { 
        System.out.println("Enter the number for factorial calculations : ");
        Scanner scanner = new Scanner(System.in);

        // GB: single-letter variable names are not appropriate except as
        // GB: loop control variables or indices
        int a = scanner.nextInt();  
        int fact= 1;
        
        // GB: added variable for R (explained below)
        int factSum = 0;

        // GB: not sure you want this here        
        System.out.println("Factorial of " +a+ ":");
        
        // GB: but this will set up printing the derivation of R
        // GB: (moved from below)
        System.out.print("R! = " );
        
        for (int i = 1; i <= a; i++)
        {
            // GB: this does compute the factorial for each i
            fact=fact*i;
            
            // GB: to compute R, you want to sum the factorials
            // GB: up to and including the user's value.  If the
            // GB: user inputs 5, you want:
            // GB: R = 1! + 2! + 3! + 4! + 5!
            // GB: If you were to work it out on paper, you might
            // GB: notice that each subfactorial is computed on the
            // GB: way to the user's number.  You simply need to sum
            // GB: the subfactorial results:
            
            // GB: sums the factorials
            factSum += fact;
            
            // GB: then you can print each subfactorial with each pass
            // GB: through the loop
            
            // GB: print the derivation of R with each factorial
            // GB: the extra '+' will have to be dealt with (use an if)
            System.out.print( fact " + " );
        }
        
        // GB: then print the resulting R
        System.out.println( " = " + factSum );
    }     
}

Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9025
  • View blog
  • Posts: 33,462
  • Joined: 27-December 08

Re: use a nested loop or loop with a mathematical method.

Posted 10 July 2012 - 09:32 AM

Since a factorial is n*(n-1)*(n-2), etc., you will only need a single loop.

An algorithm:
[code]
sumOfFacts(int n)
sum <-- 0
fact <-- 1

for i <-- 1, i <= n, i <-- i+1
fact <-- fact * i
sum <-- sum + fact
end for
end
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1