Followers

Showing posts with label area. Show all posts
Showing posts with label area. Show all posts

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.
















Friday, April 7, 2023

Program To Calculate The Area Of A Circle Using The Radius (Input By the User) Using Function

Program:
 #include <iostream>  
 using namespace std;  
 double calcArea(double radius){  
      double area = 3.14 * radius * radius; // area of circle = pi * r* r  
      return area;  
 }  
 int main(){  
      double radius;  
      cout<<"Enter the radius of the circle: ";  
      cin>>radius;  
      cout<<"The area of circle with radius "<<radius<<" is "<<calcArea(radius)<<endl;  
      return 0;  
 }  
Output:
Working Of The Program:
This program calculates the area of a circle given the radius. Here is how it works:
1. The program defines a function called calcArea that takes a single argument, radius, and returns the area of a circle with that radius.
2. Inside the calcArea function, the area is calculated using the formula area = pi * r * r, where pi is the mathematical constant pi (approximately equal to 3.14).
3. The calculated area is then returned by the function using the return statement.
4. In the main function, the user is prompted to enter the radius of the circle.
5. The input radius is read from the user and stored in a double variable called radius using the cin statement.
6. The calcArea function is called with the radius variable as an argument, and the returned area is printed to the console using the cout statement.

Program to Find the Length of a String

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