Pageviews

Saturday 7 September 2019

+1]7 . Control Statements - Solved Questions from text book


                     PLUS ONE 
COMPUTER APPLICATION / COMPUTER SCIENCE

                                            Chapter 7. Control Statements

                           (+1. Computer Application Questions and   answers from text book)     



1. Write a program to input an integer and check whether it is positive, negative or zero.

        #include <iostream>
using namespace std; int main() { signed long num1 = 0; cout << "\n\n Check whether a number is positive, negative or zero :\n"; cout << "-----------------------------------------------------------\n"; cout << " Input a number : "; cin >> num1; if(num1 > 0) { cout << " The entered number is positive.\n\n"; } else if(num1 < 0) { cout << " The entered number is negative.\n\n"; } else { std::cout << " The number is zero.\n\n"; } return 0; }
Output


Check whether a number is positive, negative or zero :                
-----------------------------------------------------------            
 Input a number : 8                                                    
 The entered number is positive.

 2.Write a program to input three numbers and print the smallest one.

       #include <iostream>
using namespace std;

int main()
{
    int a, b, c, d;
    cout << "Enter the value of a: ";
        cin >> a;
    cout << "Enter the value of b: ";
        cin >> b;
    cout << "Enter the value of c: ";
        cin >> c;

   if (a < b)
        d = a;
    else
        d = b;

        cout << "the smallest of the numbers is: " << ((d < c) ? d : c) << endl; //


    return 0;

}

3. Write a program to input an integer number and check whether it is positive, negative or zero        using     if…else if statement.

     
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int number;
  6. cout << "Enter an integer: ";
  7. cin >> number;
  8. if ( number > 0)
  9. {
  10. cout << "You entered a positive integer: " << number << endl;
  11. }
  12. else if (number < 0)
  13. {
  14. cout<<"You entered a negative integer: " << number << endl;
  15. }
  16. else
  17. {
  18. cout << "You entered 0." << endl;
  19. }
  20. cout << "This line is always printed.";
  21. return 0;
  22. }

 4. Write a program to input a character (a, b, c or d) and print as follows: a - "abacus", b - "boolean", c - "computer" and d - "debugging"

5. Write a program to input a character and print whether it is an alphabet, digit or any other character.

   #include<iostream>
using namespace std;

int main()
{
    char ch;
    cout << "Enter any character";
    cin >> ch;

    // Alphabet checking condition
    if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
    {
        cout << ch << " is an Alphabet";
    }
    else if(ch >= '0' && ch <= '9')
    {
        cout << ch << " is a Digit";
    }
    else
    {
        cout << ch << " is a Special Character";
    }

    return 0;
}

Result

C language techstudy.org
6. Write a program to input a number in the range 1 to 12 and display the corresponding month of the year. (January for the value 1, February for 2, etc.)

        #include <iostream>
#include <string>

using namespace std;
char chr;

