AndreiL
Welcome to AndreiL.
Enjoy your time.
Please log in.

Prime factors of a given number - C algorithm

Go down

Prime factors of a given number - C algorithm

Post by Lucaci Andrei on Thu Mar 15, 2012 1:07 am

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include<stdio.h>


int check(int a){
int i=2;
for (i; i<=a/2;i++){
if (a%i==0){
return 0;
}
return 1;
}
}


int main(){
printf("Enter a number: ");
int num;
scanf("%d", &num);
if (check(num)==1){
printf("This is a prime number.\n Prime factors: 1*");
printf("%d", num);
printf("\n");}
else{
int i;
printf("The prime factors of ");
printf("%d",num);
printf(" are: \n");
for (i=2; i<=num; i++){
while(num%i==0){
num/=i;
printf("%d", i);
printf("*");
}
}
printf ("1\n");
return 0;
}
}

avatar
Lucaci Andrei
"Tata Lor"


Number of messages : 222
Points : 2266743
Reputation : 1007
Registration date : 2008-08-15
Age : 26
Location : Cluj-Napoca

View user profile http://www.andreil.wgz.ro

Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum