Hey hi I have a problem with to C++ programs for my final project, the problem is that i tried to make the code but first the C++ program say general protection exception i read some of this but i still think that the structure of my program is not good so i will post to u the original problem and give me opinions please(I just know basic stuffs of C++):

Make a program to calculate maximum common divisor of two positive whole number using the Euclides algorithm.
The two whole positive numbers are identified with the letters M and N
Euclides Algorithm
1.-Divide N by M, R =Residue
2.- If R=0, M is the maximum common divisor and the program end.
3.-Assign to N the values of M and assign the the values of R to M and start again the step 1
code i use:

#include<stdio.h>
#include<conio.h>
#include<math.h>
main(void)
{
  int N, M, R, D, Y, F, Z, S;
  printf("\nPlease enter N\n");
  scanf ("%s", N);
  printf("\nPLease enter  M\n");
  scanf ("%s", M);
  D=N/M;
  Y=M*D;
  R=N-Y;
  if(R=0)goto A;
  printf("%d\n",R);
  if(R>0)goto B;
B:
  F=M/R;
  Z=M*F;
  S=M-Z;
A:
  return(0);
}

The other problem i don't understand the part of no using logarithms the original problem is:

2.- Make a program to calculate AB , B can be positive or negative whole number or zero. Can´t use logarithms.

Thank you in advance for u help and sorry if its difficult to understand my problems but translated from spanish and sorry if i made some mistakes.

First general advice: use code tags (here is an explanation of how) when posting code.
Second general advice: a more descriptive thread title would result in great improvement of the forum readability - so why not something like "problem with gcd algorithm and logarithms" ? Think about it on the next thread you'll open please.

For calculating GCD(a, b) I'd suggest using a recursive function - it's more elegant and easier to write imho. Hint: use % operator. Try to give more meaningful names to your variable also.

What's AB? A * B? The distance between A and B? Please clarify this point.

Btw this is C++ forum...

I made a few mistakes pasting the code the best for me is this

#include<stdio.h>
#include<conio.h>
#include<math.h>
main(void)
{
int N, M, R, D, Y, F, Z, S;
printf("\nPlease enter N\n");
scanf ("%d", &N);
printf("\nPLease enter  M\n");
scanf ("%d", &M);
D=N/M;
Y=M*D;
R=N-Y;
if(R=0)goto A;
printf("%d\n",R);
if(R>0)goto B;
B:
F=M/R;
Z=M*F;
S=M-Z;
A:
return(0);
}

Ok first sorry for everything and next time i will follow u advice, then in the rpoblem number 2 i really dont know what´s AB because the teacher give us this problems in paper and she said that we can´t ask that we need to think, so I´m asking the same question as u, i hope that ab is equal to a multiplication so is to easy and i don´t really see the use of algorithms.
About the first problem thanks for u answer and i will try to search of what u tell me

Thanks for all

Well if it's multiplication it's trivial... but I don't see any purpose in mentioning logarithms. Waiting for others to express their opinion on the matter.

Only thing I'd like to add is: avoid use of goto (useful link to understand why)

One (last?) thing: R = 0 is an assignment, use R == 0 to check wether R is equal to 0 or not.

#include<stdio.h>
#include<conio.h>
#include<math.h>
int main(void)
{
int N, M, R, S;
printf("\nPlease enter N\n");
scanf ("%d", &N);
printf("\nPLease enter  M\n");
scanf ("%d", &M);
R=(N-(M*(N/M)));
if(R > 0)
{
   S=(M-(M*(M/R)));
   printf("S = %d\n",S);
}
printf("R = %d\n",R);
int AB=(M*(M-(M*(M/R))));
printf("AB = %d\n",AB);
system("pause");
return(0);
}

Hey thanks for u help i finally make it work the problem is that i use dev C++ to compile and run but everything work fine if i put the option run until line but when i open the executable file i enter the values and when i finished enter the second value the program just close i read that maybe is because dev doesn´t have the libraries or something like that can anyone help me

Thanks for all i alredy fix my problem and really thanks for the help and i hope that i can help others in the future

AB...???

#include<stdio.h>
#include<conio.h>
#include<math.h>
int main(void)
{
int N, M, R, S;
printf("\nPlease enter N\n");
scanf ("%d", &N);
fflush(stdin);
printf("\nPLease enter  M\n");
scanf ("%d", &M);
R=(N-(M*(N/M)));
if(R > 0)
{
   S=(M-(M*(M/R)));
   printf("S = %d\n",S);
}
printf("R = %d\n",R);

//this is AB..
int AB=(M*(M-(M*(M/R))));
printf("AB = %d\n",AB);
system("pause");
return(0);
}

cikara21 really thanks for all u help u have been really cool and yes i alredy have AB and really again thanks for u help and support and never doubt to ask me anything maybe not of C++ because im not good enough and thanks again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.