Skip to content

Instantly share code, notes, and snippets.

@lettergram
Created March 19, 2015 01:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lettergram/eaf945540c7502358dce to your computer and use it in GitHub Desktop.
Save lettergram/eaf945540c7502358dce to your computer and use it in GitHub Desktop.
/*
* The program should be able to calculate the energy required to move two charges
* near eachother with a starting distance.
*
* Created by: Austin Walters
*/
# include <stdio.h>
# include <math.h>
# define ke 8.98755e9
int
main(void)
{
/* Charge of units being brought in going from top left clockwise*/
double q_1;
double q_2;
double r; /* Final distance */
double R; /* Original distance */
double x; /* Value of origional configuration */
double y; /* Value of final configuration */
double U; /* Potential Energy */
printf("This program only works to calculate the energy required to group");
printf("two charges in and calculate the energy required, from a set starting distance");
printf("and end distance \n");
printf("Enter the charge of q, please take only the single value of q.");
printf("Here is an example: if there is something such as 2q and q being q =");
printf("3.2e-5c, you only need to have 3.2e-5, adding in the C will cause a malfunction.\n");
printf("\nIf the charge comes from a distance infinity enter 9999 for origional distance\n\n");
printf("q1 (Charge One) > ");
scanf("%lf", &q_1);
printf("q2 (Charge Two) > ");
scanf("%lf", &q_2);
printf("Please enter the origional distance in meters. > ");
scanf("%lf", &R);
printf("Please enter the final distance in meters. > ");
scanf("%lf", &r);
if(R == 9999){
U = ((ke*(q_1)*(q_2)) / (R));
}else{
x = ((ke*((q_1)*(q_2))) / (R));
y = ((ke*((q_1)*(q_2))) / (r));
U = (y - x);
}
printf("The total energy required to bring the charges to the configuration is");
printf("\n> %.9f Joules\n\n", U);
system("pause");
return(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment