Answers for "prime factorization program 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 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("nttType 0 to exitn");
     while (1)
     {
          printf("nNumber : ");

          scanf("%d", &N);
          if (N == 0)
          {
               printf("nThanks for using our componentn");
               return 0;
          }
          if (N<2 | N> 99)
          {
               printf("nType a number between 2-99n");
               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 program in c"

Code answers related to "C"

Browse Popular Code Answers by Language