TechArena Community program to print prime factor of given number

# program to print prime factor of given number

## Software Development

#1
27-11-2009
 Member Join Date: Nov 2009 Posts: 1,302
program to print prime factor of given number

Hi All,

I am the beginner in the field of programming. I am getting little-bit difficulties while writing program logic. One of the difficulty is in program of prime factor. I have to write a program to print the prime factor of the given number. The given number should be provided by the user while running the program.

Can anyone help me to write the program to print prime factor of given number?

#2
27-11-2009
 Member Join Date: Oct 2005 Posts: 2,382
Re: program to print prime factor of given number

Hi friend,

see something below can help you to write program to find-out prime factors of the given number:

Quote:
 #include< conio.h > #include< stdio.h > void main() { int a,b,c,d,e; clrscr(); printf("Enter Number:"); scanf("%d",&e); a=1; while(a<=e) { if(e%a==0) { b=1; c=0; d=1; while(d<=b) { if(b%d==0) c++; d++; } if(c==2) { printf("\n%d is the Prime factor of %d",d-1,e); } } a++; } getch(); }
#3
27-11-2009
 Member Join Date: May 2008 Posts: 2,371
Re: program to print prime factor of given number

Hi,

Don't worry friend, I have C programming code which will display the prime factors of given number. Please copy this program code and run on your computer and lets see you will definitely get the prime factor of given number.

C Program:
Quote:
 #include #include void main() { int c,m; printf("Enter a Number to prime factors:"); scanf("%d",&c); printf("\n\nPrime Factors of %d is: ",c); for(m=2;m<=c;m++) { if(n%m==0) { printf("%d,",m); c=c/m; m--; if(c==1) break; } } getche(); }
__________________
The FIFA Manager 2009 PC Game
#4
27-11-2009
 Member Join Date: Jan 2008 Posts: 1,507
Re: program to print prime factor of given number

Why don't you try below code to print prime factor of given number? I got below program from one of my friend, and it's executes without any syntax errors with the correct output.

C code:
Quote:
 void main() { int l,m,n; clrscr(); printf("Enter the number:"); scanf("%d",&n); for(m=2;m<=n;m++) { if(n%m==0) { for(l=2;l<=m-1;l++) { if(m%l==0) break; } if(l==m) printf("%d ",m); } } getch(); }
#5
27-11-2009
 Member Join Date: Apr 2008 Posts: 1,943
Re: program to print prime factor of given number

Hi,

I am giving you just hint to write prime factor of given number not all program. You should use the special function to calculate the prime factor.

In following example used the concept of the nested loops to achieve the my target. I have used "IF" loop inside "FOR" loop to check the condition correctly:

Quote:
 void prime_demo(int h) { int p,g=0; for(p=2;p<=h/2;p++) { if(h%p==0) { g=1; break; } } if(g==0) printf(" %d",h); }