Wednesday, February 16, 2011

Decision Making and Branching

Program presented here illustrates the use of the goto statement. The program evaluates the square root for five numbers. The variable count keeps the count of numbers read. When count is less than or equal to 5, goto read; directs the control to the label read; otherwise, the program prints a message and stops.

#include <stdio.h>
#include <math.h>
void main()
{
double x, y;
int count;
count = 1;
printf("Enter FIVE real values\n");
read:
scanf("%lf", &x);
printf("\n");
if (x < 0)
printf("Value - %d is negative\n",count);
else
{
y = sqrt(x);
printf("%lf\t %lf\n", x, y);
}
count = count + 1;
if (count <= 5)
goto read;
printf("\nEnd of computation");
getch();
}

 

No comments:

Post a Comment