I have written a simple CLI app that finds factors in 3 different forms. I would like the code to be reviewed.
Here's the code:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
void eq(int num)
{
int factor_found = 0, factor = 2;
printf("%d = " , num);
while(factor_found==0)
{
if(num%factor==0 && num!=0)
{
printf("%d*" , factor);
num = num/factor;
}
else if(num==-1 || num==0 || num==1)
{
printf("%d." , num);
factor_found=1;
}
else
{
++factor;
}
}
}
void n(int num)
{
int factor_found = 0, factor = 2;
printf("Factors of %d are : ", num);
if(num==1 || num==-1)
{
printf("%d,%d.", num, num*-1);
}
else if(num==0)
{
printf("Math Error.");
}
else
{
printf("1, -1, ");
while(factor_found==0)
{
if(num%factor==0 && num!=factor && num!=factor*(-1))
{
printf("%d, %d, ", factor, (factor*(-1)));
++factor;
}
else if(num==factor || num==factor*(-1))
{
printf("%d, %d.", num, (num*(-1)));
++factor_found;
}
else
{
++factor;
}
}
}
}
void poc(int num)
{
int factor_found = 0, factor = 2, no_of_factors = 0, temp;
if(num==1 || num==0 || num==-1)
{
printf("%d is nor prime nor composite.", num);
}
else
{
temp = num;
num = abs(num);
while(factor_found==0)
{
if(num%factor==0 && num!=factor)
{
++no_of_factors;
num=num/factor;
}
else if(num%factor==0 && (num==factor || factor==1))
{
num=num/factor;
}
else if(num==1 || num==-1)
{
++factor_found;
}
else
{
++factor;
}
}
if(no_of_factors==0)
{
printf("%d is a prime number.", temp);
}
else if(no_of_factors!=0)
{
printf("%d is a composite number.", temp);
}
}
}
void check(char mode[5], int num)
{
if(strcmp(mode, "-eq")==0 || strcmp(mode, "eq")==0)
eq(num);
else if(strcmp(mode, "-n")==0 || strcmp(mode, "n")==0)
n(num);
else if(strcmp(mode, "-poc")==0 || strcmp(mode, "poc")==0)
poc(num);
else if(strcmp(mode, "-all")==0 || strcmp(mode, "all")==0)
{
eq(num); printf("\n");
n(num); printf("\n");
poc(num);
}
else
printf("Invalid Argument!");
}
void main(int argc, char *argv[])
{
int num = atoi(argv[1]); char mode[5];
if(argc==1)
{
printf("Enter the number : ");
scanf("%d" , &num);
printf("Enter the mode : ");
scanf("%s" , mode);
check(mode, num);
}
else if(argc==2)
{
printf("Enter the mode : ");
scanf("%s" , mode);
check(mode, num);
}
else
check(argv[2], num);
}