The program illustrates the use of continue statements,
The program evaluates the square root of a series of numbers and prints the results. The process stops when the number 9999 is typed in. In case, the series contains the negative numbers, the process of evaluation of square root should be bypassed for such numbers because the square root of a negative number is not defined. The continute statement is used to achieve this. The program also prints a message saying that the number is negative and keeps an account of negative numbers. The fineal output includes the number of positive values evaluated and the number of negative items encountered.
#include <stdio.h>
#include <math.h>
void main()
{
int count, negative;
double number, sqroot;
printf("Enter 9999 to STOP\n");
count = 0 ;
negative = 0 ;
while (count <= 100)
{
printf("Enter a number : ");
scanf("%lf", &number);
if (number == 9999)
break; /* EXIT FROM THE LOOP */
if (number < 0)
{
printf("Number is negative\n\n");
negative++ ;
continue; /* SKIP REST OF THE LOOP */
}
sqroot = sqrt(number);
printf("Number = %lf\nSquare root = %lf\n\n",number, sqroot);
count++ ;
}
printf("Number of items done = %d\n", count);
printf("\n\nNegative items = %d\n", negative);
printf("END OF DATA\n");
getch();
}
The program evaluates the square root of a series of numbers and prints the results. The process stops when the number 9999 is typed in. In case, the series contains the negative numbers, the process of evaluation of square root should be bypassed for such numbers because the square root of a negative number is not defined. The continute statement is used to achieve this. The program also prints a message saying that the number is negative and keeps an account of negative numbers. The fineal output includes the number of positive values evaluated and the number of negative items encountered.
#include <stdio.h>
#include <math.h>
void main()
{
int count, negative;
double number, sqroot;
printf("Enter 9999 to STOP\n");
count = 0 ;
negative = 0 ;
while (count <= 100)
{
printf("Enter a number : ");
scanf("%lf", &number);
if (number == 9999)
break; /* EXIT FROM THE LOOP */
if (number < 0)
{
printf("Number is negative\n\n");
negative++ ;
continue; /* SKIP REST OF THE LOOP */
}
sqroot = sqrt(number);
printf("Number = %lf\nSquare root = %lf\n\n",number, sqroot);
count++ ;
}
printf("Number of items done = %d\n", count);
printf("\n\nNegative items = %d\n", negative);
printf("END OF DATA\n");
getch();
}
No comments:
Post a Comment