Friday, 1 December 2017

Class 8 - Revision C++ Programming


 1. Identify the valid and invalid variables:

            i.       Bond007 – Valid
           ii.       where?    - invalid ( Special character ? not allowed)
          iii.       Prove them wrong – Invalid(Special character space)
          iv.        int – Invalid(keywords not allowed)
           v.        Plug&Play – Invalid(Special character & not allowed)
          vi.        a123 – Valid
         vii.       10DowningStreet – Invalid(Starts with a number)

  2. Correct the  following statements:
    i.       cin>>”a”;
   ii.       cout<<The answer=>>total;
  iii.       Character a=’x’;
  iv.        k=100

                i.   cin>>a;
       The variable should  not be within quotation.      
ii. cout<<”The answer=”<<total;
     The answer =  is just a message ,not a variable          Therefore it should be within quotations. and << is used          with cout.
                iii.      char a=’x’;
      The data type is char.
                iv.       k=100;
      Most of the statements are terminated by ;


   3.  Write the c++ expression for the following :
                             i.   r2
                  = r * r
                            ii.   a3/6
                 = (a*a*a)/6
Here the multiplication ,division symbols should be written correctly.

   4. Evaluate :
                   i.            (10 >100) && (4>0)
                  ii.            !(25 < 20)
                 iii.            (1+2 <6)

Here the answer will be either true or false.The statements use logical and relational operators.
         in question i.  
           10 is not greater than 100 so it is false . But 4 is greater than 0 , so it is true . Remember in AND operation both the statements should be true to get the answer True.
           Therefor the answer is False.
        in question ii.
            25 is greater than 20  so it is False, but because of the NOT(!) operator  the    answer is True.      
                                
A program in C++ involves 4 basic steps.

1.Declaration
2. Assignment / Input
3. Calculation
4. Output

In step 1 , we declare a variable ( data type and a variable name).
In step 2 , we either give values to variables or accept values from                     the user.
In step 3 , Calculation, if any.
In step 4 , Print the answer.

Simple.
Step 1:
Declaration 
The data types of the variables should be mentioned before we use them.
We need to specify whether the number we are going to use is a whole number(int) or a decimal number(float),
or  we are going to use a single letter(char) or group of characters(String).


Step 3
If we have any calculation to do, we do it in step 3.
Step 4
Finally we print the output(answer).

Program 1:

Assign your class and sec and display them .
#include<iostream.h> ------------------------ 1
#include<conio.h>      ------------------------ 2
void main()                 ------------------------- 3
{
clrscr();                       ------------------------- 4
int c;                           ------------------------- 5
char s;                         ------------------------- 6
c=8;                            ------------------------- 7
s=’G’;                         ------------------------- 8
cout<<c<<s; ------------------------- 9
getch();                      
}




In line nos 1 and 2, we have  header files.It contains pre defined functions. iostream conatains functions for cin and cout.
conio has some functions like clrscr which is clear screen .
In line nos 5,6 we mention the data types(declaration).
In line nos  7,8 we assign the values to the variables. and in line 10 , we print the answer.

Program 2:

Accept 3 numbers and find its average.
#include<iostream.h> 
#include<conio.h>      
void main()                
{
clrscr();                      
int a,b,c,tot;                ------------------------- 1
float avg;                    ------------------------- 2
cout<<"Enter first number:";     
cin>>a;                           -------------------------3
cout<<"\n Enter second number:";        
cin>>b;                                    -------------------------4
cout<<"\n Enter third number:";        
cin>>c;                                    -------------------------5       

tot=a+b+c;                 

avg=tot/3;
cout<<"\n Total="<<tot;
cout<<"\n Average="<<avg;
getch();
}

Here we are accepting 3 numbers from the user, they could be anything. Line numbers 3,4,5 could be written in a single line like this cin>>a>>b>>c;
\n is used to take the cursor to the next line while printing, otherwise everything will be in the same line.

Program 3
Accept the side of the square from the user and calculate its area.
Area of square = side x side


#include<iostream.h> 

#include<conio.h>      
void main()                

{
clrscr();                      
int side,area;                

cout<<"Enter the side:";        
cin>>side;                                     

area=side * side;
cout<<"\n Area ="<<area;
getch();
}


No comments:

Post a Comment