Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 149 - CoreGPT(Podar)_Flipbook_C8
P. 149

    •  if(x) // Same as: if (x != 0)
                         cout<< x << " is a nonzero number";
                     •  if(guess == num)
                         cout<< "You guessed the right number!";

                     •  if(x >= y)  // Corrected logical condition
                         cout<< x << " is greater than or equal to " << y;

                     Let us take an example given below:
                     void main()
                     {

                         clrscr();
                                                                        Clickipedia
                         int a;
                         cout<< "Enter any number: ";
                                                                         You  can  also  write  conditional  statements
                         cin>> a;
                                                                         without the braces if there is only one line
                         if(age > 100)
                                                                         of code.
                         {
                             cout<< "The number "<<a<<" is greater than 100";
                         }

                         getch();
                     }
                     In  the  above  code,  if  the  value  in  a  is  greater  than  100,  the  statement  written  inside  the  if

                     statement is executed. Otherwise, C++ skips the entire block and continues with the program.
                     Program1: To take a number from the user as input and print the cube of it. Show a message
                     "Too Large Number", if user enters a number greater than 20.

                     #include<iostream.h>
                     #include<conio.h>

                     void main()
                     {
                         clrscr();
                         int n, cu;
                         cout<< "Enter a number less than 21: ";

                         cin>> n;
                         if(n <= 20)                                        Enter a number less than 21: 20
                         {                                                  The cube of 20 is: 8000

                          cu = n * n * n;
                          cout<< "The cube of " << n << " is: " << cu;
                         }

                                                                                      Developing Programs in C++        147

| 144 145 146 147 148 149 150 151 152 153 154 |