A positive integer is entered through the keyboard , write a program to obtain the prime factors of the number . Modify the function suitably to obtain the prime factors recursively .
Q. A positive integer is entered through the keyboard , write a program to obtain the prime factors of the number . Modify the function suitably to obtain the prime factors recursively .
Summary:-
Here asking for prime factors. Prime numbers are those numbers who can be divided by only 1 and itself . Like:- 2,3,5,7...... .
Algorithm: -
1.first take any positive input from the user.Let n .
2.make a code that could find prime number starting from 1 to the n.
3.if we get prime number from the above step , then check if n%pn==0 .pn = prime number.
4.If n%pn==0 false ,then again go to step 2 to find next prime number . if n%pn==0 is true , then print that number and make n=n/pn . Run this in loop till n%pn==0 is false. If this happens go to again at step 2 to find next prime number.
Here asking for prime factors. Prime numbers are those numbers who can be divided by only 1 and itself . Like:- 2,3,5,7...... .
Algorithm: -
1.first take any positive input from the user.Let n .
2.make a code that could find prime number starting from 1 to the n.
3.if we get prime number from the above step , then check if n%pn==0 .pn = prime number.
4.If n%pn==0 false ,then again go to step 2 to find next prime number . if n%pn==0 is true , then print that number and make n=n/pn . Run this in loop till n%pn==0 is false. If this happens go to again at step 2 to find next prime number.
1. Without Recursion
#include <stdio.h>
void prime_fact(int );
int main()
{
int n;
printf("\nEnter a number: ");
scanf("%d",&n);
prime_fact(n);
return 0;
}
void prime_fact(int n)
{
int count;
int a ;
int b =1;
while(b<=n)
{
a=1;
count = 0;
while(a<=b)
{
if(b%a==0)
{
count = count + 1;
}
a++;
}
if(count == 2)
{
if(n%b==0)
{
printf(" %d",b);
n=n/b;
if(n%b!=0)
{
b++;
}
}
else
{
b++;
}
}
else
{
b++;
}
}
}
void prime_fact(int );
int main()
{
int n;
printf("\nEnter a number: ");
scanf("%d",&n);
prime_fact(n);
return 0;
}
void prime_fact(int n)
{
int count;
int a ;
int b =1;
while(b<=n)
{
a=1;
count = 0;
while(a<=b)
{
if(b%a==0)
{
count = count + 1;
}
a++;
}
if(count == 2)
{
if(n%b==0)
{
printf(" %d",b);
n=n/b;
if(n%b!=0)
{
b++;
}
}
else
{
b++;
}
}
else
{
b++;
}
}
}
2. Recursion
#include <stdio.h>
#include <stlib.h>
void prime_fact(int );
int b=1;
int main()
{
int n;
printf("\nEnter a number: ");
scanf("%d",&n);
prime_fact(n);
return 0;
}
void prime_fact(int n)
{
int a;
int count;
if(b<=n)
{
a=1;
count = 0;
while(a<=b)
{
if(b%a==0)
{
count = count +1;
}
a++;
}
if(count == 2)
{
while(n%b == 0)
{
if(n%b == 0)
{
printf(" %d",b);
n=n/b;
}
}
++b;
prime_fact(n);/*<-recursion happens here*/
}
else
{
++b;
prime_fact(n);/*<-recursion happens here*/
}
}
else
{
exit(0);
}
}
#include <stlib.h>
void prime_fact(int );
int b=1;
int main()
{
int n;
printf("\nEnter a number: ");
scanf("%d",&n);
prime_fact(n);
return 0;
}
void prime_fact(int n)
{
int a;
int count;
if(b<=n)
{
a=1;
count = 0;
while(a<=b)
{
if(b%a==0)
{
count = count +1;
}
a++;
}
if(count == 2)
{
while(n%b == 0)
{
if(n%b == 0)
{
printf(" %d",b);
n=n/b;
}
}
++b;
prime_fact(n);/*<-recursion happens here*/
}
else
{
++b;
prime_fact(n);/*<-recursion happens here*/
}
}
else
{
exit(0);
}
}
Comments
Post a Comment