Explaination for this programme

hi. Can anyone Please explain me this programme....

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
  #include <stdio.h>
  #include <conio.h>
  #include <iostream.h>

  int main()
  {

     int a,b,;
     clrscr();
     cout<<"enter no.1:";
     cin>>a;
     cout<<"enter no.2:";
     cin>>b;
     while(a!=b)
     {
         if(a>b)
           {
             a=a-b;
           }
          else(b>a)
           {
              b=b-a;
           }
       }
        cout<<"H.C.F="<<a;  
       getch();
       return 0;
}


Thank you very much....
Last edited on
Hello Mite,

I will give this a try.

Starting at the top lines 1, 2 and 3 are the header file that are included in the program. These files include function proto types or forward declarations of functions. They also include functions that you would use in your program like "iostream" for "cout" and "cin".

Line 2 is a problem. This is not a C++ header file, but a C header file. This may work on your compiler and it dos work with my compiler, but it may not work on other compilers that do not have this file as part of the include files.

If I try to run your program here it gives the error that "conio.h" could not be found and the program will not run. By that I mean that in the top right corner of the code block is a small gear icon. Pressing this will open another tab allowing you to compile and run the program.

I will also mention that line 1 the "stdio.h" file is a C header file and there is nothing in the program that needs this file.

Line 8 defines two variables of type "int". A good practice to get into is to initialize these variables before they are used. You have three choice for initialization:
1. int a = 0, b = 0; 2. int a(0), b(0); or 3. int a{ 0 }, b{ 0 }; I prefer the third one my self.

Line 9 may work for you , but it will not work for everyone. Replacing this will take some time to research what is best for cross platform use.

Lines 10 and 12 are simple statements to write to the screen to tell the user what to do.

Lines 11 and 13 allow the program to get input from the keyboard.

Line 14 starts a while loop that will continue until the condition a != b becomes false. Then the loop will end and execution will continue with line 25.

Line 16 is the start of an "if" statement. the condition of a > b if it evaluates to true the program would continue with line 18 then jump to line 24 and start the while loop over checking the while condition to see if it would execute again or end the while loop. FYI line 18 could be more simply written as a -= b; and the same would apply to line 22.

Line 20 is nor written correctly. A simple else does not have a condition. Either the else should be written as else // (b > A) to show what is meant for the else or as else if (b > a). The "if" to make use of the condition.

Line 22 is similar too line 18.

Line 24 is the end of the while loop. From here it program jumps back to the condition of line 14.

Line 25 will print the text to the screen plus the value of "a".

Line 26 is used to hold the console window open until any key is pressed. This requires the "conio.h" header file and may not work on all platforms. You may fild that getch(); is outdated and that _getch(); may be needed. This depends on the compiler.

Lines 27 and 28 are the end of the program. With line 27 exiting the program and returning control to whatever called the program to start it. And line 28 being the closing brace of main.

Hope that helps,

Andy

P.S. While playing with the program I found an extra "," on line 8 before the ";".
Last edited on
line 3 is also not a C++ header file. This looks like a Turbo C++ program from 1992.
If a and b variables are not equals, your final output for positives numbers could be both are the same, in other words if you try a=5 and b=3 for example your final output will be 1, because both variables will arrive at 1.
Thank you very very much everyone.
Topic archived. No new replies allowed.