2nd year Computer science Notes Chapter 4 Control Structures for kpk board.
KPK Grade 12 Computer Science Notes
Table of Contents
2nd year Computer science Notes Chapter 4 Control Structures
Q.4) Write a C++ program that takes two integers and characters representing one of the following mathematical operations: +,-,/, or *. Use a switch statement to perform the appropriate mathematical operation on the integers, and display the result. If an invalid operator enters then “Error” message should be displayed and the program should exit (use the exit() function).
Answer:
int main()
{
int num1, num2, result;
result = 0;
cout << “Enter First Integer : “;
cin >> num1;
cout << “Enter Second Integer : “;
cin >> num2;
cout << “Choose an from the following \n A-For Addtion \n S-For Subtraction \n D-For Division \n M-For Multiplication\n”;
char opeartion;
cout << “Enter a Character (A, S, D, M): “;
cin >> opeartion;
int i = 0;
switch (opeartion)
{
case ‘A’:
result = num1 + num2;
cout << “You Selected Addition \nThe Result is = ” << result << “\n\n” << endl;
break;
case ‘S’:
result = num1 – num2;
cout << “You Selected Subtraction \nThe Result is = ” << result << “\n\n” << endl;
break;
case ‘D’:
result = num1 / num2;
cout << “You Selected Division \nThe Result is = ” << result << “\n\n” << endl;
break;
case ‘M’:
result = num1 * num2;
cout << “You Selected Multiplication \nThe Result is = ” << result << “\n\n” << endl;
break;
default:
cout << “Error! operator is not correct”;
exit(0);
}
return 0;
}
Output:
The output of the program is as follows:

Q.5) Write a program using for loop that prints even numbers from 0 to 20.
Answer:
PROGRAM USING FOR LOOP THAT PRINTS EVEN NUMBERS FROM 0 T0 20:
#include <iostream>
using namespace std;
int main ()
{
int x;
for(x=0 ;x<=20; x=x+1)
if (x%2 ==0)
cout <<“This is an even number”<< x<< “.\n”;
}
Output:
The output of the program is as follows:

Q.6) Write a program using while loop that takes an integer for a variable nValue, and returns the sum of all the numbers from 1 to nValue.
Hint: For example, nValue=5 should return 15, which is 1+2+3+4+5
Answer:
#include <iostream>
using namespace std;
int main ()
{
int number, i =1, sum =0;
cout<<“Enter a positive integer”;
cin>> number;
while (i<= number)
{
sum +=i;
++i;
}
cout<<“Sum of ” << number <<“=”<< sum;
return 0;
}
Output:
The output of the program is as follows:

Q.7) What is wrong with the following for loop?
// Print all numbers from 9 to 0
for (unsigned int nCount = 9; nCount >=0; nCount –)
cout <<nCount <<” “;
Answer:
PROBLEM:
The problem in the loop is that an unsigned number is never negative.
Therefore, the loop-test nCount >= 0 will always be true. Thus you get an infinite loop.
The output of the infinite loop with the above mentioned code is as follows:

Q.8) Write a C++ program to read the address of a person and exit when the user enters dot (.) from the keyboard.
Answer:
#include <iostream>
using namespace std;
int main()
{
char ch;
cout << “Enter address : ” << endl;
ch = _getche();
while (ch != ‘.’)
{
ch = _getche();
}
cout << “\n\n\n”;
cout << “You pressed dot(.)” <<“\n\n\n”;
return 0;
}
Output:
The output of the above program is as follows:

Q.9) Write a C++ program to find out the area of a triangle and if any side is zero then display the message “There is no triangle”.
Answer:
#include<iostream>
using namespace std;
int main ()
{
int height, base;
float ans;
cout <<“Enter height and base:”;
cin>>height>> base;
ans=(0.5)*height*base;
if (height == 0 ||base==0)
cout<< “there is no triangle”;
else
cout<<“Area of triangle is :<<ans;
return 0;
}
Output:
The output of the above program is as follows:

Q.10) Write a C++ program to input a character from the keyboard and display the message after testing whether it is Vowel or Consonant.
Answer:
PROGRAM TO CHECK WHETHER ENTERED CHARACTER IS VOWEL OR CONSONANT:
#include<iostream>
using namespace std;
int main ()
{
char c;
int isLowercaseVowel , isUppercaseVowel;
cout<<“Enter an alphabet:”;
cin>> c;
isLowercaseVowel =( c== ‘a’ || c==’e’ || c==’i’ || c==’o’ ||c==’u’);
isUppercaseVowel =( c==’A’ || c==’E’ || c==’I’ || c== ‘O’ || c==’U’);
if (isLowercaseVowel || isUppercaseVowel)
cout<<c<< ” is a Vowel”;
else
cout<< c<< ” is a consonant”;
return 0;
}
Output:
The output of the above program is as follows:

Reated Post:
- 2nd-year computer science notes Chapter 1 Operating Systems
- 2nd-year computer science notes Chapter 2 System Development Life Cycle
- Chapter 3 Object-Oriented Programming using C++
What are the 3 types of control structures?
The flow of authority through any given part is implemented with 03 essential types of control systems:
Sequential: defaulting mode. …
Selection: used for decisions, branching — selecting between 2 or more alternative paths. …
Repetition: operated for looping, i.e. repeating a portion of code multiple times in a row.
Visit Now