int main()
{
 int month;

 cout<<" Enter a number from 1-12."<<endl;
 
 if (month ==1)
  cout<< "January";
 else if (month==2)
  cout<< "February";
 else if (month==3)
  cout<<"March";
 else if (month==4)
  cout<<"April";
 else if (month==5)
  cout<<"May";
 else if (month==6)
        cout<<"June";
 else if (month==7)
  cout<<"July";
 else if (month==8)
  cout<<"August";
 else if (month==9)
  cout<<"September";
 else if (month==10)
  cout<<"October";
 else if (month==11)
  cout<<"November";
 else if (month==12)
  cout<<"December";
 else 
  cout<<"Sorry I need a number from 1-12.";
 cout<< "The month is "<<month;
 cin>>chr;
 return 0;

7. What is the significance of break statement within a switch statement?

     When a break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop. It can be used to terminate a case in the switch statement.

8. Rewrite the following statement using if...else statement result= mark>30 ? 'p' :' f';

  Ans. 

      if(mark>30)

    {

        cout<<result="p";

else

      cout<<result="f";

}

9. Write the significance of break statement in switch statement. What is the effect of
absence of break in a switch statement?

          The break keyword causes the entire switch statement to exit, and the control is passed to statement following the switch.. case construct. Without break, the control passes to the statements for the next case.

10. What will be the output of the following code fragment?

for(i=1;i<=10;++i) ;

cout<<i+5;

 Ans.

11. Write a program using for loop that will print the numbers between 1000 and
2000 which are divisible by 132.

12. Rewrite the following statement using while and do...while loops.

for (i=1; i<=10; i++)

 cout<<i;

Ans.

    int   i=1;

      do

    {

      cout<<i;

      i++;

}

     while(i<=10);


 13. How many times the following loop will execute?

int s=0, i=0;
while(i++<5)
s+=i;

14. Briefly explain the working of a for loop along with its syntax. Give an example of for loop to support your answer.

         Syntax:
  for (initialization ; test expression; update statement)
        {
 body of the loop;
         } 
 eg. 
 for  ( n=1;  n< = 10;  ++n)
  cout << n ;
  At first initialization takes place     n=1
Then test expression evaluated   n<=10
If it is true body of the loop executed, otherwise the program control goes out of the for loop.
After execution of loop body update expression is executed   ++n.  it will repeated until test expression become false

 15. Compare and discuss the suitability of three loops in different situations.

 16. What is wrong with the following while statement if the value of z = 3. while(z>=0) sum+=z;

17. Consider the following if else if statement. Rewrite it with switch command.

 if (a==1) 

cout << “One”; 

else if (a==0)

 cout << “Zero”;

 else 

cout << “Not a binary digit”;

Ans.
        
               switch (a) 
 { 
  case  1: cout <<  “One”; 
    break; 
  case   0: cout<< “Zero”; 
    break; 
  default: cout    <<  “ Not a binary digit”; 
 } 

 18. Write the importance of a loop control variable. Briefly explain the different parts of a loop.

19.1. Explain different types of decision statements in C++.

  2. Explain different types of iteration statements available in C++ with syntax and examples.

                 Iteration statements are used to perform repeated execution of a set of one or more statements in a program. They are also known as looping statements.
  A looping statement has four parts
a). initialization of control variable
b). test expression
c). update statement for control variable
d). body of the loop
eg.  for loop, while loop, do while loop

for loop:
 
      It is an entry – controlled loop in C++.


 Syntax:
  for (initialization ; test expression; update statement)
        {
 body of the loop;
         } 
 eg. 
 for  ( n=1;  n< = 10;  ++n)
  cout << n
  At first initialization takes place     n=1
Then test expression evaluated   n<=10
If it is true body of the loop executed, otherwise the program control goes out of the for loop.
After execution of loop body update expression is executed   ++n.  it will repeated until test expression become false.

     while loop:

          It is an entry – controlled loop in C++.
 Syntax
      Initialization of loop control variable
while ( test expression)
        {
 body of the loop;
 updating of loop control variable;
         } 
 eg. 
 n=1;
             while( n< = 10)

       {
  cout << n ;
    ++n;
                }
  At first initialization takes place     n=1
Then test expression evaluated   n<=10
If it is true body of the loop executed, otherwise the program control goes out of the while loop.
After execution of  update expression   ++n . repetition takes place   until test expression become false.

         do----while loop:

         It is an exit – controlled loop in C++.
 Syntax
      Initialization of loop control variable;
do
                   {
          body of the loop;
          updating of loop control variable;
                 }  ( test expression);
 eg. 
 n=1;
             do
                 {
  cout << n ;
    ++n;
                }   while(n<= 10);
  At first initialization takes place     n=1
Then test expression evaluated   n<=10

If it is true body of the loop executed, otherwise the program control goes out of the while loop.
After execution of  update expression   ++n . repetition takes place   until test expression become false.