167k views
3 votes
14. Write an if/then/else primitive to do each of the following operations: a. Compute and display the value x ÷ y if the value of y is not 0. If y does have the value 0, then display the message ‘Unable to perform the division’. b. Compute the area and circumference of a circle given the radius r if the radius is greater than or equal to 1.0; otherwise, you should compute only the circumference.

1 Answer

3 votes

Answer:

// part (a).

//include header

#include <bits/stdc++.h>

using namespace std;

//main function

int main() {

// variables

int x_val,y_val;

cout<<"enter value of x:";

// read the value of x

cin>>x_val;

cout<<"enter value of y:";

// read the value of y

cin>>y_val;

// check if y=0 or not

if(y_val==0)

cout<<"Unable to perform the division"<<endl;

else

cout<<"value of x/y is: "<<x_val/y_val<<endl;

return 0;

}

Step-by-step explanation:

Declare variable "x","y" and read the value of both from user.If value of "y" is equal to 0 then it will print "Unable to perform the division" else it will calculate x/y and print its value.

Output:

enter value of x:4

enter value of y:0

Unable to perform the division

enter value of x:5

enter value of y:2

value of x/y is: 2

//part(b)

//include header

#include <bits/stdc++.h>

using namespace std;

//main function

int main() {

// variable to store radius of circle

float cir_rad;

cout<<"enter the radius :";

// read the radius of circle

cin>>cir_rad;

// validate the radius, it must be positive

while(cir_rad<=0)

{

cout<<"radius must be positive:";

cin>>cir_rad;

}

// if radius is greater or equal to 1 Then

// it will calculate area and circumference and print

if(cir_rad>=1)

{

float area=3.14*cir_rad*cir_rad;

cout<<"area of circle is: "<<area<<endl;

float circumference=2*3.14*cir_rad;

cout<<"circumference of circle is: "<<circumference<<endl;

}

// else it will only circumference and print

else{

float circumference=2*3.14*cir_rad;

cout<<"circumference of circle is: "<<circumference<<endl;

}

return 0;

}

Step-by-step explanation:

Read the value of radius from user and Assign it to variable "r".Check if "r" is negative or not.If "r" is negative then again ask user to enter a positive radius. After that if radius is greater or equal to 1 then it will Calculate area and circumference of circle and print.If radius is less than 1 then it will Calculate only circumference and print.

Output:

enter the radius:-2

radius must be positive:2

area of circle is: 12.56

circumference of circle is: 12.56

User Nook
by
5.9k points