How to write a C program to find prime factors of an integer number

Prime factors

The factors of a number which are prime are called prime factors
example prime factors of integer 6 are 2 & 3 similarly prime factors of integer 8 are: 2

1>write a program in c to generate prime factors of an integer number using for loop

write a program to generate prime factors of an integer number using for loop using c programming

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,c ; // c is flag
clrscr();
printf(“To generate prime factors of an integer number”);
printf(“Enter Number:”);
scanf(“%d”,&n);
printf(“\n\n The Prime Factors of number %d are: “,n);
for(i=2;i<=n;i++) // for finding factors
{
if(n%i==0)
{
for(j=2;j<i;j++) // for finding prime number in factors
{
if i%j==0:
{
c =0;
break;
}
else
c =1;
} //for end

if (c==1)
printf(“\t %d,”,i);

} //end of if
} // end of for

getch();
}

OUTPUT:
To generate prime factors of an integer number
Enter number:12

Prime factors of number 6 are 2 3

RUN2

To generate prime factors of an integer number
Enter a number:15

Prime factors of number 15 are 3 5

2>WAP to find prime factors of an integer number using while loop

WAP to find prime factors of an integer number using while loop in c

#include<stdio.h>
#include<conio.h>
void main()
{
int no,i=2,j=2,c; // c is flag value
clrscr();
printf(“To find prime factors of an integer number \n”);
printf(“Enter a Number:”);
scanf(“%d”,&no);
printf(“\n Prime Factors of integer number %d are: “,n);
while(i<=no) // for finding factors
{
if(no%i==0)
{
while(j<i) // for finding prime number in factors
{
if( i%j==0:)
c =0;
break;
else
c =1;
j++;
}
if (c==1)
printf(“\t %d,”,i);
}
i++;
}
getch();
}

OUTPUT:
To find prime factors of an integer number
Enter a number:16

The Prime factors of integer number 16 are: 2
3>write a C program to find sum of prime factors of an integer number using c programming

write a program to find sum of prime factors of an integer number using c programming

#include<stdio.h>
#include<conio.h>
void main()
{
int num,i=2,j=2,c; // c is flag
clrscr();
printf(“To find sum prime factors of an integer number \n”);
printf(“Enter a Number:”);
scanf(“%d”,&num);
printf(“\n prime factors of integer %d are: “,n);
while(i<=num) // for finding factors
{
if(num%i==0)
{
while(j<i) // for finding prime number in factors
{
if( i%j==0:)
c =0;
break;
else
c=1;
j++;
}
if ( c ==1)
printf(“\t %d,”,i);
sum=sum+i; // sum of prime factors
}
i++;
}
printf(“Sum of prime factors of %d is %d,”,num, sum);
getch();
}

OUTPUT:
To find sum of prime factors of an integer

Enter a number:12

Prime factors of integer 12 are: 2 3

Sum of prime factors of 12 is 5

RUN2:

To find sum of prime factors of an integer

Enter a number:15

Prime factors of integer 15 are: 3 5

Sum of prime factors of 15 is 8

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>