Pages

Friday, July 19, 2013

Write a program that Read a Number and Square in C++

This program read a number and print its Square

Source Code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
    #include <iostream.h>
    #include <conio.h>
    using namespace std;
    
    // Prototype Declarations
    int  getNum   (void);
    int  sqr      (int x);
    void printOne (int x);
    
    int main()
    {
        // Get a number and square it
        int a = getNum ();
        // Square the number just read
        int b = sqr (a);
        // Now print it
        printOne (b);
        // Done. Return to operating system.
        return 0;
                 }//main
        /*   ============= getNum ===============
         Read number from keyboard and return it.
         Pre   nothing 
         Post  number read and returned         */
         int getNum (void)
         {
           cout << " Enter a number to be a squared: ";
           int     numIn;
           cin  >> numIn;  
           return  numIn;
             } //getNum
        /*   =============== sqr ====================
         Return the square of the parameter.
         Pre  x contains number to be squared
         Post squared value returned          */     
         int sqr (int x)
         {
             return (x * x);
             }// sqr
         /*  ============= printOne =================
          print one integer value
          Pre  x contains number to be printed
          Post squared value printed.            */
         
         void printOne (int x)
         { 
              cout << " The value is: " << x << endl;
         getch();
         return;
              } // printOne

OutPut:

 Enter a number to be a squared: 76
 The value is: 5776

No comments:

Post a Comment