Conditions and Loops Task.1713966862461
Conditions and Loops Task.1713966862461
✓ 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 a,b,c; }
if(a<b) }
if(a<c) return 0;
}
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)";
{ break;
default:
case 'A':
cout<<"Invalid grade";
cout<<"Your marks are greater than or equal to 90
(marks>=90)"; }
break; return 0;
case 'B’: }
#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!