PF Week 6
PF Week 6
INTRODUCTION
There are three major decision making structures. The ‘if’ statement, the ‘if-else’ statement, and the ‘switch’
statement. Another less commonly used structure is the conditional operator.
THE IF STATEMENT
The if statement enables you to test for a condition (such as whether two variables are equal) and branch to
different parts of your code, depending on the result or the conditions with relational and logical operators
are also included.
The simplest form of an ‘if’ statement is:
if (expression)
statement;
#include <iostream>
using namespace std;
int main()
{
int a;
cout << "Enter a number: ";
cin >> a; //get number
if (a == 10)
cout << "The number is equal to 10";
if (a > 10)
cout << "The number is greater than 10";
if (a < 10)
cout << "The number is less than 10";
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int age;
cout << "Enter age: "; cin >> age; //get number
return 0;
}
#include <iostream>
#include <conio.h> // for getche()
using namespace std;
int main()
{
int chcount=0; //counts non-space characters
int wdcount=1; //counts spaces between words
char ch = 'a'; //ensure it isn't '\r'
RAND() FUNCTION
The rand() function is used to generate random numbers. If we generate a sequence of random number with
rand() function, it will create the same sequence again and again every time program runs.
int rand(void):
Returns a pseudo-random number in the range of 0 to RAND_MAX. RAND_MAX: is a constant whose default
value may vary between implementations but it is granted to be at least 32767.
#include <iostream>
#include <conio.h>
#include <cstdlib> // for rand() function
#include <iomanip> // for setw()
#include <iostream>
#include <conio.h>
#include <cstdlib>
using namespace std;
int main() {
int a;
char ch;
do{
int x = (rand() % 5) + 1;
cout<<endl<<"Enter number between 1 to 5...";
cin>>a;
if (a==x)
cout<<"You guessed the right number";
else
cout<<"Sorry try again the number was "<< x;
cout<<endl<<"Do you want to run again?"
<<endl<<"Press y for yes...";
ch = getch();
}
while(ch=='y');
return 0;
}
#include <iostream>
using namespace std;
int main() {
int marks;
float per;
cout << " Enter marks out of (800)"; cin >> marks;
per = (float)marks/800 * 100;
if (per >= 80)
cout << "Percentage = " << per <<" \t A Grade :-D ";
else if (per >= 70)
cout << "Percentage = " << per <<"\t B+ Grade :-D ";
else if (per >= 60)
cout << "Percentage = " << per <<"\t B Grade :-) ";
else if (per >= 50)
cout << "Percentage = " << per <<"\t C Grade :-| ";
else if (per >= 40)
cout << "Percentage = " << per <<"\t D Grade :-| ";
else
cout << "Percentage = " << per <<"\t Fail :-( ";
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int a;
char ch;
do
{
cout<<"Enter any number from 1 to 10: "; cin>>a;
cout<<"The number is ";
switch(a)
{
case 1: cout<<"One"; break;
case 2: cout<<"Two"; break;
case 3: cout<<"Three"; break;
case 4: cout<<"Four"; break;
case 5: cout<<"Five"; break;
case 6: cout<<"Six"; break;
case 7: cout<<"Seven"; break;
case 8: cout<<"Eight"; break;
case 9: cout<<"Nine"; break;
case 10: cout<<"Ten"; break;
default: cout<<"Sorry cannot convert this number";
}
cout<<endl<<"Do U want to continue.... (Y / N)?"; cin>>ch;
}
while (ch =='y');
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int a, b;
char ch;
cout<<"Enter number operator number:";
cin>>a>>ch>>b;
switch(ch)
{
case '*':
cout<<"The answer is "<<a*b; break;
case '/':
if (b == 0)
{
cout<<"Cant divide with Zero"; break;
}
cout<<"The answer is "<<a/b; break;
case '-':
cout<<"The answer is "<<a-b; break;
case '+':
cout<<"The answer is "<<a+b; break;
default:
cout<<"Sorry wrong operator";
}
return 0;
}
Exercise 1:
Write a program to input day number (1-7) and print day of week name using switch case.
Input day number(1-7): 2
The day is Tuesday
Exercise 2:
Write a program to input month number and print total number of days in month using switch...case
Input month number(1-12): 2
Total number of days = 28
Exercise 3:
Write two programs to check whether an alphabet is vowel or consonant. First program by using
switch statement and second by using else-if structure.
Input character: a
'a' is a vowel
Input character: b
'b' is a consonant
Exercise 4:
Write a program which prints ASCII table from 129 to 255
(Use setw() for arrangement and make sure there should be only 8 ASCII numbers and characters on
each line. Take hint from program 5 of this handout)