C++ program for printing square if number is even otherwise print cube.

Q30. Write a C++ program to input a number. If the number is even, print its square otherwise print its cube.

Ans.

#include<iostream.h>  //for standard input and output

#include<conio.h>     //for clrscr()

 

//Main function

void main()

{

//Initialization

int n, res;

clrscr();  //for clear screen

 

//Input from user

cout<<“\n Enter a number: “;

cin>>n;

 

//Calculation

res=n%2==0?n*n:n*n*n;

 

//Output

cout<<“\n The resultant is: “<<res;

 

getch();  //To hold the output screen

 

}

//End of main

 

OUTPUT1:

 

Enter a number: 8

The resultant is: 64

OUTPUT2:

 

Enter a number: 5

The resultant is: 125

Leave a Reply

Your email address will not be published. Required fields are marked *

722 Views
%d bloggers like this: