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

Conditions and Loops Task.1713966862461

The document outlines a lab task for a programming course, focusing on conditional structures and loops in C++. It includes multiple example programs demonstrating the use of if statements, if-else conditions, nested ifs, switch statements, while loops, do-while loops, for loops, and nested loops. Each program is accompanied by code snippets illustrating the implementation of the concepts discussed.

Uploaded by

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

Conditions and Loops Task.1713966862461

The document outlines a lab task for a programming course, focusing on conditional structures and loops in C++. It includes multiple example programs demonstrating the use of if statements, if-else conditions, nested ifs, switch statements, while loops, do-while loops, for loops, and nested loops. Each program is accompanied by code snippets illustrating the implementation of the concepts discussed.

Uploaded by

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

LAB TASK

✓ Orwa Wajid
✓ BSCS (Section F)
✓ Roll # 27848
✓ 2nd Semester
✓ Object Oriented
Programming (OOP)
[Conditions and Loops]
✓ Mam Hafiza Sofia
Points to be focused in programs:
• Using five conditional structures in programs:
• Using if statement in programs.
• Using if else condition in programs.
• Using if else if condition in programs.
• Using nested if in programs.
• Using switch statement in programs.
• Using loops in programs.
• Using while loop.
• Using do-while loop.
• Using for loop.
• Using nested loop.
PROGRAM 1
• Write a program that inputs marks and
display "passed if the marks are 50 or more. IF STATEMENT:
#include<iostream>
using namespace std;
int main()
{
int marks;
cout<<"Enter the marks ";
cin>>marks;
if(marks>=50)
cout<<"Passed";
return 0;
}
PROGRAM 2
• This program inputs five numbers from if(c<min) min=c;
user and determine the largest and if(d<min) min=d;
smallest value between them using if if(e<min) min=e;
statement. cout<<"Smallest number is
#include<iostream> "<<min<<endl;
using namespace std;
int main() if(b>max) max=b;
{ if(c>max) max=c;
int a,b,c,d,e; if(d>max) max=d;
int max; if(e>max) max=e;
int min;
cout<<"Largest number is
cout<<"Enter five integers:"; "<<max<<endl;
cin>>a>>b>>c>>d>>e; return 0;
min=max=a;
}
if(b<min) min=b;
PROGRAM 3
• Write a program that inputs two numbers
and finds whether both are equal.
#include<iostream>
using namespace std;
int main()
{
int a,b;
cout<<"Enter first number ";
cin>>a;
cout<<"Enter second number ";
cin>>b;
if(a==b)
cout<<"Both numbers are equal";
return 0;
}
PROGRAM 1
• Write a program that inputs two integers, and
finds if first integer is the multiple of second integer.
If else STATEMENT:
#include<iostream>
using namespace std;
int main()
{
int a,b;
cout<<"Enter the first integer";
cin>>a;
cout<<"Enter the second integer";
cin>>b;
if( a%b==0)
cout<<"the first integer is the multiple of second
integer";
else
cout<<"the first integer is not a multiple of second
integer";
return 0; }
PROGRAM 2
• Write a program that inputs two numbers
and check whether the equal or not.
#include<iostream>
using namespace std;
int main()
{
int a;
int b;
cout<<"Enter first number";
cin>>a;
cout<<"Enter second number";
cin>>b;
if(a==b)
cout<<"Yes,Numbers are equal";
else
cout<<"Numbers are not equal";
return 0; }
}
PROGRAM 3
• Write a program that inputs two numbers
and check whether the it is even or odd.
#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter the number";
cin>>n;
if(n%2==0)
cout<<n<<" is an even number";
else
cout<<n<<" is an odd number";
return 0;
}
PROGRAM 1
• Write a program that inputs marks of student
and display it grades by if else if. If else if STATEMENT:
#include<iostream>
using namespace std;
int main()

