-->
Showing posts with label program to know a number is prime or composite or not using function.program to know a number is prime or composite or not using function with parameter and return.. Show all posts
Showing posts with label program to know a number is prime or composite or not using function.program to know a number is prime or composite or not using function with parameter and return.. Show all posts

program to know a number is prime or composite or not using function.


using codeblock
-----------------------------------------------------------------------------------
//program to know a number is prime or composite or not using function..
#include<stdio.h>                      //header file
void prime_number();                          // function prototype/declaration with return type zero.
int main()                              //main function
{
prime_number();                                     // function calling.
return 0;                                       //returns 0 value
}
void prime_number()                           // function body line ; also called function definition
{
 int i,number,count=0;                    // variable declaration
printf("enter a number\n");               //message display to input a number
scanf("%d",&number);                      //gets input
for(i=2;i<number;i++)                     //executing from 2 to <n
{
    if(number%i==0)                       //testing for next number
    {
        count=1;                        //assigning to  1 to count
   
    }
   
}   
 if(count==0)                                //testing count ;whetehre it has 0 or 1
 {
    printf("%d is prime, ",number);
 } 
 else
 {
     printf("%d is not prime number,i.e. it's a composite",number);
 }
}

---------------------------------------------------------------------------------------------------------
using turbo
--------------------------------------------------------------------------------------------------------
//program to know a number is prime or composite or not using function..
#include<stdio.h>                      //header file
#include<conio.h>
void prime_number();                // function prototype/declaration with return type zero.
int main()                              //main function
{
prime_number();                                     // function calling.
getch();                                       //holds the output
return 0;                                       //returns 0 value
}
void prime_number()                           // function body line ; also called function definition
{
 int i,number,count=0;                    // variable declaration
printf("enter a number\n");               //message display to input a number
scanf("%d",&number);                      //gets input
for(i=2;i<number;i++)                     //executing from 2 to <n
{
    if(number%i==0)                       //testing for next number
    {
        count=1;                        //assigning to  1 to count
   
    }
   
}   
 if(count==0)                                //testing count ;whether it has 0 or 1
 {
    printf("%d is prime, ",number);
 } 
 else
 {
     printf("%d is not prime number,i.e. it's a composite",number);
 }
}