Index-1              Index-2              Home Page

Check whether the sum of all printed prime numbers are equal to the given input or not?



Write a program to get a range from user and give a list of prime numbers up to that number.
And check whether the sum of all printed prime numbers are equal to the given input or not?

Sample Input: 10

Sample Output:
Prime list: 1, 2, 3, 5, 7
The sum is: 18
The sum is not equal to 10

Hints:
A Prime Number can be divided evenly only by 1, or itself.
Example: 5 can only be divided evenly by 1 or 5, so it is a prime number.
But 6 can be divided evenly by 1, 2, 3 and 6 so it is NOT a prime number (it is a composite number).
 



Source Code:

#include<stdio.h>
int main()
{
     int i, num, p, n, sum=0 ;
     printf ("\n Enter the range :");
     scanf ( "%d", &n);
     printf ("\n Prime list : ");
     for(num =1; num <=n; num++)
     {
           p =1;
               for(i=2; i<= num/2; i++)
                   {
                        if ( num % i= =0)
                            {
                                  p = 0;
                                  break;
                            }
                  }
           if ( p = =1)
                 {
                       printf (" %d ",num);
                       sum = sum+num;
                 }
      }
      printf("\nThe sum of prime number is: %d ",sum);
     
      if( sum = = n)
           {
                 printf ("\nThe sum of prime number is equal to %d",n);
           }
      else
           {
                 printf ("\nThe sum of prime number is not equal to %d ",n);
           }
}


