Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 27-11-2009
Member
 
Join Date: Nov 2009
Posts: 1,292
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?
Reply With Quote
  #2  
Old 27-11-2009
Member
 
Join Date: Oct 2005
Posts: 2,389
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();
}

Reply With Quote
  #3  
Old 27-11-2009
Member
 
Join Date: May 2008
Posts: 2,383
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<conio.h>
#include<stdio.h>

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
Reply With Quote
  #4  
Old 27-11-2009
Member
 
Join Date: Jan 2008
Posts: 1,515
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();
}
Reply With Quote
  #5  
Old 27-11-2009
Member
 
Join Date: Apr 2008
Posts: 1,948
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);

}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "program to print prime factor of given number"
Thread Thread Starter Forum Replies Last Post
Need C program to print triangle of character or number Khan Baba Software Development 5 17-09-2011 04:40 PM
What is the program to print table of given number? Roxy_jacob Software Development 5 04-09-2011 10:40 PM
Problem with C# program which calculates the prime number $Bird$ Software Development 4 29-11-2010 12:54 PM
Shell program for the prime number Rup_me Software Development 5 14-12-2009 10:24 AM
program to check prime number in java Bansi_WADIA Software Development 3 24-11-2009 11:02 PM


All times are GMT +5.5. The time now is 01:52 PM.