some code explanation please?
editing the initial code to correct the syntax
Code:
import java.io.*;
class factorial
{
public static void main (String args []) throws IOException
{
int n2,fac=1;
String n1;
System.out.println("\n enter the number");
BufferedReader in= new BufferedReader(new InputStreamReader (System.in));
n1= in.readLine();
n2=Integer.parseInt(n1);
for( int i=1; i<= n2; i++)
{
fac=fac*i;
}
System.out.println("\n factorial" + fac);
}
i am new to java.. this is my first lab question.. i am in college
how the hell am i supposed to remember all these things?
Re: some code explanation please?
Perhaps you should change courses if you find that there are too many details to remember.
No one remembers all the details. You need to know where to look to find the answers.
If you have any questions about the code you posted, please ask.
Re: some code explanation please?
What are you having difficulty with? For the basics of the language, including syntax, try the tutorial. For the methods of objects like BufferedReader, look it up in the API.
A few observations:
Code:
// This won't compile. BufferedReader needs to be one word
Buffered Reader in= new Buffered Reader(new InputStreamReader (System.in));
// Neither will this. Capitalisation matters; it needs to be readLine()
n1= in.readline();
// This does nothing. What happens to a number when you multiply it by 1?
fac=fac*1;
Re: some code explanation please?
.i copied the code from someone else note book..i have a java exam coming up in a month...i failed the first time .. did not attend the second time and this is my last time ...
i am supposed to write a program that takes a number as input and displays it factorial...
teachers teaching these subjects at college suckes bawls... they have never explained anything.. even they dont have much clue...
Re: some code explanation please?
Please explain your problem and/or ask some questions about what problems you are having.
Do you understand how to compute a factorial if you do it by hand with paper and pencil?
What are the steps if you write them down, one simple step at a time to compute 5 factorial?
Look at those steps and then try to generalize what is done in each step
Re: some code explanation please?
its ok ..am leaving this shit
all i know is factorial of 5 is 5*4*3*2*1 = 120
Re: some code explanation please?
Now write down how you can do the computation by only having one multiplication (*) on each line using a variable to save the value from the last multiply.
Re: some code explanation please?
i dont know how to ?can you please show me how to ?
Re: some code explanation please?
I'll give you the first line:
fac = 1;
Now what should be the next thing you do to compute two factorial? What do you multiply fac by?
Re: some code explanation please?
n+1 ?
fac =fac*n
while n<=5 ; n++
Re: some code explanation please?
Quote:
Originally Posted by
javawreker
its ok ..am leaving this shit
If everything that people don't understand would be shit, the world would be full of it; erm, I guess the world is full of stupid people. You have to improve your knowledge and understanding or you are going to fail a third time. All you have to know is how to multiply 1*2*3*4*5 ... Java normally uses a loop for that where the loop counter represents a single term used for the multiplication.
kind regards,
Jos
Re: some code explanation please?
Close. Now write it in a program, compile and execute it.
Re: some code explanation please?
Quote:
Originally Posted by
JosAH
If everything that people don't understand would be shit, the world would be full of it; erm, I guess the world is full of stupid people. You have to improve your knowledge and understanding or you are going to fail a third time. All you have to know is how to multiply 1*2*3*4*5 ... Java normally uses a loop for that where the loop counter represents a single term used for the multiplication.
kind regards,
Jos
this is actually my 5th language which i am trying to learn...
c c++ java php asp..
everything is in my syllabus...
i was doing a php project with mysql database until yesterday...got so fed up and confused..that i decided to do java.. opened up my note and this was the first problem in it..
many of the things in java are really new to me..
i didnt mean i am leaving this forum shit... i meant i was planning to leave my college without a degree.. because this was not going anywhere
yes am really a noob..
let me see if i can get a working sample code in java to find a factorial...
Re: some code explanation please?
Quote:
Originally Posted by
javawreker
this is actually my 5th language which i am trying to learn...
c c++ java php asp..
everything is in my syllabus...
i was doing a php project with mysql database until yesterday...got so fed up and confused..that i decided to do java.. opened up my note and this was the first problem in it..
many of the things in java are really new to me..
i didnt mean i am leaving this forum shit... i meant i was planning to leave my college without a degree.. because this was not going anywhere
yes am really a noob..
let me see if i can get a working sample code in java to find a factorial...
You almost have your code; it's just the 'fac= fac*1' that doesn't make sense (it doesn't do anything); make it 'fac= fac*i' because 'i' represents the current term in the factorial product.
kind regards,
Jos
Re: some code explanation please?
ok thanks for all the help