i am working on a program which shows the CGD in C since im a beginner in C i wanted to write my own way and not really follow the ways of algorithms to do this, sort of for fun. here is the code:
#include <stdio.h>
int main()
{
int n1,n2;
int ndiv=0;
int ndiv2=0;
printf("Enter a number: ");
scanf("%d %d", &n1, &n2);
if(n1 % 2 == 0){
for (int i=1; i<=n1; i++){
ndiv=n1;
n1/=2;
printf("%d ", ndiv);
}
}else if (n1 % 3 == 0){
for (int i=1; i<=n1; i++){
ndiv=n1;
n1/=3;
printf("%d ", ndiv);
}
}else if (n1 % 5 == 0){
for (int i=1; i<=n1; i++){
ndiv=n1;
n1/=5;
printf("%d ", ndiv);
}
}
printf("\n");
if(n2 % 2 == 0){
for (int i=1; i<=n2; i++){
ndiv2=n2;
n2/=2;
printf("%d ", ndiv2);
}
}else if (n2 % 3 == 0){
for (int i=1; i<=n2; i++){
ndiv2=n2;
n2/=3;
printf("%d ", ndiv);
}
}else if (n2 % 5 == 0){
for (int i=1; i<=n2; i++){
ndiv2=n2;
n2/=5;
printf("%d ", ndiv2);
}
}
return 0;
}
So this code currently is used to find te prime factor of the number, im using this method to find the GCD, " https://www.dummies.com/education/math/using-prime-factorizations/ ", the problem i have is that with numbers such as 42 it outputs "42 21 10 5" which isnt correct since 21 cant divide by 2 but it looks like it gets divided anyway for some reason when its supposed to go to divide by 3 instead...
Also ontop of this i know my code isnt the best what can i do too it to make it run faster and better altogether and in general?
Aucun commentaire:
Enregistrer un commentaire