Let’s Do Some More Tasks in C++ To Practice input output statements (cin/cout) in C++.
You May also want to Check Part 1 and Part 2 of Input Output Statment Series Practice
Task 5:
Write a Program that takes age in years and displays age in months and days
1 year = 12 months
1 year= 365 days
Code:
#include<iostream>
using namespace std;
int main()
{
int age;
cout<<"Enter Your age in Years : ";
cin>>age;
int months,days;
months=age*12;
days=age*365;
cout<<"\nYour Age in Months : "<<months<<endl;
cout<<"\nYour Age in Days : "<<days<<endl;
return 0;
}
Output:
Enter Your age in Years : 23
Your Age in Months : 276
Your Age in Days : 8395
Task 6:
Write a Program that Takes input From user and displays square and cube of that number
Use both methods..
Without using pow function,
Using power function
Code:
#include<iostream>
#include<math.h>
//this header file contains all functions of mathematics..
//e.g cos,sin,tan,sqrt,power,exp,etc
using namespace std;
int main()
{
//declare variable
double n;
//taking input
cout<<"Enter Number : ";
cin>>n;
//find cube and square without using pow function
double sq,cube;
sq=n*n;
cube=n*n*n;
cout<<"\nSquare of "<<n<<" is "<<sq<<endl;
cout<<"\nCube of "<<n<<" is "<<cube<<endl;
cout<<"\n---------using Builtin Function---------"<<endl;
//pow(number,power)
//this function accepts two parameters.. one is number and the 2nd is its
//power
sq=pow(n,2);
cube=pow(n,3);
cout<<"\nSquare of "<<n<<" is "<<sq<<endl;
cout<<"\nCube of "<<n<<" is "<<cube<<endl;
return 0;
}
Output:
Enter Number : 6
Square of 6 is 36
Cube of 6 is 216
---------using Builtin Function---------
Square of 6 is 36
Cube of 6 is 216