14 comments:

  1. Here Is the Code

    #include stdio.h
    #include math.h


    int main()
    {
    long long num, temp, ar[100000]={0}, i, j, sum=0;


    scanf("%lld", &num);

    temp=(int)sqrt(num);

    for(i=4;i<=num;i+=2)
    {
    ar[i]=1;
    }

    for(i=3;i<=temp;i+=2)
    {
    for(j=i*i;j<=num;j+=(2*i))
    {
    ar[j]=1;
    }
    }


    printf("Prime list: ");
    for(i=1;i<=num;i++)
    {
    if(ar[i]==0)
    {
    printf("%lld, ", i);
    sum+=i;
    }
    }
    printf("\nThe Sum is : %lld", sum);
    if(sum==num)
    printf("\nThe sum is equal to %lld", num);
    else
    printf("\nThe sum is not equal to %lld", num);

    return 0;

    }

    ReplyDelete
  2. int prime_num();

    main()
    {

    prime_num();
    getch();
    }

    int prime_num()
    {
    int n,i;
    printf("\n Enter number: ");
    scanf("%d",&n);
    for(i=2;i<=n;i++)
    {
    if(n%i==1)
    printf("\n the number is prime");
    else
    printf("The number is not prime");
    return i;

    }
    }

    ReplyDelete
  3. #include
    int main()
    {

    int i,num,p,n,sum=0;
    printf("\n enter the range :");
    scanf("%d & n");
    printf("\n prime list :");
    for(num=1;num<=n;num++)
    {
    p=1;
    for(i=2;i<=num/2;i++)
    {
    if(nim%i==0)
    {
    p=0;
    break;
    }
    }
    if(p==1)
    {
    printf("%d\t",num);
    sum=sum+num;
    }
    }
    printf("the sum of prime number is:%d",sum);
    if(sum==n)
    {
    printf("\n the sum of prime number is equal to %d",n);
    }
    else
    {
    printf("\n the sum of prime number is not equal to %d",n);
    }
    }

    ReplyDelete
  4. /*
    bashir Fardoush
    ID: 201430385
    */


    #include
    using namespace std;

    void prime_range(int p);
    int main(){
    int p;
    cout<<"Enter the value: ";
    cin>>p;
    prime_range(p);
    return 0;
    }
    void prime_range(int p){

    int i,r,j,sum=0;
    cout<<"prime number list: "<<" 1 2 3 ";
    sum=1+2+3;

    for(j=4; j<=p; j++){
    int flag=1;

    for(i=2;i<=j/2;i++){
    r=j%i;
    if(r==0){
    flag=0;

    break;
    }
    }
    if(flag==1){
    cout<<j<<" ";
    sum+=j;}
    }
    cout<<endl<<"The sum is: "<<sum<<endl;
    if(sum==p)cout<<"The sum is equal to "<<p<<endl;
    else cout<<"The sum is not equal to "<<p<<endl;

    }

    ReplyDelete
  5. //Md. Moyedul Hoque, ID: 201430200

    #include
    using namespace std;

    void prime_check(int p);
    int main()
    {
    int p;
    cout<<"Enter the value : ";
    cin>>p;
    prime_check(p);
    return 0;
    }

    void prime_check(int p)
    {

    int a,b,c,sum=0;

    for(b=1;b<=p;b++)
    {
    int flag=1;

    for(a=2;a<=b/2;a++)
    {
    c = b%a;
    if(c==0)
    {
    flag=0;
    break;
    }

    }
    if(flag==1)

    {
    cout <<b<<" ";
    sum = sum + b;
    }

    }
    cout<<endl<<"Summation is : "<<sum<<endl;
    if(sum==p)
    cout<<"Equal.";
    else
    cout<<"Not equal.";
    }

    ReplyDelete
  6. ID-433
    39th Batch

    using namespace std;
    int main()
    {
    int num, temp, ar[50]={0}, i, j, sum=0;
    cin>>num;
    temp= int sqrt(num);

    for(i=4;i<=num;i+=2)
    {
    ar[i]=1;
    }

    for(i=3;i<=temp;i+=2)
    {
    for(j=i*i;j<=num;j+=(2*i))
    {
    ar[j]=1;
    }
    }


    cout"Prime list: ";
    for(i=1;i<=num;i++)
    {
    if(ar[i]==0)
    {
    cout<< i;
    sum+=i;
    }
    }
    cout<<The Sum is : << sum<<endl;
    if(sum==num)
    cout<<The sum is equal to << num<<endl;
    else
    cout<<The sum is not equal to << num<<endl;

    return 0;

    }

    ReplyDelete
  7. batch:46th

    #include
    using namespace std;
    int prime_number(int num)
    {
    int i,j,sum=0,ar[100000]={0};

    for(i=4;i<=num;i+=2)
    {
    ar[i]=1;
    }
    for(i=3;i<=num;i+=2)
    {
    for(j=i*i;j<=num;j+=(2*i))
    {
    ar[j]=1;
    }
    }
    for(i=1;i<=num;i++)
    {
    if(ar[i]==0)
    {
    cout<<" "<< i;
    sum+=i;
    }
    }
    cout<<"The Sum is : "<< sum<>num;
    cout<<"Prime list: ";
    prime_number(num);
    return 0;
    }

    ReplyDelete
  8. /*RIPON
    ID:734
    BEATCH:47TH*/

    #include
    using namespace std;

    void prime_range(int a);
    int main(){
    int n;
    cout<<"Enter the value: ";
    cin>>n;
    prime_range(n);
    return 0;
    }
    void prime_range(int a){

    int i,r,j,sum=0;
    cout<<"prime number list: "<<" 1 2 3 ";

    for(i=2; i<=a; i++){
    int flag=1;

    for(j=2;j<=i/2;j++){
    r=i%j;
    if(r==0){
    flag=0;

    break;
    }
    }
    if(flag==1)
    {
    cout<<i<<" ";
    sum+=i;
    }

    }
    cout<<"summation is: "<<sum<<endl;
    if(sum==a)
    cout<<"equal"<<endl;
    else
    cout<<"not equal"<<endl;
    }

    ReplyDelete
  9. #include
    using namespace std;
    int prime(int n)
    {

    int i,j,c,sum=0;
    for(i=2;i>num;
    cout<<"List of prime number:"<<endl;
    prime(num);
    }

    ReplyDelete
  10. /*Fatema Binte Shakir
    id-201410038*/
    #include
    using namespace std;
    int primenumber_range(int n);
    int main()
    {
    int num;
    cout<<"Enter the range: ";
    cin>>num;
    primenumber_range(num);
    return 0;
    }
    int primenumber_range(int n)
    {
    int i,t,num,sum=0;
    cout<<endl<<"prime list: "<<endl<<endl;
    for(num=1;num<=n;num++)
    {
    t=1;
    for(i=2;i<=num/2;i++)
    {
    if(num%i==0)
    {
    t=0;
    break;
    }
    }
    if(t==1)
    {
    cout<<num<<endl;
    sum=sum+num;
    }
    }
    cout<<endl<<"the sum of prime number is: "<<sum<<endl<<endl;
    if(sum==n)
    cout<<endl<<"the sum of prime number is equal to: "<<n<<endl;
    else
    cout<<endl<<"the sum of prime number is not equal to: "<<n<<endl;
    }

    ReplyDelete
  11. #include
    using namespace std;
    void primenumber_range(int n);
    int main()
    {
    int num;
    cout<<"Enter the range: ";
    cin>>num;
    primenumber_range(num);
    return 0;
    }
    void primenumber_range(int n)
    {
    int i,t,num,sum=0;
    cout<<endl<<"prime list: "<<endl<<endl;
    for(num=1;num<=n;num++)
    {
    t=1;
    for(i=2;i<=num/2;i++)
    {
    if(num%i==0)
    {
    t=0;
    break;
    }
    }
    if(t==1)
    {
    cout<<num<<endl;
    sum=sum+num;
    }
    }
    cout<<endl<<"the sum of prime number is: "<<sum<<endl<<endl;
    if(sum==n)
    cout<<endl<<"the sum of prime number is equal to: "<<n<<endl;
    else
    cout<<endl<<"the sum of prime number is not equal to: "<<n<<endl;
    }

    ReplyDelete
  12. #include
    using namespace std;
    void prime_range(int range)
    {
    int i,num,count,p,sum=0;
    for(num = 1;num<=p;num++)
    {
    count=0;

    for(i=2;i<=num/2;i++)
    {
    if(num%i==0)
    {
    count++;
    break;
    }
    }
    if(count==0 && num!=0)
    {
    cout<>range;
    prime_range(range);
    return 0;
    }

    ReplyDelete
  13. #include
    using namespace std;
    void prime_range(int range)
    {
    int i,num,count,p,sum=0;
    for(num = 1;num<=p;num++)
    {
    count=0;

    for(i=2;i<=num/2;i++)
    {
    if(num%i==0)
    {
    count++;
    break;
    }
    }
    if(count==0 && num!=0)
    {
    cout<>range;
    prime_range(range);
    return 0;
    }

    ReplyDelete
  14. #include
    using namespace std;
    void prime_print(int a, int b);
    main()
    {
    int first,last;
    cin>>first;
    cin>>last;
    prime_print(first, last);
    }

    void prime_print(int a, int b)
    {
    int num,i,temp,sum=0;

    for(num=a;num<=b;num++)
    {
    temp=1;
    for (i=2;i<num;i++)
    {
    if(num%i==0)
    {
    temp=0;
    break;

    }
    }
    if(temp==1)
    {
    cout<<num<<endl;
    sum=sum+num ;
    }
    }
    cout<<endl;
    cout<<"sum="<<sum<<endl;
    if(sum==b)
    cout<<"the sum is equal to "<<b<<endl;
    else
    cout<<"the sum is not equal to "<<b<<endl;
    }

    ReplyDelete