Web and Software Development

Google
 

Friday, November 16, 2007

C Program to find All Prime Numbers from 1 to 100

1. Program to findout all Prime Number between 1 to 100.


void main()
{
int i,num=1;
clrscr();

while(num<=100)
{ i=2; while(i<=num)
{ if(num%i==0)
break;
i++; }
if(i==num)
printf("\n%d is Prime",num);
num++;
}
getch();
}

Note: You can modify the first while loop to increase the range.


Now you can check output of this program at http://codepad.org/B4eMfCxI

24 comments:

  1. using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace ConsoleApplication1
    {
    class Program
    {
    static void Main(string[] args)
    {
    int num ,count;
    bool stat=false;
    Console.WriteLine("Enter the number");
    num = Convert.ToInt32(Console.ReadLine());

    for (int i = 1; i <= num; i++)
    {
    count = 0;

    for (int j = 1; j <= i; j++)
    {
    if ((i % j) == 0)
    stat = true;
    if (stat)
    {
    count++;
    stat = false;
    }

    }

    if(count==2)
    Console.WriteLine(i);


    }
    Console.ReadLine();
    }
    }
    }

    ReplyDelete
  2. using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace ConsoleApplication1
    {
    class Program
    {
    static void Main(string[] args)
    {
    int num ,count;
    bool stat=false;
    Console.WriteLine("Enter the number");
    num = Convert.ToInt32(Console.ReadLine());

    for (int i = 1; i <= num; i++)
    {
    count = 0;

    for (int j = 1; j <= i; j++)
    {
    if ((i % j) == 0)
    stat = true;
    if (stat)
    {
    count++;
    stat = false;
    }

    }

    if(count==2)
    Console.WriteLine(i);


    }
    Console.ReadLine();
    }
    }
    }

    ReplyDelete
  3. thanks Shariq Khan that was unique n easy

    ReplyDelete
  4. Program to find 1st n prime no:


    int main()
    {
    int n,i=0,a[200],j,d=3;
    printf("Enter the no of terms::");
    scanf("%d",&n);
    a[i]=2;
    while(i!=n)
    {
    for(j=0;a[j]<d/2;j++)
    if(d%a[j]==0)
    {
    d+=2;j=0;
    }
    a[++i]=d;
    d+=2;
    }
    for(i=0;i<n;i++)
    printf("%d, ",a[i]);
    return 0;
    }

    ReplyDelete
  5. Excellent job!!!!!!!!!!!!!

    Its really appreciable........

    ReplyDelete
  6. excuse me ... after execution of first if statement whether it will go to second if condition or to for loop

    ReplyDelete
  7. Three programmers presented the codes, how do you say that it is not running and also without giving the error details.

    ReplyDelete
  8. gr8 job guys..., that was appreciable..,

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete
  10. Welcome guys, Soon I will post more codes, tips and tricks, keep visiting.

    ReplyDelete
  11. hey shariq, good work!
    best of luck 4 future....

    ReplyDelete
  12. Well Done!.... thanks to provide it

    ReplyDelete
  13. what if there's no specification on its limits ?

    ReplyDelete
  14. Just simply remove the limits and check what will happen !

    ReplyDelete
  15. #include
    #include
    void main()
    {
    Int i, j, C;
    printf("Enter the no upto which you want to find out the prime numbers");
    Scanf("%d", &n);
    for(i=2; i<=n; i++)
    {
    for(j=2; j<i; j++);
    {
    If(i%j==0)
    {
    c++;
    }
    }
    if(c==0)
    {
    printf("%d", i)
    }
    c=0;
    }
    getch();
    }

    ReplyDelete
  16. #include
    #include
    main()
    {
    int i,j;
    for(i=3;i<=300;i++)
    {
    for(j=2;j<=i;j++)
    {
    if(i%j!=0)
    {}
    else if(i%j==0)
    break;
    }
    if(j==i)
    printf("%d%s\n",i,"is prime");
    }
    getch();
    return 0;
    }

    ReplyDelete

Boomark My Blog @