Results 1 to 5 of 5

Thread: program to print prime factor of given number

  1. #1
    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?

  2. #2
    Join Date
    Oct 2005
    Posts
    2,393

    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:


    #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. #3
    Join Date
    May 2008
    Posts
    2,389

    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:
    #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();
    }

  4. #4
    Join Date
    Jan 2008
    Posts
    1,521

    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:

    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. #5
    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:


    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);

    }

Similar Threads

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

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,637,997.33724 seconds with 17 queries