Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

using c programming language (will run in terminal.. gcccompiler) A prime number

ID: 3609722 • Letter: U

Question

using c programming language (will run in terminal.. gcccompiler)

A prime number is an integer that has no factors except itself and1. the first several prime numbers are 2,3,5,7,11,13,17,19,23. verylarge prime numbers have become important in the field ofcryptography. the original public-key crytographic algoritihm isbased on the fact that there is no fast way to find the primefactors of a 200-digit number that is the product of 100-digitprime numbers. in this program, you will implement a simple butvery slow way to test whether a number is prime. one methodof testing a number N for primality, is by calculating N % x, wherex is equal to every prime number from 2 to R=N. if any ofthese results equals to 0, then N is not prime. we can stop testingat N, since if N has any factor greater than R, it also musthave a factor less than or equal to R. unfortunately, keeping tractof the list of prime numbers requires techniques that have not yetbeen presented. however, a less efficient method is to calculate N% x for x=2 and every odd number between 3 and N. some ofthese numbers will be primes, most will not. but if any one valuedivides n evenly, we know that N is not prime. write a functionthat enter an integer N to test and prints the word prime if it isa prime number and the other prime number less than N or nonprimeotherwise. write a main program with a query loop to test manynumbers.

Explanation / Answer

Please rate - thanks #include<iostream.h>
#include<math.h>
void prime(int);
int main()
{int num;
cout<<"enter a number to check (<=0 to exit):";
cin>>num;
while(num>0)
{prime(num);       
cout<<" enter a number to check (<=0 toexit):";
cin>>num;
}
system("pause");
return 0;
}
void prime(int num)
{int i,j,val;
for (i=3;i<=num;i++)
   {j=2;
    do
      {val=i%j;
       if(j==2)
          j++;
       else
          j+=2;
      }while(val!=0&&j<=sqrt(i));
      if(val!=0)
         cout<<i<<": prime ";
    }
return;
}