CPP - Programming Construct

 C Language

Control statement




Fill in the blanks

1.  How many control statement presents in c   3

2.  While is   Entry controlled loop

3.  Do while is Exit controlled loop

4.  Write a expansion for iostream.h  input output stream

5.  Write a expansion for conio.h  console input output

6.  Write a syntax for for loop   for(initialization;condition checking;inc/dec)

7.  Getch presents in which header file  conio.h

8.  cout presents in conio.h.[T/F]    False

9.  Clrscr presents in stdio.h  [T/F]    False

10.       What is the output for below condition

   for(int i=0;i<=8;i++)

{

if(i==4)

continue;

}

Output   :    1235678

Answer the following Questions

                                                            (4 Marks)

 

1.Difference between while and do-while

 

While Loop

Do-While Loop

Entry Controlled Loop

Exit Controlled Loop

While loop is pretested loop

Do-while is post tested loop

Syntax

while(condition){

Statement;

}

Syntax

do{

statement;

}while(condition);

#include<iostream.h>

#include<conio.h>

void main(){

int a=5;

clrscr();

while(a<5){

cout<<a;

a++;

}

getch();

}

#include<iostream.h>

#include<conio.h>

void main(){

int a=5;

clrscr();

do{

cout<<a;

a++;

} while(a<5);

getch();

}

 

2.Write a program for leap year and not leap year

#include<iostream.h>

#include<conio.h>

void main()

{

int  a;

clrscr();

cout<<”enter any year”;

cin>>a;

if(a%4==0){

cout<<“Leap year”;

}

else{

cout<<”Not Leap Year”;

}

getch();

}

3.Write a program for age verification Vote eligibility test program in CPP

#include<iostream.h>

#include<conio.h>

void main()

{

int  a;

clrscr();

cout<<”enter your age”;

cin>>a;

if(a>=18){

cout<<“Eligible for vote”;

}

else{

cout<<”Not eligible for vote”;

}

getch();

}

4.Write a program to print pass or fail using if else

#include<iostream.h>

#include<conio.h>

void main()

{

int  a;

clrscr();

cout<<”enter your mark”;

cin>>a;

if(a>=50){

cout<<“pass”;

}

else{

cout<<”fail”;

}

getch();

}

5.To print 1 to 100 using for loop

#include<stdio.h>

#include<conio.h>

main()

{

int i;

clrscr();

for(i=1;i<=100;i++){

cout<<i<<endl;

}

getch();

}

6.Difference between break and continue


  Break

#include <iostream.h>  

#include <conio.h>  

void main() {  

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

        {    

              if (i == 5)    

              {    

                  break;    

              }    

        cout<<i<<"\n";    

          }    

getch();

}  

 

Continue

#include <iostream.h>  

#include <conio.h>  

void main() {  

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

          {    

              if (i == 5)    

              {    

                  continue;    

              }    

        cout<<i<<"\n";    

          }    

getch();

}  

7.Write a Program to to build a simple calculator using switch Statement

#include <iostream.h>

#include<conio.h>

void main() {

    char oper;

    float a, b;

    cout << "Enter an operator (+, -, *, /): ";

    cin >> oper;

    cout << "Enter two numbers: " << endl;

    cin >> a >> b;

 switch (oper) {

 case '+':

 cout << a << " + " << b << " = " << a + b;

break;

 case '-':

cout << a << " - " << b << " = " << a - b;

break;

case '*':

cout << a << " * " << b << " = " << a * b;

break;

 case '/':

cout << a << " / " << b << " = " << a / b;

break;

default:

cout << "Error! The operator is not correct";

 break;

    }

getch();

}

8.To print Vowels and Consonent using Switch case

#include <iostream.h>

#include<conio.h>

void main() {

    char a;

    cout << "Enter any character";

    cin >> a;

 switch (a) {

 case 'a':

case 'e':

case 'i':

case 'o':

case 'u':

case 'A':

case 'E':

case 'I':

case 'O':

case 'U':

cout<<“Vowel”;

break;

default:

cout << "Consonent";

 break;

    }

getch();

}

 

 

Answer the following Questions

                                                            (10 Marks)

 

1.Write a program to print student marklist with grade system using Nested if

#include <iostream.h>

#include<conio.h>

void main() {

int m1=60,m2=90,m3=79,m4=69,m5=95,total,average;

total=m1+m2+m3+m4+m5;

average=total/5;

 if(average>=50)

{

if(average>=80)

{

cout<<”A Grade”;

}

else{

cout<<”B Grade”;

}

}

else{

if(average>=30){

cout<<”C Grade”;

}

else{

cout<<”Fail”;

}

getch();

}

2. Explain the below control statements program

*While

*do while

* if

*Break

*continue



 

While Loop

Do-While Loop

Entry Controlled Loop

Exit Controlled Loop

While loop is pretested loop

Do-while is post tested loop

Syntax

while(condition){

Statement;

}

Syntax

do{

statement;

}while(condition);

#include<iostream.h>

#include<conio.h>

void main(){

int a=5;

clrscr();

while(a<5){

cout<<a;

a++;

}

getch();

}

#include<iostream.h>

#include<conio.h>

void main(){

int a=5;

clrscr();

do{

cout<<a;

a++;

} while(a<5);

getch();

}

 

IF Statement

#include<iostream.h>

#include<conio.h>

void main()

{

int  age;

clrscr();

cout<<"enter your age"

cin>>age;

if(age>=18)

{

cout<<"eligible for vote";

}

if(age<18)

{

cout<<"Not eligible for vote";

}

getch();

}


Break

 

#include <iostream.h>  

#include <conio.h>  

void main() {  

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

        {    

              if (i == 5)    

              {    

                  break;    

              }    

        cout<<i<<"\n";    

          }    

getch();

}  

 

Continue

#include <iostream.h>  

#include <conio.h>  

void main() {  

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

          {    

              if (i == 5)    

              {    

                  continue;    

              }    

        cout<<i<<"\n";    

          }    

getch();

}  

 


No comments:

Post a Comment