Followers

Saturday, April 8, 2023

Program To Calculate The Area Of The Ring

Program:
 #include <iostream>  
 using namespace std;  
 double calcRingArea(double r1, double r2){  // function to calculate area of ring 
      return 3.14* ((r1 * r1) - (r2 * r2));  
 }  
 int main(){  
      double r1, r2;  
      cout<<"Enter the external radius of ring: ";  
      cin>>r1;  
      cout<<"Enter the internal radius of ring: ";  
      cin>>r2;  
      cout<<"The area of ring is "<<calcRingArea(r1, r2)<<endl;     // function call
      return 0;  
 }       
Output:







Working of the Program:
1. This is a C++ program that calculates the area of a ring. 2. The program starts by including the iostream library to handle input and output operations. 3. It defines a function called calcRingArea that takes two double values as input parameters representing the external radius r1 and internal radius r2 of the ring. 4. The calcRingArea function calculates the area of the ring using the formula A = π(R² - r²), where R is the external radius and r is the internal radius, and returns the result as a double value. 5. The main function declares two double variables r1 and r2 to store the user inputs for the external and internal radii of the ring respectively. 6. The main function prompts the user to enter the values of r1 and r2 using the cout and cin statements. 7. The main function calls the calcRingArea function with the input values of r1 and r2 to calculate the area of the ring. 8. Finally, the program displays the calculated area of the ring on the console using the cout statement.
















No comments:

Post a Comment

Program to Find the Length of a String

Program: #include <iostream> using namespace std; int main() { string a; cout<<"Enter the string...