Tuesday 25 September 2018

Printing n prime numbers using a user-defined function in C++

Prime numbers

Prime numbers are those numbers which are divisible by and itself only. A prime number doesn't have any pattern but can be determined by the division process.
      E.g. 2,3,5,7,...etc. (i.e. 7 is divisible by 1 and itself only, so it is a prime number.)
Zero and one (0,1) are neither prime nor composite numbers


Write a program in C++ to input a number from the user and print all the prime numbers up to the number using a user-defined function.

We have to input a number till which the user wants to print the prime numbers. In this program user- defined function is used to print the numbers. A user-defined function is a function created by the user to perform a specific task. It contains three essential statements which are as follows:
  1. Function declaration or prototype 
  2. Function call statement
  3. Function definition
In the program, all the number will be passed to the function definition. The function will return 1 for prime numbers and 0 for composite. If 1 is returned to the function call then the number will be printed on the screen else it is not printed. 


Program



#include<iostream.h>
#include<conio.h>
int prime(int);                                                           //Function declaration or prototype
void main()
{
int i,n,r;
clrscr();
cout<<"Enter the number upto which you want to print the prime numbers=";
cin>>n;
cout<<"\n";
for (i=0;i<=n;i++)                                         
{                                                                                     
r=prime (i);                                                          //Function call statement
if (r==1)
cout<<i<<endl;
}
getch();
}
int prime(int a)                                                         //Function definition
    {
    int c;
    for(c=2;c<=a-1;c++)
         {
         if (a%c==0)
         return 0;
         }
if (a==c)
return 1;
}


Output

Program to print n prime numbers
PRINTING N PRIME NUMBERS 









Explanation

We have entered the value 19 in the program. Now all the values from 1 to 19 are passed to the function definition. The operations in the loop will be performed as follows:

a=i        c         c<=a-1                        a%c==0                a==c                        Return value
0           2         2<=-1 is false                                                                            NULL
1           2         2<=0 is false                                                                             NULL
2           2         2<=1 is false                                             2=2 is true               1
3           2         2<=2 is true              3%2=0 is false
             3         3<=2 is false                                             3=3 is true               1
4           2         2<=3 is true              4%2=0 is true                                          0
5           2         2<=4 is true              5%2=0 is false       
             3         3<=4 is true              5%3=0 is false
             4         4<=4 is true              5%4=0 is false
             5         5<=4 is false                                             5=5 is true               1


The process will continue till a=19. Those numbers with return value 1 will be printed and we obtain our required output.



No comments:

Post a Comment