Page 1 of 1

Problem with my simple factorial N computing Java program

Posted: Thu Jan 26, 2012 6:23 pm
by Nipuna
Hi

When I was surfing the net, I just thought to search about Flow charts.

Then I found the wiki page about it.
http://en.wikipedia.org/wiki/Flowchart#Examples

In there I saw this simple flow chart.
327px-FlowchartExample[1].png
327px-FlowchartExample[1].png (48.67 KiB) Viewed 6126 times
And I made a program for that in Java. But it doesn't work as expected.

If I enter "1" I get correct result.

But if I enter another amount, Then nothing happens it only prints "1' no matter what amount I enter :? I tried to correct but I couldn't. I even searched on Google to find out a similar program but they are different than my code.

Here is the code

Code: Select all

import java.util.Scanner;

class WikiFlowChartDemo{
    
    public static void main(String args[]){
        
        Scanner input = new Scanner(System.in);
        
        System.out.println("Enter N");
        int n= input.nextInt();
        int m = 1;
        int f = 1;
        
        f=f*m;
        
        if(m==n){     
            System.out.println(f);
        }else{
            m=m+1; // I could use m++ but just used this one.
            
        }
        System.out.println(f);
    }
   
}

please have a look at it and help me.


Thanks

Re: Problem with my simple factorial N computing Java program

Posted: Thu Jan 26, 2012 7:53 pm
by Herath
When you input 1, it will print the value of f since m==n. But nothing is happening other than m++ when it comes to the "else" part. And at the end of the program you are printing the value of "f" which is not affected by any code segment other than first f=f*m after initializing the variable.

This is a sample that accords the flow chart

Code: Select all

import java.util.Scanner;
 
class WikiFlowChartDemo{
   
    public static void main(String args[]){
       
        Scanner input = new Scanner(System.in);

        System.out.println("Enter N");
        int n= input.nextInt();
        int m = 1;
        int f = 1;

       do{
           f*=m;
          if(m==n)break;
          else m++;

       }while(true);
        
        System.out.println(f);;
    }
   
}
Another way to easily calculate factorials is to use recursion. :)

Re: Problem with my simple factorial N computing Java program

Posted: Thu Jan 26, 2012 10:03 pm
by Nipuna
Thank you friend.

After a long time :)

By the way, Cool avatar you got here ;)

by the way, do you have any books or practice questions in Java ? If you have tell me then I can ask from you to give them to me. They will help a lot for me to learn ? :) (I found some online, but I mean ones that you got from SLITT etc..)

Re: Problem with my simple factorial N computing Java program

Posted: Thu Jan 26, 2012 10:14 pm
by Herath
Oh god!. It is not an Avatar. :oops: . It is me!!!. :laughing:

Books..eh?. Mmm... How about sending you my brain?. :D . I have no specific books, all are in my head. Had a java text book from SLIIT. But it was just primary stuff. The course had lot more than that.

Re: Problem with my simple factorial N computing Java program

Posted: Thu Jan 26, 2012 10:19 pm
by Nipuna
Yeah, I understand that is your image :)

I forgot to tell it before :)

How about sending your brain ? ;) May I come to take it :D

Re: Problem with my simple factorial N computing Java program

Posted: Thu Jan 26, 2012 10:22 pm
by Herath
Sure sure. :mrgreen:

Re: Problem with my simple factorial N computing Java program

Posted: Thu Jan 26, 2012 10:25 pm
by Nipuna
:mrgreen: