Answers for "prime factorization in c"

C
0

Write a c program to show the prime factor of a given number.

/**
 * C program to find all prime factors of a given number
 */

#include <stdio.h>

int main()
{
    int i, j, num, isPrime;

    /* Input a number from user */
    printf("Enter any number to print Prime factors: ");
    scanf("%d", &num);

    printf("All Prime Factors of %d are: \n", num);

    /* Find all Prime factors */
    for(i=2; i<=num; i++)
    {
        /* Check 'i' for factor of num */
        if(num%i==0)
        {
            /* Check 'i' for Prime */
            isPrime = 1;
            for(j=2; j<=i/2; j++)
            {
                if(i%j==0)
                {
                    isPrime = 0;
                    break;
                }
            }

            /* If 'i' is Prime number and factor of num */
            if(isPrime==1)
            {
                printf("%d, ", i);
            }
        }
    }

    return 0;
}
Posted by: Guest on September-01-2021
0

prime factorization in c

#include <stdio.h>
#define MIN 100
#define MAX 100000


int main(){
    int pdiv=2,j;
    for (int num=MIN;num<=MAX;num++){
        printf("The prime factors of %d are:\n",num);
        j=num;
        do {
            
            if (j%pdiv==0)
            {
                
                printf("%d\n",pdiv);
                j=j/pdiv;
            }else
            {
               pdiv++; 
            }
            
            
        }while(j>1);
        pdiv=2;
        
    }



}
Posted by: Guest on November-13-2020
1

prime number c

#include <stdio.h>
int main()
{
   int i, num, p = 0;
   printf("Please enter a number: \n");
   scanf("%d", &num);
   for(i=1; i<=num; i++)
   {
      if(num%i==0)
      {
         p++;
      }
   }
   if(p==2)
   {
      printf("Entered number is %d "\
             "and it is a prime number.",num);
   }
   else
   {
      printf("Entered number is %d "\
             "and it is not a prime number.",num);
   }
}
Posted by: Guest on June-04-2020
0

prime factorization of factorials using C

#include <stdio.h>
#include <math.h>
int main()
{
     int N;
     // storing prime number between 2-99
     int p_arr[25] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};
     int prime_index;
     int i;
     int power, count, store;
     printf("\n Prime Factorization of n Factorial. The first number of parameter is prime number and 2nd number is its value.\n");
     printf("\n\t\tType 0 to exit\n");
     while (1)
     {
          printf("\nNumber : ");

          scanf("%d", &N);
          if (N == 0)
          {
               printf("\nThanks for using our component\n");
               return 0;
          }
          if (N<2 | N> 99)
          {
               printf("\nType a number between 2-99\n");
               continue;
          }
          for (i = 0; N >= p_arr[i]; i++)
          {
               prime_index = i;
          }

          printf("Factorial : ");
          for (i = 0; i <= prime_index; i++)
          {
               count = 0;
               power = 1;

               for (store = N / pow(p_arr[i], power); store != 0; power++, store = N / pow(p_arr[i], power))
               {
                    count = count + store;
               }

               if (count > 0)
               {
                    if (i == prime_index)
                    {
                         printf("(%d,%d)", p_arr[i], count);
                    }
                    else
                    {
                         printf("(%d,%d) * ", p_arr[i], count);
                    }
               }
          }
          printf("\n");
     }

     return 0;
}
Posted by: Guest on July-04-2021

Code answers related to "prime factorization in c"

Code answers related to "C"

Browse Popular Code Answers by Language