# use a nested loop or loop with a mathematical method.

Page 1 of 1

## 11 Replies - 844 Views - Last Post: 10 July 2012 - 09:32 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=285266&amp;s=8acb7f2cc06983f9bb4a5dd1961e82b5&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Aoua

Reputation: 0
• 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

• D.I.C Lover

Reputation: 1973
• 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?

### #3 GregBrannon

• D.I.C Lover

Reputation: 1973
• 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

### #4 Aoua

Reputation: 0
• 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

### #5 GregBrannon

• D.I.C Lover

Reputation: 1973
• 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)
decrement user's input
:end loop.

```

Can you code that?

### #6 Aoua

Reputation: 0
• 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!

### #7 no2pencil

• Original Digital Gansta

Reputation: 4462
• 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.

### #8 Aoua

Reputation: 0
• 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);
}
}
```

### #9 Aoua

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

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

Posted 09 July 2012 - 11:40 PM

Aoua, 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

### #10 no2pencil

• Original Digital Gansta

Reputation: 4462
• 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.

### #11 GregBrannon

• D.I.C Lover

Reputation: 1973
• 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.

```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 );
}
}
```

### #12 macosxnerd101

• Self-Trained Economist

Reputation: 9025
• 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

Page 1 of 1

 Query failed: connection to localhost:3312 failed (errno=111, msg=Connection refused).