adsense

Hii, welcome to my site. My name is Om prakash kartik. This blog helps you to learn programming languages concepts.

Program in C and C++ to print prime factors of given number Using recursive function

Program in C and C++ to print prime factors of given number Using recursive function

C Program
#include<stdio.h>
 void primeFacter(int n){
    static int i = 2;
    if( n == 1)
        return;
    if(n % i == 0){
        printf(" x %d",i);
        primeFacter(n / i);
    }
    else{
        i++;
        primeFacter(n);
    }
 }
 int main(){
    int n;
    printf("Enter a number :- ");
    scanf("%d",&n);
    printf("Prime factor : 1");
    primeFacter(n);
    return 0;
 }
C++ Program
#include<iostream>
 void primeFacter(int n){
    static int i = 2;
    if( n == 1)
        return;
    if(n % i == 0){
        std::cout<<" x "<<i;
        primeFacter(n / i);
    }
    else{
        i++;
        primeFacter(n);
    }
 }
 int main(){
    int n;
    std::cout<<"Enter a number :- ";
    std::cin>>n;
    std::cout<<"Prime factor : 1";
    primeFacter(n);
    return 0;
 }
Output :-









Related Programs


Post a Comment

0 Comments