Monday 6 April 2015

Programs in C++ With Syntax

QUE 1- WAP to find square of a number.
     #include<iostream.h>    #include<conio.h>   
     void main()   
   { clrscr();    
      int num, square;    
      cout<<"enter the num:";    
      cin>>num;    
      square=num*num;    
      cout<<"the square of given num:";    
      cout<<square;    getch();    }


QUE 2- WAP to  find percentage of marks in 3 subjects.

      #include<iostream.h>
      #include<conio.h>
      int main()
     { clrscr();
       float sub1, sub2, sub3, marks, perc;
       cout<<"Enter marks obtained in 3 subjects:";cin>>sub1 >>sub2 >>sub3;
       marks =sub1+sub2+sub3;perc=(marks/300)*100;
       cout<<"\n"<<"the percentage marks are:"<<perc<<"%";
       getch();return 0;
      }

QUE 3- WAP to add, subtract,multiply and divide 2 numbers.
        #include<iostream.h> 
        #include<conio.h>
        int main()
       {  clrscr();   
          char ch;   
          float a, b, result;   
          cout<<"Enter two numbers:";   
          cin>>a>>b;  
          cout<<"\n"<<"Enter the operator(+,-,*,/):";  
          cin>>ch;   
          cout<<"\n"; 
          if(ch=='+')   result=a+b;   else   if(ch=='-')   result=a-b;   else   if(ch=='*')   result=a*b;   
          else   if(ch=='/')   result=a/b;  
          else   cout<<"wrong operator \n";   
          cout<<"\n"<<"The calculated result is:"<<result<<"\n";
          getch();   
         return 0;   
      }




QUE 4- WAP to find area of circle & circumference.
#include<iostream.h>
#include<conio.h>
int main()
{clrscr();
int choice,r, Area;
cout<<"Area menu"<<"\n";
cout<<"1.Area of_circle"<<"\n";
cout<<"2.Area of_circumference"<<"\n";
cout<<"Enter your choice (1-2):";
cin>>choice;if(choice==1){cout<<"\n"<<"Enter the value of r:";
cin>>r;Area = 3.14*r*r;cout<<"\n"<<"The Area of_circle is"<<Area<<"\n";}
else{cout<<"\n"<<"Enter the value of r:";cin>>r;Area = 2*3.14*r;
cout<<"\n"<<"The Area of_circumference is"<<Area<<"\n";}
getch();
return 0;}


QUE 5- WAP to find cube of a number.  
 #include<iostream.h>  
 #include<conio.h>   
void main()   
{clrscr();   
int num, cube;   
cout<<"enter the num:";  
 cin>>num;   
cube=num*num*num;   
cout<<"the cube of given num:";  
cout<<cube;
getch();   }


QUE 6- WAP to find simple interest.

#include<iostream.h>
 #include<conio.h>
 int main()
 {clrscr(); 
int p, r, t, SI; 
cout<<"Enter the value of p:";
 cin>>p; 
cout<<"Enter the value of r:"; 
cin>>r; 
cout<<"Enter the value of t:"; 
cin>>t;
 SI=p*r*t/100; 
cout<<"The SI of given values is:"; 
cout<<SI; 
getch();
return 0;}
 
QUE 7- WAP to check a no. is even or odd.
#include<iostream.h>
 #include<conio.h> 
int main()
 {clrscr();
 char ch;
 int num, i; 
cout<<"Enter the num:";
 cin>>num; 
if(num%2==0)cout<<"even num";elsecout<<"odd num"; 
getch(); 
return 0;}


QUE 8- WAP to check a given year is leap or not
#include<iostream.h> 
#include<conio.h> 
int main() 
{clrscr();
 int year;
 cout<<"Enter year(in 4-digits)\n";
 cin>>year; 
if(year%100==0){if(year%400==0)  cout<<"leap year\n";
 }else if(year%4==0)cout<<"leapyear\n";
 elsecout<<"not a leap year\n"; 
getch(); 
return 0;}



