C Program To Find Prime Number And If Number Is Not Prime Then Find Factors

Write a C program to find prime number and if number is not prime then find factors of the number in C Programming Language ?


Solution:

  1. #include <stdio.h>
  2. #include <conio.h>
  3. void main()
  4. //C Program to Check whether the Given Number is a Prime
  5. //C program to display all the factors of a number entered by user.
  6. {
  7.  int flag=0,i,n,r,a[100],b=0,s;
  8.  clrscr();
  9.  printf("\n Enter Number: ");
  10.  scanf("%d",&n);
  11.  for(i=2;i<=n/2;i++)
  12.  {
  13.   r=n%i;
  14.   if(r==0)
  15.   {
  16.    flag=s;
  17.    a[b]=i;
  18.    b++;
  19.   }
  20.  }
  21.  if(flag==s)
  22.  {
  23.   printf(" So The Number %d is not prime",n);
  24.   printf("\n The factors are: ");
  25.   for(i=0;i<b;i++)
  26.   {
  27.    printf("%d ",a[i]);
  28.   }
  29.  }
  30.  else
  31.  {
  32.   printf("\n So The Number %d is prime",n);
  33.  }
  34.  getch();
  35. }


Learn More :