Problem with my simple factorial N computing Java program

Java programming topics
Post Reply
User avatar
Nipuna
Moderator
Moderator
Posts: 2729
Joined: Mon Jan 04, 2010 8:02 pm
Location: Deraniyagala,SRI LANKA

Problem with my simple factorial N computing Java program

Post by Nipuna » Thu Jan 26, 2012 6:23 pm

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 6094 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
User avatar
Herath
Major
Major
Posts: 417
Joined: Thu Aug 05, 2010 7:09 pm

Re: Problem with my simple factorial N computing Java program

Post by Herath » Thu Jan 26, 2012 7:53 pm

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. :)
User avatar
Nipuna
Moderator
Moderator
Posts: 2729
Joined: Mon Jan 04, 2010 8:02 pm
Location: Deraniyagala,SRI LANKA

Re: Problem with my simple factorial N computing Java program

Post by Nipuna » Thu Jan 26, 2012 10:03 pm

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..)
User avatar
Herath
Major
Major
Posts: 417
Joined: Thu Aug 05, 2010 7:09 pm

Re: Problem with my simple factorial N computing Java program

Post by Herath » Thu Jan 26, 2012 10:14 pm

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.
User avatar
Nipuna
Moderator
Moderator
Posts: 2729
Joined: Mon Jan 04, 2010 8:02 pm
Location: Deraniyagala,SRI LANKA

Re: Problem with my simple factorial N computing Java program

Post by Nipuna » Thu Jan 26, 2012 10:19 pm

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
User avatar
Herath
Major
Major
Posts: 417
Joined: Thu Aug 05, 2010 7:09 pm

Re: Problem with my simple factorial N computing Java program

Post by Herath » Thu Jan 26, 2012 10:22 pm

Sure sure. :mrgreen:
User avatar
Nipuna
Moderator
Moderator
Posts: 2729
Joined: Mon Jan 04, 2010 8:02 pm
Location: Deraniyagala,SRI LANKA

Re: Problem with my simple factorial N computing Java program

Post by Nipuna » Thu Jan 26, 2012 10:25 pm

:mrgreen:
Post Reply

Return to “Java Programming”