Page 71 - Modular_V2.0_C++_Flikpbook
P. 71

for (i = 1; i <= n; i++)
                     {

                           p = p * x / (float)i;
                           sum = sum + p;
                     }
                     cout<< "The sum is: " << sum << endl;
                     getch();

                 }
                 Program 11: To check whether the number is prime or not.

                 #include<iostream.h>
                 #include<conio.h>
                 void main()
                 {
                     clrscr();
                     int n, i = 2, flag = 0;

                     cout<< "Enter a number: ";
                     cin>> n;
                     if(n <= 1)
                     {

                         cout<< "\nThe number is not prime.";
                     }
                     else
                     {
                         while(i < n)
                         {

                             if(n % i == 0)
                                                                                DOSBox 0.74, Cpu speed: max 100% cycles,
                             {
                                                                            Enter a number: 7
                                 flag = 1;
                                                                            The number is prime.
                                 break;

                             }                                                  DOSBox 0.74, Cpu speed: max 100% cycles,
                                 i++;                                       Enter a number: 4
                                                                            The number is not prime.
                         }
                         if(flag == 1)
                             cout<< "\nThe number is not prime.";
                         else

                             cout<< "\nThe number is prime.";
                     }
                     getch();
                 }

                                                                                                                  69
                                                                                                         Loops
   66   67   68   69   70   71   72   73   74   75   76