Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: What does this code do?

  1. #1
    Junior Member
    Join Date
    Sep 2021
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post What does this code do?

    Here is a code to check if a number is prime or not

    public class PrimeExample{    
     public static void main(String args[]){    
      int i,m=0,flag=0;      
      int n=3;//it is the number to be checked    
      m=n/2;      
      if(n==0||n==1){  
       System.out.println(n+" is not prime number");      
      }else{  
       for(i=2;i<=m;i++){      
        if(n%i==0){      
         System.out.println(n+" is not prime number");      
         flag=1;      
         break;      
        }      
       }      
       if(flag==0)  { System.out.println(n+" is prime number"); }  
      }//end of else  
    }    
    }

    I wonder what are the variables m and flag meant for?

    P.S:I just found this program on the internet, i didn't write it.
    Last edited by Seth Lau.20; December 5th, 2021 at 01:43 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: What does this code do?

    what are the variables m and flag meant for?
    Read the code to see what values those variables hold.
    What is in m?
    What is in flag?
    In what conditions are they given values?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 6
    Last Post: August 5th, 2014, 11:19 AM
  2. Replies: 3
    Last Post: April 27th, 2013, 07:19 AM
  3. Replies: 4
    Last Post: January 24th, 2013, 11:20 AM
  4. Trouble Porting my Java File Reading Code to Android Code
    By Gravity Games in forum Android Development
    Replies: 0
    Last Post: December 6th, 2012, 04:38 PM
  5. Replies: 3
    Last Post: September 3rd, 2012, 11:36 AM