Even if you can't even understand what you're saying, I think I can guess what your problem is: scanf. My policy is to never use scanf. It leaves the newline in the input stream, which is annoying and leads to exactly the kinds of problems you're trying to describe. Try something like the following code fragment, and I believe all your problems will disappear:
#define MAX_LINE_LEN 80
char line[MAX_LINE_LEN];
do {
printf("Enter length of side: ");
fgets(line, MAX_LINE_LEN, stdin);
} while (sscanf(line, "%f", &a) != 1);
Case Problem 2: C-language
/*The Math Program 1.3 Updated 08/31/06. Created by Kenny Ashman*/
/*****************************************************************
*This program has three functions besides the main which are for *
*The Pythagorean Theorem, Distance Formula and calculating the *
*circumference and area of a circle. The main function is used *
*only for directing you to the other three functions and is *
*returned to if the user enters 0 when prompted for input which *
*is explained in the comments below. *
*****************************************************************/
#include
#include
int Pythag(); //function for pythagorean theorem
int Distance(); //function for distance formula
int Circle(); //function for circle circumference and area
main()
{
int iSelection=0;
while (iSelection!=4) { //begin while loop
system("cls"); //clear screen
printf("\n\tThe Math Program 1.3");
printf("\n1:Pythagorean Theorem");
printf("\n2:Distance Formula");
printf("\n3:Circle Circumference & Area");
printf("\n4:Quit");
printf("\n\nSelect a function:");
scanf("%d", &iSelection); //function selection
if (iSelection==1) {Pythag();} //1 for Pythag
if (iSelection==2) {Distance();} //2 for Distance
if (iSelection==3) {Circle();} //3 for Circle
} //end while loop
} //end main function
int Pythag()
{ //begin Pythag function
int iSelection;
float A, B, C;
system("cls");
printf("\n\tPythagorean Theorem\t(Enter 0 for selection to quit)\n");
printf("\n1:A");
printf("\n2:B");
printf("\n3:C");
while (iSelection==1 || 2 || 3) { //begin while loop
printf("\nSelection:");
scanf("%d", &iSelection); //selecting what you need. A, B or C
if (iSelection==1) {
printf("\nB:");
scanf("%f", &B);
printf("C:");
scanf("%f", &C);
A=sqrt((C*C)-(B*B)); //pythagorean theorem for A
printf("\nA=%f", A);
printf("\n-----------");
}
if (iSelection==2) {
printf("\nA:");
scanf("%f", &A);
printf("C:");
scanf("%f", &C);
B=sqrt((C*C)-(A*A)); //pythagorean theorem for B
printf("\nB=%f", B);
printf("\n-----------");
}
if (iSelection==3) {
printf("\nA:");
scanf("%f", &A);
printf("B:");
scanf("%f", &B);
C=sqrt((A*A)+(B*B)); //pythagorean theorem for C
printf("\nC=%f", C);
printf("\n-----------");
}
if (iSelection==0) {
return 0;//if selection = 0 return to the main function
}
}//end while
} //end Pythag function
int Distance()
{ //begin Distance function
float X1, X2, Y1, Y2, D;
system("cls"); //clear screen
printf("\n\tDistance Formula\t(Enter 0 for all coordinates to quit)");
while (1==1){ //begin while loop
printf("\nFirst Point:"); //enter the points
printf("\nX:");
scanf("%f", &X1);
printf("Y:");
scanf("%f", &Y1);
printf("Second Point:");
printf("\nX:");
scanf("%f", &X2);
printf("Y:");
scanf("%f", &Y2);
if (X1==0, X2==0, Y1==0, Y2==0) {
return 0; //if both points = 0,0 then return to main function
}
D=sqrt((X2-X1)*(X2-X1)+(Y2-Y1)*(Y2-Y1)); //the distance formula
printf("\nDistance=%f", D);
printf("\n-----------------");
} //end while loop
} //end Distance function
int Circle()
{ //begin Circle function
float Pi=(4*atan(1));
float R, C, SA;
system("cls"); //clear screen
printf("\n\tCircle Circumference & Surface Area\t(Enter 0 for radius to quit)");
while (1==1) { //begin while loop (I don't know why I put 1==1 for the condition but whatever, it matters not...I hope)
printf("\nRadius:");
scanf("%f", &R); //prompted for the Radius
if (R==0){
return 0;//if the radius = 0 return to the main function
}
C=Pi*R*2; //formula for circumference
SA=Pi*R*R; //formula for surface area
printf("Circumference=%f", C);
printf("\nSurface Area=%f", SA);
printf("\n-----------------------");
}//end while
}//end circle function
Case Problem 3:
Pythagorean Triplet Problem
This is a discussion on Pythagorean Triplet Problem within the C Programming forums, part of the General Programming Boards category; I am solving the euler probelm (no. 9) that wants to find the only Pythagorean triplet such that a+b+c = ...
#include
#include
#include
int
main()
{
int
a,b,c;
int
max = 500;
int
aSquared =
pow
(a,2);
int
bSquared =
pow
(b,2);
int
cSquared;
for
(a = 1; a < max; a++)
{
for
(b = 1; b < max; b++)
{
int
aSquared = a*a;
int
bSquared = b*b;
cSquared = aSquared + bSquared;
c =
sqrt
(cSquared);
if
(a
printf
(
"%d is a, %d is b, %d is c.\n"
, a, b, c);
}
}
system
(
"PAUSE"
);
return
0;
}
note:
There is no need to redefine 'aSquared' and 'bSquared' as integers within the loop. You already declared them as variables.
Is there some restriction from using the obvious three nested loops for this problem?
Not a particularly elegant solution, but it finds the answer in less than one second.
#include
#define MAX 500
int
main()
{
unsigned
long
int
a,b,c,asqr,bsqr,csqr;
for
(a=1;a
for
(b=1;b
for
(c=1;c
if
(a + b + c == 1000) {
asqr=a*a;
bsqr=b*b;
csqr=c*c;
if
(asqr + bsqr == csqr) {
printf
(
"\nasqr=%2lu bsqr=%2lu csqr=%2lu\n"
, asqr, bsqr, csqr);
printf
(
"\nAnswer is: a=%lu b=%lu c=%lu"
, a,b,c);
printf
(
"\n\n\t\t\t press enter when ready"
);
(
void
)
getchar
();
return
0;
}
}
}
}
}
printf
(
"\n\n\t\t\t press enter when ready"
);
(
void
)
getchar
();
return
0;
}