Factorization
#include <stdio.h>
int factor(int n){
int p, flag;
flag = 0;
p=2;
while (p*p <= n) {
if (n % p == 0) {
flag = 1;
return(p);
break;
}
else
p=p+1;
}
if(flag == 0)
return(n);
}
int main() {
int n, fact;
printf("Input a number > ");
scanf("%d",&n);
printf("Prime factors of %d are: ",n);
do {
fact = factor(n);
printf("%d ",fact);
n = n/fact;
} while(n != 1);
printf("\n");
}
Finding zeros of functions
Bisection Method
/* bisection.c */
#include <stdio.h>
#define epsilon 0.0000001
double f (double x) {
return (x*x - 3) ;
}
int main() {
double L,M,R;
printf("Enter the left (L) and right (R) end of the interval\n");
printf("where the zero lies: ");
scanf("%lf %lf", &L, &R);
printf(" [%lf,%lf]\n",L,R);
if (f(L)*f(R) > 0)
printf(" No sign change ... exit!\n");
else {
do {
M = (L+R)/2.0;
if (f(L)*f(M) > 0)
L = M;
else
R = M;
printf(" [%lf,%lf]\n",L,R);
} while(R-L > epsilon);
printf("A zero is equal %lf\n",L);
}
}