# Working with 'while' loop.

This is a discussion on Working with 'while' loop. within the C Programming forums, part of the General Programming Boards category; Hi again everyone. So, I got past the segfault error in my code and now it compiles and returns an ...

1. ## Working with 'while' loop.

Hi again everyone. So, I got past the segfault error in my code and now it compiles and returns an actual output. However, the output is backwards. For example, besides the numbers being off, the first line is T=1.87... and R = 0.45. However, it should be flipped. That is, when T=1.87..., R should be 0. Here's the code:

Code:
```#include <stdio.h>
#include <math.h>

double G, T, Mtot, R, v, B, A, C, Pi, distance;

int main (void)
{

G = 6.67e-11;
Pi = 3.14159;

printf("Enter the combined mass of the systems (kg):");
scanf("%lf", &Mtot);

printf("Enter distance between galaxies (kpc):");
scanf("%lf", &R);

printf("Enter the relative velocity of the approaching galaxy (km/s):");
scanf("%lf", &v);

while (R > 1.0e-21)
{

C = ((pow(v,2))/2.0) - (G*Mtot)/R;

T = (2.0*Pi*v*(pow(R,2)))/(C*G*Mtot);
printf("\nWhen T = %d years",T);
printf("  then the other galaxy is %f kpc away",R);
R=R-0.05;
}

}```
Does anyone know why it's doing this or what I could do to fix it?

In case another explanation of what my code should do is needed:

The program is meant to calculate the time it would take for two galaxies to merge after meeting. Treating them both as point-masses, here are the inputs:

R = distance between the galaxies = 0.45 (kpc)
Mtot = total mass of the two galaxies = 3762e39 (kg)
v = velocity of the approaching galaxy = 150 (km/s)

The program plugs that into the equation for C and T = (2*Pi*v*R^2)/(C*G*Mtot) and outputs the value for T, which comes out to be 1.87 x 10^9.

What I'm trying to do with the second part is have the program able to return that T in increments. For example, when T = 1 x 10^9 (1 billion years), then the other galaxy is ___ kpc away, and so on. And I want it to be able to do this starting from the initial R, 0.45 kpc, and ending when R = 0.

2. You could end it when R hits 0 but you'll get your segfault back because you'll be dividing by 0 in your first equation... as we've already seen.

BTW.. since this is essentially the same as your last thread... there was no reason to create a new one.

3. Ah, I'm sorry. I wasn't entirely sure, since the problem I'm dealing with now is different.