QUE 9- WAP to find greatest no. between two nos
#include<iostream.h> 
#include<conio.h> 
int main() 
{clrscr();
 int a, b;
 cout<<"Enter the value of a:";
 cin>>a; 
cout<<"Enter the value of b:"; 
cin>>b; 
if(a>b)cout<<"a is greater than b:";
 elsecout<<"b is greater than a:"; 
getch(); 
return 0;}

QUE -10 WAP to find greatest no. among three nos.    
#include<iostream.h> 
#include<conio.h>    
int main()   
 {     int x, y, z, max; 
 cout<<"Enter three numbers :";   
  cin>> x>> y>> z;     max =x;   
  if(y>max)     max =y;     if(z>max)     max =z;  
  cout<<"\n"<<"the largest of"<<x<<","<<y<<"&"<<z<<"is"<<max;  
  getch();   
 return 0;    }

QUE 11- WAP to check a no. is negative , positive, or zero.
#include<iostream.h>
#include<conio.h>
int main()
{ clrscr();
int no;
cout<<"Enter the value of no:";
cin>>no;
if(no==0)cout<<"zero no";
elseif(no>0)cout<<"positive no";
elseif(no<0)cout<<"negative no";
getch();
return 0;}



QUE 12- WAP which accept percentage marks of student and print his result as given criteria:
pm>35                                        failpm>=35 and pm<50                  3rd division
pm>=50 and pm <60                 2nd divisionpm>=60 and pm<95                  1st  division
otherwise student passed with  distinction

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{clrscr();
int pm;
cout<<"Enter the value of pm:";
cin>>pm;
if(pm<35)cout<<"/n fail";
if(pm>=35 && pm <50)cout<<"/n third division";
if(pm>=50 && pm <60)cout<<"/n second division";
if (pm>=60 && pm <95)cout<<"/n first division";
if(pm>95)cout<<"/n distinction";
elsecout<<"";
getch();}

QUE 13- WAP to calculate sum of each row and each column of a 2d array.

