0% found this document useful (0 votes)
12 views7 pages

Chapter4 BCP Lab ManualGuide

Uploaded by

Dejenie
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views7 pages

Chapter4 BCP Lab ManualGuide

Uploaded by

Dejenie
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

C++Programming

Flow Statement
========================if statement=================================================
// C++ Selection Statements - C++ if Statement //
#include<iostream>
#include<conio.h>
using namespace std;
int main()
int Value1;
int Value2;
cout << "Enter two integers: ";
cin >> Value1 >> Value2;
if (Value1 > Value2)
{
int RememberValue1 = Value1;
Value1 = Value2;
Value2 = RememberValue1;
}
cout << "The input in sorted order: "
<< Value1 << " " << Value2 << endl;
}
====================== if-else=====================================================
/* C++ Selection Statements - C++ if-else Statement * Example1/
#include<iostream>
#include<conio.h>
Using namespace std;
int main()
{
int num;
cout<<"Enter a number: ";
cin>>num;
if(num%2==0)
{ cout<<"You entered an even number"; }
else { cout<<"You entered an odd number"; }
return 0;
}

Exercise- Write a C++ program for finding maximum of two numbers


/* C++ Selection Statements - C++ if-else Statement * Example2/
#include<iostream>
#include<conio.h>
Using namespace std;
int main()
{
int num1, num2, max;
cout << "Enter two integers: ";
int Value1;
int Value2;
cin >> Value1 >> Value2;
int Max;
if (Value1 < Value2)
{
Max = Value2;
}
else
{
Max = Value1;
}
cout << "Maximum of inputs is: " << Max << endl;
}
===================================nested-if=========================================
Example1: /* C++ Selection Statements - C++ nested ifs Statement */
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int num;
cout<<"Enter a number: ";
cin>>num;
if(num>100)
{
if(num>500)
{
if(num>10000)
{
cout<<"Its too high..!!"; }
if(num<1000)
{
cout<<"Its medium..!!"; }
}
if(num<=500)
{
cout<<"Its low..!!"; }
}
if(num<=100)
{
cout<<"Its too low..!!"; }
return 0;
}
==================================if-else-if============================
// C++ Selection Statements - C++ if-else-if Ladder //
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
char ch;
float a, b, result;
cout<<"Enter any two number: ";
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;
cout<<"\n"<<"The calculated result is : "<<result<<"\n";
getch();
}

Exercise
// Program to check whether an integer is positive, negative or zero
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter an integer: ";
cin >> number;
if (number > 0) {
cout << "You entered a positive integer: " << number << endl;
}
else if (number < 0) {
cout << "You entered a negative integer: " << number << endl;
}
else {
cout << "You entered 0." << endl;
}
cout << "This line is always printed.";
return 0;
}

===========================switch statement=============================
// C++ Selection Statement - switch Statement //
#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
int dow;
cout<<"Enter number of week's day (1-7): ";
cin>>dow;
switch(dow)
{
case 1 : cout<<"\nSunday"; break;
case 2 : cout<<"\nMonday"; break;
case 3 : cout<<"\nTuesday"; break;
case 4 : cout<<"\nWednesday"; break;
case 5 : cout<<"\nThursday"; break;
case 6 : cout<<"\nFriday"; break;
case 7 : cout<<"\nSaturday"; break;
default : cout<<"\nWrong number of day";
break;
}
return 0;
}
Exercise:
// Program to build a simple calculator using switch Statement
#include <iostream>
using namespace std;
int main() {
char oper;
float num1, num2;
cout << "Enter an operator (+, -, *, /): ";
cin >> oper;
cout << "Enter two numbers: " << endl;
cin >> num1 >> num2;
switch (oper) {
case '+':
cout << num1 << " + " << num2 << " = " << num1 + num2;
break;
case '-':
cout << num1 << " - " << num2 << " = " << num1 - num2;
break;
case '*':
cout << num1 << " * " << num2 << " = " << num1 * num2;
break;
case '/':
cout << num1 << " / " << num2 << " = " << num1 / num2;
break;
default:
// operator is doesn't match any case constant (+, -, *, /)
cout << "Error! The operator is not correct";
break;
}
return 0;
}
=====================================for loop=================================
 /* C++ Iteration Statements - C++ for Loop */
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{ clrscr();
int i;
for(i=1; i<=10; i++)
{
cout<<i<<" ";
}
getch(); }
Exercise
// C++ program to find the sum of first n natural numbers
// positive integers such as 1,2,3,...n are known as natural numbers
#include <iostream>
using namespace std;
int main() {
int num, sum;
sum = 0;
cout << "Enter a positive integer: ";
cin >> num;
for (int i = 1; i <= num; ++i)
{
sum += i;
}
cout << "Sum = " << sum << endl;
return 0;
}
=====================================Nested for loop=================================
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int i, k;
for(i=1;i<=4;i++)
{
for(k=1;k<=i;k++)
{
cout<<"*";
}
cout << endl;
}
}
Exercise
Print out the following shapes
1 1
12 22 *
123 333 **
1234 4444 ***
****

=====================================while Loop============================
/* C++ Iteration Statements - C++ while Loop */
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
clrscr();
unsigned long num, fact=1;
cout<<"Enter a number: ";
cin>>num;
while(num)
{
fact = fact*num;
num--;
}
cout<<"The factorial of the number is "<<fact;
return 0;
}
============================ do-while===========================================
//C++ Iteration Statements - C++ do-while Loop
#include <iostream>
using namespace std;
int main() {
int i = 1;
do{
cout<<i<<"\n";
i++;
} while (i <= 10) ;
}

Exercise
Use do and do while for the following program.
// program to find the sum of positive numbers
// if the user enters a negative number, the loop ends
// the negative number entered is not added to the sum
#include <iostream>
using namespace std;
int main()
{
int number;
int sum = 0;
// take input from the user
cout << "Enter a number: ";
cin >> number;
while (number >= 0) {
// add all positive numbers
sum += number;
// take input again if the number is positive
cout << "Enter a number: ";
cin >> number;
}
// display the sum
cout << "\nThe sum is " << sum << endl;
return 0;
}
========================================= continue===================================
int a, b, c, i;
for(i=0; i<20; i++)
{
cout << "\n Enter 2 numbers" ;
cin >> a >> b ;
if(b == 0)
{
cout << "\n The denominator cannot be zero" << "Enter again !";
continue;
}
else c = a/b ;
cout << "\n Quotient =" << c << "\n" ;
}
=================================Break statement==================================
int a, b, c, i;
for(i=0; i<20; i++)
{
cout << "Enter 2 numbers" ;
cin >> a >> b ;
if(b == 0) break;
else c = a/b ;
cout << "\n Quotient =" << c << "\n" ;
}
===========================C++ break and continue Statement======================
/* C++ Jump Statements - C++ break and continue Statement */
#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
cout<<"The loop with \'break\' produces output as:\n";
for(int i=1; i<=10; i++)
{
if((i%3)==0) break;
else cout<<i<<endl;
}
cout<<"\nThe loop with \'continue\' produce output as:\n";
for(i=1; i<=10; i++)
{
if((i%3)==0)
continue;
else cout<<i<<endl;
}
getch(); }

You might also like