C++ Why the loop going in infinite loop?

In summary, we discussed finding the greatest common divisor (GCD) or highest common factor (HCF) of two numbers. We looked at using a do-while loop to iterate through potential values of the GCD and used a break statement to exit the loop once we found the correct value. We also discussed the importance of correct placement of statements in order to avoid infinite loops.
  • #1
Raghav Gupta
1,011
76

Homework Statement


I have to find GCD or HCF of two numbers.

Homework Equations

The Attempt at a Solution


Code:
#include<iostream.h>
#include<conio.h>
void main()
{
    clrscr();
    int i,m,n,temp;
    cout<<"Enter two numbers\n";
    cin>>m>>n;
    do
    {
    if(n>m)
    {
     temp=n;
     n=m;
     m=temp;   // this loop I think it is correct but still it is going to an infinite loop.
    }
     i=n;
     if(m%i==0&&n%i==0)
     break;
     i--;
    }while(i>=1);
    cout<<"The HCF is\t"<<i;
    getch();
}
 
Physics news on Phys.org
  • #2
Use some printf statements for debugging (or cout)

See if your code is doing what you think it is doing.

I usually define a debug flag and have debugging statements such as

if(debug) printf("%d %d\n", num, num2);

etc
 
  • Like
Likes Raghav Gupta
  • #3
Where you give i its starting value, you have incorrectly placed the statement so that it is executed during every pass through the loop. You should place it where it gets executed once only.
 
  • #4
NascentOxygen said:
Where you give i its starting value, you have incorrectly placed the statement so that it is executed during every pass through the loop. You should place it where it gets executed once only.
Thanks,
Now I am able to execute and get correct output.
My code looks like this now,
Code:
#include<iostream.h>
#include<conio.h>
void main()
{
    clrscr();
    int i,m,n,temp;
    cout<<"Enter two numbers\n";
    cin>>m>>n;
    i=n;

    do
    {

      if(m%i==0&&n%i==0)
      break;
      i--;
    }while(i>=1);
     cout<<"The HCF is\t"<<i;
     getch();
}
 
  • Like
Likes NascentOxygen

Related to C++ Why the loop going in infinite loop?

1. Why does my loop in C++ seem to never end?

The most common cause of an infinite loop in C++ is having an incorrect or missing condition for the loop's termination. Make sure to check your loop condition and update it appropriately.

2. How can I prevent an infinite loop in C++?

To prevent an infinite loop, always make sure to have a valid termination condition for your loop. Also, be careful when using variables or expressions as your condition, as they may unintentionally evaluate to "true" indefinitely.

3. Can a loop in C++ be infinite by design?

Yes, it is possible to intentionally create an infinite loop in C++. This is often used for tasks that require continuous execution, such as game loops or event handlers.

4. What are the consequences of an infinite loop in C++?

An infinite loop can cause your program to crash or freeze, as it will continue executing the same code indefinitely. This can also lead to high CPU usage and slow down other processes on your computer.

5. How can I debug an infinite loop in C++?

To debug an infinite loop, you can use a debugger or add print statements at different points in your code to track the loop's progress. You can also try stepping through your code line by line to see where the loop goes wrong.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
789
  • Engineering and Comp Sci Homework Help
Replies
14
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
Back
Top