Sunday 5 August 2012

How to print square and cube of a given value in C++

Program to print square and cube of given number



#include<iostream>

#include<conio.h>

using namespace std;

int main()
{
       int small_number = 0, larger_number = 0, increment = 0, temp = 0;

       cout << "\n\n\t\tSquare and Cube from Smaller number to Larger number";

       cout << "\n\n  Smaller Number  -   ";

       cin >> small_number;

       cout << "\n\n  Larger Number   -   ";

       cin >> larger_number;

       if(small_number <= larger_number)
       {
              cout << "\n\n  Increment Value -   ";

              cin >> increment;

              cout << "\n\n\t__ Value __ \t\t __ Square __\t\t  __ Cube __";

              for(temp = small_number; temp <= larger_number; temp = temp + increment)
              {
                     cout << "\n\n\t    " << temp << "\t\t\t    " << temp * temp << "\t\t\t      " << temp * temp * temp;
              }
       }
       else
       {
              cout << "\n\n\tSmaller number entered is greater than Larger number";
       }

       getch();

       return 0;
}



Program will scan three numbers -
  1. Smaller number (the initialization point of the for loop)
  2. Larger number (the number till which the for loop is going to run)
  3. Increment number (the number with which the counter is going to be incremented)
The program will check whether the smaller entered is actually smaller than the larger number, if it is the square and cube along with the number will get printed on the console.

No comments:

Post a Comment