Thread: non prime numbers or composite numbers program.help plz!

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    64

    non prime numbers or composite numbers program.help plz!

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main()
    {
    int num;
    while (num<=100) /*Limit*/
    {if(num%2==0)  /*Condition for composite or non prime numbers*/
    break;
    if ((num!=2)); /* as 2 is prime so it is excluded*/
    printf("\n%d",num);/* print that non prime numbers*/
    num++; /*increment for checking next number*/
    }
    getch ();
    }

    not giving any output.what i am lacking???
    Last edited by danishzaidi; 11-15-2011 at 06:33 AM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Not initialising num, for starters, so it's initial value is indeterminate.

    Also breaking out of the loop the first time num is divisible by 2.

    getch() and <conio.h> are also non-standard.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    64
    ok i defined num=1
    it just printed 1
    what should i do with the loop now? i couldn't got any idea about the breaking of loop?

  4. #4
    Registered User
    Join Date
    Oct 2011
    Location
    Denmark
    Posts
    80
    As grumpy said before, you are breaking the loop when a number is divisible by 2, which means the program will exit the while loop (and if you start with num=1, that will occur when num=2, which is why you have only 1 in output).

    If I understand what you want to do, you want to print any non prime number, so you should just have a condition saying "if that number is not divisible by 2 and is not 2" then "print it", this should be enough.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    64
    Quote Originally Posted by Tibo-88 View Post
    As grumpy said before, you are breaking the loop when a number is divisible by 2, which means the program will exit the while loop (and if you start with num=1, that will occur when num=2, which is why you have only 1 in output).

    If I understand what you want to do, you want to print any non prime number, so you should just have a condition saying "if that number is not divisible by 2 and is not 2" then "print it", this should be enough.
    can you make changes to the code for me.

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    64
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main()
    {
    int num=1;
    while (num<=100)
    {if((num%2==0)&& (num!=2));
    printf("\n%d",num);
    num++;
    }
    getch ();
    }
    changed the code now,it is printing prime numbers also.

  7. #7
    Registered User
    Join Date
    Oct 2011
    Location
    Denmark
    Posts
    80
    You should work a bit in your indentation, it will help you avoiding this kind of mistake. You have a ";" at the end of your if statement that you should remove, because now, if your condition is true, it does nothing, but the printf will be executed no matter what. Also the num<=100 is not so good, you should use num!=101 or num<101 instead.

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    72
    Code:
    #include <stdio.h>
    
    
    
    p(int asal)
    {
    int y=1;
    
    
    int i4;
    for(i4=2;i4<asal;i4++)
    {
    if (asal%i4==0)
    {
    y=0;
    }
    else
    y=1*y;
    }
    return y;
    }
    
    
    
    
    int main()
    {
    int i;
    
    
    
    
    for(i=0;i<=100;i++)
    {
    if((i==0)||(i==1)) printf("%d\n",i);
    if (p(i)==1) continue;
    else printf("%d\n",i);
    
    
    }
    }

    look this. if you want to print all the prime numbers make p(i)==0
    Last edited by rac1; 11-15-2011 at 09:37 AM.

  9. #9
    Registered User
    Join Date
    Oct 2011
    Location
    Denmark
    Posts
    80
    Quote Originally Posted by rac1 View Post
    Code:
    #include <stdio.h>
    
    
    
    p(int asal)
    {
    int y=1;
    
    
    int i4;
    for(i4=2;i4<asal;i4++)
    {
    if (asal%i4==0)
    {
    y=0;
    }
    else
    y=1*y;
    }
    return y;
    }
    
    
    
    
    int main()
    {
    int i;
    
    
    
    
    for(i=0;i<=100;i++)
    {
    if((i==0)||(i==1)) printf("%d\n",i);
    if (p(i)==1) continue;
    else printf("%d\n",i);
    
    
    }
    }

    look this. if you want to print all the prime numbers make p(i)==0
    I didn't run your code, but two things are bothering me while reading it :

    Code:
    //FIRST
      if(p(i)==1) continue;
      else printf("%d\n", i);
    //can be expressed as 
      if(p(i)!=1) printf("%d\n", i);
    
    //SECOND
      y=1*y; //is this really necessary?

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Please indent and format your code correctly.


    Quote Originally Posted by rac1 View Post
    Code:
    #include <stdio.h>
    
    
    
    p(int asal)
    {
    int y=1;
    
    
    int i4;
    for(i4=2;i4<asal;i4++)
    {
    if (asal%i4==0)
    {
    y=0;
    }
    else
    y=1*y;
    }
    return y;
    }
    
    
    
    
    int main()
    {
    int i;
    
    
    
    
    for(i=0;i<=100;i++)
    {
    if((i==0)||(i==1)) printf("%d\n",i);
    if (p(i)==1) continue;
    else printf("%d\n",i);
    
    
    }
    }

    look this. if you want to print all the prime numbers make p(i)==0

  11. #11
    Registered User
    Join Date
    Nov 2011
    Posts
    72
    Code:
    #include <stdio.h>
    
    p(int asal)
    {
    int y=1;
    int i4;
    
    
    for(i4=2;i4<asal;i4++)
    {
       if (asal%i4==0)  y=0; 
    }
    return y;
    }
    
    int main()
    {
    int i;
    for(i=0;i<=100;i++)
    {
      if((i==0)||(i==1)) printf("%d\n",i);
    
        if (p(i)==1) continue;
        else printf("%d\n",i);
    
    }
    getchar();
    return 0;
    }
    i was using " code blocks" program, there wasnt any error.Now it is working on visual studio 2006 C program. Sorry for that reason.
    Thanks Tater, my program is very short and useful now
    Last edited by rac1; 11-15-2011 at 11:17 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Prime numbers program
    By Prestige in forum C Programming
    Replies: 3
    Last Post: 10-16-2010, 01:57 PM
  2. Program the prints prime numbers
    By cloudstrife910 in forum C++ Programming
    Replies: 8
    Last Post: 09-22-2010, 03:03 PM
  3. /i need help in C- program for prime numbers plzZzZ
    By linda in forum C Programming
    Replies: 13
    Last Post: 03-04-2008, 06:44 AM
  4. C Program for Prime Numbers
    By mmuhlenb in forum C Programming
    Replies: 12
    Last Post: 02-19-2003, 04:55 AM
  5. Help with program code re prime numbers
    By Anna Lane in forum C Programming
    Replies: 3
    Last Post: 11-16-2002, 10:48 AM