7. C PROGRAMMING LAB | Check Now

C PROGRAMMING LAB –07] Implement using functions to check whether the given number is prime and display appropriate messages


Algorithm

  • Step-1: Start
  • Step-2: [Read the value of N ( positive integer )]-Read N
  • Step-3: [Check whether the N is prime or not]-
    • for i = 2 to m/2
    • if m % i = 0 than goto step 4 otherwise goto step 5
  • Step-4: Print the message of the number is not prime
  • Step-5: Print the message of the number is prime number
  • Step-6: stop

Flow Chart

C PROGRAMMING -Prime Number Program

Program -7 source code

#include<stdio.h>
int isprime(int n);

int main()
{
 int i,m1,m2;
 printf("enter range\n");
 scanf("%d%d",&m1,&m2);
 printf("prime num from %d to %d are:\n",m1,m2);
 for(i=m1;i<=m2;i++)
 {
  if(isprime(i))
   printf("%d\n",i);
 }
}

int isprime(int m)
{
 int i;
  for(i=2;i<=m/2;i++)
  {
   if(m%i==0)
   {
    return 0;
   }
  }
 return 1;
}

or

#include<stdio.h>
int isprime(int);
int main()
{
int num;
printf("Enter value of num:\n");
scanf("%d",&num);
if (isprime(num))
printf("%d is a prime number",num);
else
Printf(“%d is not a prime number”,num)
}
int isprime(int x)
{
int i;
for(i=2;i<=x/2;i++)
if(x%i == 0)
return 0;
return 1;
}

C PROGRAMMING -Output

  • Enter range
    • 1
    • 80
  • prime num from 1 to 80 are :
    • 1
    • 2
    • 3
    • 5
    • 7
    • 11
    • 13
    • 17
    • 19
    • 23
    • 29
    • 31
    • 37
    • 41
    • 43
    • 47
    • 53
    • 59
    • 61
    • 67
    • 71
    • 73
    • 79

OR

Enter the value of num: 3

3 is a prime number

C PROGRAMMING -Viva Questions

1] What is a prime number?

2] Give examples for prime numbers?

3] What are keywords?

4] What are conditional statements?

Leave a Reply

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

WhatsApp Icon Join For Job Alerts