C++ program for printing Square Root if number is odd and positive otherwise print n5.
Q31. Write a C++ program to input a number. If the number n is odd and positive, print its square root otherwise print n5.
Ans.
#include<iostream.h> //for standard input and output
#include<conio.h> //for clrscr() and getch()
#include<math.h> //for sqrt() and pow()
//Main function
void main()
{
//Initialization
int n;
float res;
clrscr(); //for clear screen
//Input from user
cout<<“\n Enter a number: “;
cin>>n;
//Calculation
res=n%2!=0&&n>0?sqrt(n):pow(n,5);
//Output
cout<<“\n The resultant is: “<<res;
getch(); //To hold the output screen
}
//End of main()
OUTPUT1:
Enter a number: 4
The resultant is: 1024
OUTPUT2:
Enter a number: 9
The resultant is: 3