click on this link......

LinkGrand.com

Wednesday 23 January 2013

To find prime numbers in an array and to calculate the sum of those prime numbers

#include<stdio.h>
#include<conio.h>
void main()
{
    int a[50],count,c,n,i,j,sum=0;
    clrscr();
    printf("Enter the no. of elements:");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    count=0;
    printf("\nprime numbers are:\n");
    for(i=0;i<n;i++)
    {
        c=0;
        for(j=2;j<a[i];j++)
        {
            if(a[i]%j==0)
            {
                   c=1;
                   break;
            }
        }
         if(c==0)
         {
            printf("%d\t",a[i]);
            sum=sum+a[i];
            count++;

         }

    }
    printf("\nThe number of prime numbers is %d",count);
    printf("\nThe sum of prime no. is %d",sum);

    getch();
}

To find prime numbers in an array and to calculate the sum of those prime numbers
To find prime numbers in an array and to calculate the sum of those prime numbers

15 comments:

  1. 1 is not a prime number. is there any way that i enter 1 in array but it did not calculte 1 as prime number... i m havine the same issue by iostream.h

    ReplyDelete
    Replies
    1. #Mady Gee, Here is the correct program:
      #include
      using namespace std;
      int main()
      {
      int a[50],c,n,sum=0;
      cout<<"Enter the no. of elements:";
      cin>>n;
      for(int i=0;i>a[i];
      }
      cout<<"\nprime numbers are:\n";
      for(int i=0;i<n;i++)
      {
      c=0;
      for(int j=2;j<=a[i];j++)
      {
      if(a[i]%j==0)
      {
      c++;
      }
      }
      if(c==1)
      {
      cout<<"\t"<<a[i];
      sum=sum+a[i];

      }

      }
      cout<<"\nThe sum of prime no. is "<<sum;


      }

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. find the number of prime numbers count, store them into a separate array and display it.

    ReplyDelete
  4. This code is correct ?Prime Number Program in C

    #include
    #include
    void main()
    {
    int num,res=0;
    clrscr();
    printf("\nENTER A NUMBER: ");
    scanf("%d",&num);
    res=prime(num);
    if(res==0)
    printf("\n%d is a prime number",num);
    else
    printf("\n%d is not a prime number",num);
    getch();
    }
    int prime(int n)
    {
    int i;
    for(i=2;i<=n/2;i++)
    {
    if(n%i!=0)
    continue;
    else
    return 1;
    }
    return 0;
    }

    ReplyDelete
  5. Why is this incorrect?
    #include
    #include
    void main()
    {
    int n;
    printf("Enter the numbr of elements:");
    scanf("%d",&n);
    int arra[n];

    getch();
    clrscr();
    }

    ReplyDelete
  6. #include
    #include
    pls enter the correct header file

    ReplyDelete
  7. There is no need to use break in the program
    Programs rums properly avoiding break

    ReplyDelete
  8. ckeck code at http://www.programmingsimplysolved.com/c-programs/c-program-to-check-prime-number-or-not/

    ReplyDelete
  9. How the count value is increasing

    ReplyDelete
  10. #include

    using namespace std;

    int main()
    {
    int arr[5],i,odd=0,j,k;
    cout<<"Read array:";
    for(i=0;i<=4;i++)
    cin>>arr[i];
    cout<<"i have read the following array:"<<endl;
    for(i=0;i<=4;i++)
    cout<<arr[i]<<endl;
    for(i=0;i<=4;i++)
    {
    if(arr[i]%2==1)
    odd++;
    }
    cout<<"odds are:"<<odd<<endl;
    }
    how i can make the list of odd numbers

    ReplyDelete