0

This is the code that i used to find the area of unspecified number of circles.

#include<stdio.h>
#define pi 3.1415926
float process(float radius);
float radius, area;
int count;
float process (float radius){
    float a;
    a = pi*radius*radius;
    return a;
}

int main(){
    printf("!!!This program is used to find the radius of circles.!!!\n");
    printf("To terminate the program enter radius 0.\n");
    printf("Enter the radius of the first circle.\n");
    scanf("%f", &radius);
    for(count = 0; radius!=0; count++)
    {

       if (radius>0)
       {
            area = process(radius);
            printf("Area of circle %d is %f\n", count + 1, area);
       }
       else
       {
           printf("The radius of a circle can't be negative\n");
       }
       printf("Enter the radius of circle %d\n", count + 2);
       scanf("%f",&radius);
    }
    printf("Thank you for using my program");

    return 0;
}

After calculating the area of the first circle. if I enter the '/' symbol the for loop keeps on running infinitely and outputs the same area as the first circle without any additional input radius.

I don't really know enough for me to try other other things. Hope someone can help.

Karthick
  • 1,010
  • 1
  • 8
  • 24
  • 1
    You should always check the return value of `scanf`. Also, what exactly did you expect to happen if you enter a `'/'` when your program wants to read a floating point value? – Gerhardh May 11 '23 at 07:45

0 Answers0