C Program to Find Prime Factors of a Number

C program to find prime factors of a number; Through this tutorial, we will learn how to find and print prime factors of a number in the c program using for loop, while loop, and recursion.

Programs to Find Prime Factors of a Number in C

  • C Program to Find Prime Factors of a Number Using For Loop
  • C Program to Find Prime Factors of a Number Using While Loop
  • C Program to Find Prime Factors of a Number Using Recursion

C Program to Find Prime Factors of a Number Using For Loop

/* C Program to Find Prime Factors of a Number using For Loop */

#include <stdio.h>

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

  	printf("\n Please Enter any number to Find Factors :  ");
  	scanf("%d", &Number);

  	for (i = 2; i <= Number; i++)
   	{
     	if(Number % i == 0)
        {
   			isPrime = 1;
			for (j = 2; j <= i/2; j++)
			{
				if(i % j == 0)
				{
					isPrime = 0;
					break;
				}
			}
			if(isPrime == 1)
			{
				printf("\n %d is a Prime Factor ", i);
			}
		}
   }
  	return 0;
}

The output of the above c program; as follows:

Please Enter any number to Find Factors :  20
2 is a Prime Factor
 5 is a Prime Factor 

C Program to Find Prime Factors of a Number Using While Loop

/* C Program to Find Prime factors of a Number using While Loop */

#include <stdio.h>

int main()
{
  	int Number, i = 1, j, Count;

  	printf("\n Please Enter number to Find Factors  :  ");
  	scanf("%d", &Number);

 	while (i <= Number)
   	{
   		Count = 0;
    	if(Number % i == 0)
      	{
      		j = 1;
      		while(j <= i)
      		{
      			if(i % j == 0)
      			{
      				Count++;
				}
				j++;
			}
			if(Count == 2)
			{
				printf("\n %d is a Prime Factor ", i);
			}
      	}
    	i++;
   	}

  	return 0;
}

The output of the above c program; as follows:

Please Enter number to Find Factors  :  50
2 is a Prime Factor
 5 is a Prime Factor 

C Program to Find Prime Factors of a Number Using Function

#include <stdio.h>

void Find_Prime(int Number)
{
  	int i, Count = 0;

  	for (i = 2; i <= Number/2; i++)
   	{
    	if(Number%i == 0)
     	{
       		Count++;
     	}
   	}
   	if(Count == 0 && Number != 1 )
   	{
   		printf("\n %d is a Prime Number", Number);
   	}
}
void Find_Factors(int Number)
{
  	int i;

  	for (i = 1; i <= Number; i++)
   	{
    	if(Number % i == 0)
     	{
     		// Calling Find_Prime Function for every factor
       		Find_Prime(i);
     	}
   	}
}
int main()
{
  	int i, j, Number, count;

  	printf("\n Please Enter any number to Find it's Prime Factors :  ");
  	scanf("%d", &Number);

  	printf("\n Prime Factors of a Given Number are : \n");
	Find_Factors(Number);

  	return 0;
}

The output of the above c program; as follows:

Please Enter any number to Find it's Prime Factors :  100
Prime Factors of a Given Number are :

 2 is a Prime Number
 5 is a Prime Number

Recommended C Programs

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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