#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
char ch(); 
 int i,a[10][20],j,sum; 
 for(i=0;i<3;i++)  
{  for(j=0;j<2;j++) 
 { cout<<"enter the no for row="<<i+1<<"and col="<<j+1<<"\n";
  cin>>a[i][j];  }  }  cout<<a; 
    for(i=0;i<3;i++)  {  for(j=0;j<2;j++)  
  {  cout<<a[i][j]<<"\t"; 
   }getch();  cout<<"\n";
  }  for(j=0;j<2;j++)  
   { sum=0;  for(i=0;i<3;i++)  
   {  cout<<"sum of"<<j+1<<" column"<<"\n"<<sum;
  getch(); 


QUE 14- WAP to convert temperature given in celsius into fahrenheit and vice versa.
#include<iostream.h>
#include<conio.h>
int main()
{clrscr();
int choice;double temp, conv_temp;
cout<<"Temperature conversion menu"<<"\n";
cout<<"1.Fahrenheit to ceisius"<<"\n";
cout<<"2.celsius to fahrenheit"<<"\n";
cout<<"Enter your choice (1-2):";
cin>>choice;
if(choice==1){cout<<"\n"<<"Enter temperature in fahrenheit:";
cin>>temp;conv_temp=(temp-32)/1.8;
cout<<"the temperature in celcius is "<< conv_temp<<"\n";}
else{cout<<"\n"<<"Enter temperature in celcius:";
cin>>temp;
conv_temp=(1.8*temp)+32;
cout<<"The temperature in fahrenheit is"<<conv_temp<<"\n";
}
getch();
return 0;

QUE 15- WAP to make a calculator which has all the arithmetic operations e.g. +,-,*,/,% and remainder through switch case statement.

#include<iostream.h>
#include<conio.h>
int main()
{  clrscr();   
float op1, op2,  op3, res; 
 char ch;   
cout<<"Enter two numbers:"; 
  cin>>op1>>op2; 
  cout<<"\n"<<"Enter the operator(+,-,*,/,%):";
  cin>>ch;  
 cout<< "\n";  
 switch(ch)   {  case+'+':res=op1+op2;                 
                                   break ; 
                          case'-':res=op1-op2;           
                                   break ;      
                          case'*': res=op1*op2;      
                                   break ;    
                          case'/':if(op2==0)    
       
             cout<<"divide by zero error!!!";  
                    else                              
               res=op2/op1;                 
                                    break ;       
                  case'%':if(op2==0)            
                        cout<<"divide by zero error!!!";                   
                             else                     
                  {  int r, q;                   
                         q= op2/op1;                    
                        r= op2-(q*op1);     
                        res=r;                 
                       }                   
                break ;  
               default:cout<<"\n"<<"wrong operator!!!";   }
              cout<<"The calculated result is:"<<res<<"\n"; 
             getch(); 
             return 0;   } 
OUE 16- WAP to calculate the table of any no. 
#include<iostream.h>
 #include<conio.h>
 int main()
 {clrscr();
 int num; 
cout<<"\n enter number:";
 cin>>num; 
for(int i=1; i<11; ++i)cout<<"\n"<<num<<"*"<<i    <<"="<<num*i;
 getch();
 return 0;}


QUE 17- WAP to find factorial of any no. 
#include<iostream.h>
#include<conio.h> 
int main() 
{clrscr();
 unsigned long i , num, fact=1;cout<<"Enter integer:";cin>>num;i= num;while(num){fact=fact* num; --num;}cout <<"the factorial of"<<i<<"is"<<fact<<"\n";getch();return 0;}

QUE 18- WAP TO PRINT THE FIBONACCI SERIES UP TO NTERM.
 #include<iostream.h>
 #include<conio.h> 
int main() 
{clrscr();
 unsigned long first, second, third, n;first=0; 
second =1; 
cout<<"How many element(>5)?\n"; 
cin>>n; 
cout<<"Fibonacci series\n"; 
cout<<first<< " "<<second;for(int i=2; i<n; ++i){third= first+second;cout<<" "<<third;first =second;second=third; 
}
 getch(); 
return 0;}

QUE 19- WAP to find sum of the digit of a no
.#include <iostream.h>
#include<conio.h> 
int main() 
{clrscr(); 
int num;int sum = 0; 
cout << " Enter a number : ";
 cin >> num; 
while ( num > 0 ) {sum += num % 10;num /= 10; 
}cout << "Sum = " << sum; 
getch(); 
return 0; 
}
                                      OR 
USING FUNCTIONS 
#include<iostream.h> 
#include<conio.h>
 int main() 
{clrscr(); 
int sum(int); 
int s,i; 
cout<<"Enter the digit whose sum be calculated:"; 
cin>>s; 
i=sum(s); 
cout<<"the sum of"<<"s is:"<<i; 
getch(); 
return 0;
 }int sum(int s)
 {int res, sum=0; 
    res=s%10;s=s/10; 
    sum=res+sum; 
    return sum;
 }

QUE 20- WAP to check whether a given no. is prime or not.
#include<iostream.h>
 #include<conio.h>
 #include<process.h>
 int main() 
{clrscr(); 
 int num, i;
 cout<<"\n Enter the number:";
 cin>>num; 
for(i=2; i<=num/2; ++i)   
 if(num%i==0)  
  {    cout<<"\not a prime number!!!";   
       exit(0);  
  }cout<<"\n it is prime number:"; 
getch();
 return 0;}

QUE 21- WAP to find out the electricity bill as given in following criteria when unit consumed input by user (monthly rent is Rs120 to charged from every consumer).
Unit consumed                                     Rate per unitFirst 100                                   Rs 1.50Next 200                                                      RS 2.00Next500                                             Rs4.00Beyond850                                          Rs 5.00

 #include<iostream.h>
 #include<conio.h>
 int main() 
{clrscr(); 
 float a; 
cout<<"Enter the unit consumed :";
 cin>>a; 
cout<<"The electricity bill is :"<<"Rs"<<120+(a*1.50);
 if (a>10&&a<=350)cout<<"The electricity bill is :"<<"Rs"<<120+150+500+((a-350)*4.00); 
if(a>850);
cout<<"The electricity bill is :"<<"Rs"<<120+150+500+2000+((a-850)*5.00); 
getch(); 
return 0;}



 Que 22:-GCD of a number.
 #include<iostream.h>
 #include<conio.h>
 int main()
 { clrscr(); 
 int a,b,temp;  
cout<<"Enter the 1 integer:"; 
 cin>>a; 
 cout<<"Enter the 2 integer:"; 
 cin>>b; 
 while(b!=0){  temp=a%b;  a=b;  b=temp;  
}  cout<<a<<endl; 
 getch(); 
 return 0; 
 }


QUE 23- WAP to find the sum of following series:1+(1+2)+(1+2+3)+...........N.
 #include<iostream.h> 
#include<conio.h> 
int main()
{clrscr();
 int n; 
cout<<"Enter the num:"; 
cin>>n; 
int i,j, sum=0; 
for(i=1; i<=n; i++){for(j=1; j<=i; j++){sum+=j;
 }}cout<<sum<<"\n"; 
getch(); 
return 0; 
}

Control Statements in C++ / C with Syntax

loops



      Loop Type
                 Description

      1.while loop
Repeats a statement or group of statements while a 
given condition is true. It tests the condition before 
executing the loop body.

       2.for loop
Execute a sequence of statements multiple times 
and abbreviates the code that manages the loop
variable.

       3.do...while           loop
Like a while statement, except that it tests the condition 
at the end of the loop body

    nested loops
You can use one or more loop inside any another 
while, for or do..while loop.



While Loop:
     1.  A while loop statement repeatedly executes a target statement as long as a given condition is true.

      Syntax:      The syntax of a while loop in C++ is:
                                                                                                           


   while(condition)
                         {
                                  statement(s);
                         }        
                           
Here, statement(s) may be a single statement or a block of statements. The condition may be any expression, and true is any nonzero value. The loop iterates while the condition is true.

EXAMPLE:

#include <iostream.h>
int main ()
{
// Local variable declaration:
int a = 10;
// while loop execution
while( a < 20 )
{
cout << "value of a: " << a << endl;
a++;
}
return 0;
}



FOR LOOP :

     1.  A for loop is a repetition control structure that allows you to  efficiently write a loop that needs to execute a specific number of times
     2.  Syntax:


 for ( init; condition; increment )
        
         {
          statement(s);
          }
                  
Here is the flow of control in a for loop:

·        The init step is executed first, and only once. This step allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears.
·        Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow of control jumps to the next statement just after the for loop.
·        After the body of the for loop executes, the flow of control jumps back up to the increment statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the condition.
·        The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). After the condition becomes false, the for loop terminates


EXAMPLE.

#include <iostream.h>
int main ()
{     
    for( int a = 10; a < 20; a ++ )  // for loop execution
  
     {
        cout << "value of a: " << a << endl;
      }

return 0;
}



Do…while Loop:

1.      Unlike for and while loops, which test the loop condition at the top of the         loop, the do...while loop checks its condition at the bottom of the loop.

2.      A do...while loop is similar to a while loop, except that a do...while loop is   guaranteed to execute at least one time.

3.     Syntax:

                  do
                    {
                       statement(s);
                      }while( condition );


·        Notice that the conditional expression appears at the end of the loop, so the statement(s) in the loop execute once before the condition is tested.

·        If the condition is true, the flow of control jumps back up to do, and the statement(s) in the loop execute again. This process repeats until the given condition becomes false




·        EXAMPLE:

#include <iostream.h>  
int main ()
{
                // Local variable declaration:
int a = 10;
             // do loop execution
do
{
cout << "value of a: " << a << endl;
a = a + 1;
}while( a < 20 );
return 0;
}



The following program uses a nested for loop to find the prime numbers from 2 to 100:

#include <iostream.h>
int main ()
{
int i, j;
for(i=2; i<100; i++) {
   for(j=2; j <= (i/j); j++)
           if(!(i%j)) break;                                               // if factor found, not prime
                    if(j > (i/j)) cout << i << " is prime\n";
}
return 0;
}




Loop Control Statements:   Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed.

Control Statement
Description

break statement
Terminates the loop or switch statement
 and transfers execution to the statement 
immediately following the loop or switch.

continue statement
Causes the loop to skip the remainder of its 
body and immediately retest its condition 
prior to reiterating.


goto statement
Transfers control to the labeled statement. 
Though it is not advised to use goto statement
 in your program.


Break statement:
Ø When the break statement is encountered inside a loop, the loop is immediately terminated and program control resumes at the next statement following the loop.
Ø  It can be used to terminate a case in the switch statement
Ø Syntax:   break;
Ø EX:
#include <iostream.h>
int main ()
{
int a = 10;      // Local variable declaration:
do                    // do loop execution
{
cout << "value of a: " << a << endl;
a = a + 1;
if( a > 15)
{
    break;  // terminate the loop
}
}while( a < 20 );
return 0;
}


Continue statementThe continue statement works somewhat like the break statement. Instead of forcing termination, however, continue forces the next iteration of the loop to take place, skipping any code in between.
For the for loop, continue causes the conditional test and increment portions of the loop to execute. For the while and do...while loops, program control passes to the conditional tests.
Syntax:   continue;

EX:

#include <iostream.h>  
int main ()
{                            
int a = 10;                         // Local variable declaration                 
do                                        // do loop execution
   {
       if( a == 15)
               {
                      a = a + 1;
                      continue;     // skip the iteration.
                }
     cout << "value of a: " << a << endl;
           a = a + 1;
     }while( a < 20 );
return 0;
}







Goto statement: A goto statement provides an unconditional jump from the goto to a labeled statement in the same function.
      Syntax:    
          goto label;
                        ..
                        .
                        label: statement;

      Where label is an identifier that identifies a labeled statement. A labeled statement is any statement that is preceded by an identifier followed by a colon (:).
       EX:

#include <iostream.h>  
int main ()
{
      int a = 10;                      // Local variable declaration:

     LOOP:do                         // do loop execution
              {
                      if( a == 15)
                             {
                                    a = a + 1;
                                 goto LOOP; // skip the iteration.
                                }
   cout << "value of a: " << a << endl;
                        a = a + 1;
               }while( a < 20 );

                         return 0;
 }










Statement
Description

if statement
An if statement consists of a boolean 
expression followed by one or more 
statements.

if...else statement
An if statement can be followed by an 
optional else statement, which executes
 when the boolean expression is false.

switch statement
A switch statement allows a variable to 
be tested for equality against a list of values.

nested if statements
You can use one if or else if statement
 inside another if or else if statement(s).

nested switch statements
You can use one swicth statement inside 
another switch statement(s).



if statement:

·         An if statement consists of a boolean expression followed by    one or more statements.
·        Syntax:    


if(boolean_expression)
           {
                // statement(s) will execute if the  boolean  expression is true
                               }
If the boolean expression evaluates to true, then the block of code inside the if statement will be executed. If boolean expression evaluates to false, then the first set of code after the end of the if statement (after the closing curly brace) will be executed.
     
EX:    #include <iostream.h>
                       int main ()
                                   {                                      // local variable declaration:
                                          int a = 10;             // check the boolean condition

                                         if( a < 20 )
                                         {                     // if condition is true then print the following

                                             cout << "a is less than 20;" << endl;
                                            }
                                               cout << "value of a is : " << a << endl;
                                          return 0; }



if…else statement:
·      An if statement can be followed by an optional else statement, which executes when the boolean expression is false.
·        Syntax:         


  if(boolean_expression)
                     {
                    // statement(s) will execute if the boolean expression is true
                      }
                     else
                   {
                      // statement(s) will execute if the boolean expression is false
              }


EXAMPLE:

#include <iostream.h>
int main ()
{
                                    // local variable declaration:
int a = 100;
                                   // check the boolean condition
if( a < 20 )               // if condition is true then print the following
cout << "a is less than 20;" << endl;
}
else
{                                   // if condition is false then print the following
cout << "a is not less than 20;" << endl;
}
cout << "value of a is : " << a << endl;
return 0;

}