{
int marks;
cout<<"Enter the obtained marks";
cin>>marks;
if(marks>=90)
cout<<"Your grade is A";
else if(marks>=80)
cout<<"Your grade is B";
else if(marks>=70)
cout<<"Your grade is C";
else if(marks>=60)
cout<<"Your grade is D";
else
cout<<"Your grade is F";
return 0; }
PROGRAM 2
• Write a program which displays days of
week when user enters the number of
day b/w 1 to 7. else if(day==4)
#include<iostream> cout<<"Thursday";
using namespace std; else if(day==5)
int main() cout<<"Friday";
{ else if(day==6)
int day; cout<<"Saturday";
cout<<"Enter the number of day "; else if(day==7)
cin>>day;
cout<<"Sunday";
if(day==1)
else
cout<<"Monday";
cout<<"Invalid number";
else if(day==2)
return 0;
cout<<"Tuesday";
else if(day==3)
cout<<"Wednesday";
PROGRAM 3
• Write a program which asks the user, his age
and show the corresponding statement.
#include<iostream>
using namespace std;
int main()
{
int age;
cout<<"How old are you?";
cin>>age;
if(age<0)
cout<<"Invalid age entered.
else if(age<18)
cout<<"You are minor";
else if(age>=18)
cout<<"You are an adult";
else
cout<<"You are a senior citizen";
return 0;
}
Nested if STATEMENT:
PROGRAM 1
• This program inputs three numbers from user
and distinguish the smallest between them.
#include<iostream>
using namespace std; else

int main() if(b<c)

{ {cout<<b<<" is smallest number";

int a,b,c; }

cout<<"Enter three numbers:"; else

cin>>a>>b>>c; {cout<<c<<" is smallest number";

if(a<b) }

if(a<c) return 0;

{cout<<a<<" is smallest number"; }

}
else
{cout<<c<<" is smallest number";
}
PROGRAM 2
• Write a program which asks the user, his age and
show the corresponding statement.
#include<iostream>
using namespace std;
int main()
{
int a,b,c;
cout<<"Enter three numbers:";
cin>>a>>b>>c;
if(a==b)
if(b==c)
{cout<<"All numbers are equal";
}
else
{cout<<"Numbers are different";
}
else
cout<<"Numbers are different";
return 0; }
PROGRAM 3
• Write a program that takes two integers as
input and checks whether they are both even,
both odd, or one even and display the result.
else
int num1,num2;
if (num2 % 2 == 0)
cout<<"Enter two numbers";
cout << "The first number is odd, but the
cin>>num1>>num2; second number is even." << endl;
if (num1 % 2 == 0) else
if(num2 % 2 == 0) cout << "Both numbers are odd." << endl;
cout << "Both numbers are even." << endl; return 0;
else }
cout << "The first number is even, but the second
number is odd." << endl;
PROGRAM 1 Switch STATEMENT:
• Write a program which asks the user, his age
and show the corresponding statement.

