Write a C++ program to compute the sum of the specified number of Prime numbers

Write a C++ program to compute the sum of the specified number of Prime numbers

Assignment
11

              Write a C++ program to compute the sum of the specified
number of Prime numbers.
(Accept the number n from user and calculate the sum of prime
numbers between 1 to n)

Program
Code with explanation:

1.   
 #include <iostream>

2.   
 using namespace std;

3.   
 int main()

4.   
 {

5.   
      int
n, i, j, sum = 0, flag = 0;

6.   
      cout
<< ” * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
\n”;

7.   
      cout
<< ” \t Accept number n Accept the number n from user \n “;

8.   
      cout
<< ” \t and calculate the sum of prime numbers between 1 to n \n
“;

9.   
      cout
<< ” * * * * * * * * * * * * * * * * * * * * * * *  * * * * * * \n \t”;

10.       cin
>> n;

11.       //
Number 1 is not prime number,

12.       //
so we start find prime numbers from 2

13.       for
( i = 2 ; i <= n ; i ++ )

14.       {

15.                   for
( j = 2 ; j <= i/2 ; j ++ )

16.                   {

17.                               if(
i % j == 0)

18.                               {

19.                                           flag
= 100 ;

20.                                           break
;

21.                               }

22.                   }

23.                   if
( flag == 0)

24.                               sum
= sum + i;

25.                   else

26.                               flag
= 0;

27.       }

28.       cout
<< ” * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
\n”;

29.       cout
<< ” \t Sum of prime numbers between 1 to ” << n <<
” is ”

30.       cout
<< sum << endl;

31.       cout
<< ” * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
\n”;

32.       return
0;

33.  }

Explanation
of Code:

Line No. 1 and 2: include the Library file
and stander input – output functions.

Line No. 3: main function header.

Line No. 4 and 18: Begin and end of main
function respectively.

Line
No. 5:
Declare
the integer variables.

Line
No. 7 and 10:
Accept
number n from user to find Factorial.

Line
No. 11 and 12:
Value
1 is not prime so we start to find prime numbers from value 2.

Line
No. 13 and 27:

We declare the
variable flag with initialization value 0. flag variable use to indicate the
number is prime or not.

            First
for loop is used to move from 2 to number n.

            Second
for loop are used to check prime number for each iteration i.

                        In
first for loop after completing each iteration of second for loop, we calculate
sum using the value of flag.

For example:


 

Line No. 29 and 30: Display sum of prime
numbers between 1 to n.

Line
No. 32:

return statement appropriate with main function return data type.

Program
Code for run:

#include
<iostream>

using
namespace std;

int
main()

{

            int
n, i, j, sum = 0, flag = 0;

            cout
<< ” * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
\n”;

            cout
<< ” \t Accept number n Accept the number n from user \n “;

            cout
<< ” \t and calculate the sum of prime numbers between 1 to n \n
“;

            cout
<< ” * * * * * * * * * * * * * * * * * * * * * * *  * * * * * * \n \t”;

            cin
>> n;

            //
Number 1 is not prime number,

            //
so we start find prime numbers from 2

            for
( i = 2 ; i <= n ; i ++ )

            {

                        for
( j = 2 ; j <= i/2 ; j ++ )

                        {

                                    if(
i % j == 0)

                                    {

                                                flag = 100 ;

                                                break ;

                                    }

                        }

                        if
( flag == 0)

                                    sum
= sum + i;

                        else

                                    flag
= 0;

            }

            cout
<< ” * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
\n”;

            cout
<< ” \t Sum of prime numbers between 1 to ” << n <<
” is ”

cout << sum << endl;

            cout
<< ” * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
\n”;

            return
0;

}

Output
of Program:

            * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * *

                        Accept
number n Accept the number n from user

 
                      and
calculate the sum of prime numbers between 1 to n

 
          * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 

                        100

            *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *

                        Sum
of prime numbers between 1 to 100 is 1060

            *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *

Source link

Leave a Comment

Your email address will not be published. Required fields are marked *


Scroll to Top