case 4:
#include<iostream>
cout<<"Thursday";
using namespace std;
break;
int main()
case 5:
{
cout<<"Friday";
int n;
break;
cout<<"Enter the number of day ";
case 6:
cin>>n;
cout<<"Saturday";
switch (n)
break;
case 1:
case 7:
cout<<"Monday";
cout<<"Sunday";
break;
break;
case 2:
default:
cout<<"Tuesday";
cout<<"Invalid number of day";
break;
case 3:
return 0;
cout<<"Wednesday";
}
break;
PROGRAM 2
• Write a program which asks the user, his age
and show the corresponding statement.
#include<iostream> case 'C':
using namespace std; cout<<"Your marks are greater than or equal to 70 (marks>=70)";
int main () break;
{ case 'D':
char grades; cout<<"Your marks are greater than or equal to 60 (marks>=60)";

cout<<"Enter the obtained grades "; break;

cin>>grades; case 'F':

switch(grades) cout<<"You are fail";

{ break;
default:
case 'A':
cout<<"Invalid grade";
cout<<"Your marks are greater than or equal to 90
(marks>=90)"; }

break; return 0;

case 'B’: }

cout<<"Your marks are greater than or equal to 80 (marks>=80)";


break;
PROGRAM 1 WHILE LOOP:
• This program displays the numbers from 1 to 20
and their sum on screen using while loop.
#include<iostream>
using namespace std;
int main() cout<<c<<endl;
{ sum=sum+c;
int c; c=c+1;
int sum;
c=1; }
sum=0; cout<<"Sum is "<<sum;
while(c<=20) return 0;
{
cout<<c<<endl;
PROGRAM 2
• Write a program that displays the sum
of following series using while loop
1+1/3+1/6+.........+1/999.
#include<iostream>
using namespace std;
int main()
{
float c,r;
c=3.0;
r=1.0;
while(c<=999)
{
r=r+1.0/c;
c=c+3; }
cout<<"Result is "<<r;
return 0; }
PROGRAM 3
• Write a program that inputs starting and while(n<=e)
ending number from user and then {
displays all odd numbers in the given if(!n%2==0)
range by while loop.
cout<<n<<endl;
#include<iostream>
n++;
using namespace std;
}
int main()
return 0;
{
}
int n,s,e;
cout<<"Enter starting number ";
cin>>s;
cout<<"Enter ending number ";
cin>>e;
n=s;
PROGRAM 1
• This program displays the statement using while
loop under certain condition. DO-WHILE LOOP:
#include<iostream>
using namespace std;
int main()
{
int n=1;
do
{
cout<<"Welcome to C++"<<endl;
n++;
}
while (n<=5);
return 0;
}
PROGRAM 2
• This program inputs the starting and
{
ending number and displays odd
if(c%2!=0)
numbers between them.
cout<<c<<endl;
#include<iostream>
c++;
using namespace std;
}
int main()
while(c<=e);
{
return 0;
int c,s,e;
}
cout<<"Enter starting number ";
cin>>s;
cout<<"Enter ending number ";
cin>>e;
c=s;
do
PROGRAM 3
• This program inputs the starting and
{
ending number and displays all even
if(c%2==0)
numbers between them.
cout<<c<<endl;
#include<iostream>
c++;
using namespace std;
}
int main()
while(c<=e);
{
return 0;
int c,s,e;
}
cout<<"Enter starting number ";
cin>>s;
cout<<"Enter ending number ";
cin>>e;
c=s;
do
PROGRAM 1

• Write a program that displays counting


from 1 to 5 using for loop. FOR LOOP:
#include<iostream>
using namespace std;
int main()
{
int i;
for ( i=1;i<=5; i++)
cout<<i<<endl;
return 0;
}
PROGRAM 2
• This program takes the integer number for(c=1; c<=len; c++)
from the user and show its table
according to the user provided length. {

#include<iostream>
cout<<tab<<"*"<<c<<"="<<tab*
using namespace std; c<<endl;
int main() }
{ return 0;
int tab,len,c;
cout<<"Enter the number for table }
";
cin>>tab;
cout<<"Enter the length of table ";
cin>>len;
PROGRAM 3
• Write a program to print the following
series:
1 4 7 10 ....40 . cout<<a<<" ";
#include<iostream> a=a+incre;
using namespace std; }
int main () }
{
int a=10; int incre=3; int i;
cout<<"The series is as
follows:"<<endl;
for(i=1;a<=40;i++)
{
PROGRAM 1
• Write a program that displays the following block
using nested for loop. NESTED FOR LOOP:
#include<iostream>
***
using namespace std; ***
int main() ***
{ ***
int m,n;
for(m=1;m<=4;m++)
{
for(n=1;n<=3;n++)
{
cout<<"*";
}
cout<<endl;
}
return 0; }
PROGRAM 2
• Write a program that displays the following
shape using nested for loop.
*
#include<iostream>
**
using namespace std;
***
int main()
****
{
*****
int m,n;
for(m=1;m<=5;m++)
{
for(n=1;n<=m;n++)
cout<<"*";
cout<<endl;
}
return 0; }
PROGRAM 3
• Write a program that displays the following
shape using nested for loop.
*****
#include<iostream>
****
using namespace std;
***
int main()
**
{
*
int m,n;
for(m=1;m<=5;m++)
{
for(n=1;n<=6-m;n++)
cout<<"*";
cout<<endl;
}
return 0; }
THANK YOU!

You might also like