loop, logical operator &nested if-else
Objective: of this lab Manual is to get hands-on experience of For loop & logical operator &nested if-else in C++. Get familiar with the
syntax, Errors, and implementations.
Background: Loop structures called for loops and while loops are
covered as some basic control structures. The loop can be used to do repetitive
calculations. Though human beings can do calculations or processes or
repetitive nature by hand, it is much slower and tedious for the human being.
Good and solid logic of loop structures can be used to solve such problems in a
very efficient way. For example, in
order to compute the sum of 1 + 2 + 3 + … + 100, we can do easily using the
following code: int sum = 0; for (int j = 1; j <= 100; j++) sum += j; There are two control structures used
often: the for loop and the while loop (and do while as a different way of
while). The syntaxes of these two loops structures are as follows: for
(initialize loop variables; loop terminator; loop variable update) {
Statements in the block or one statement
} Not all three components of
the for loop are necessary, in fact for ( ; ; ) is a valid statement. The loop the variable update can be increments of loop variable(s) or decrements; there can
be more than one loop variable in the same for loop separated by comma (,) and
there can also be nested for loops of for loop inside for loop, etc. When there
is only one statement in the block, the enclosing braces{} can be omitted.
Pre-labs:
1. Study Deitel’s book (how to
program C++) control structures.
2. Explain which of the following
for loops are valid and which are invalid and why.
3. Convert the following for loop
to while loop.
4. Convert the following while loop
to for loop.
5. Analyze the following for loops
in the table and state whether
a. They compile.
b. They run
c. They print out a lined. They print out more than one
line Answer yes or no to each.
Assessment
tools:
The assessment is according to the student
participation in the lab if the student solves the exercise, he will get the
participation mark for this lab.
Lab Assignments:
Q1-
Write a program in C++. Take 10 integer numbers and print them on the console.
You can only use a For loop.
Q2.
Write a C++ Program to Check Whether a Character is Vowel or Consonant.
Q3-
Write a program in C++. Your program should print even and odd by checking the
input entered by the user for an instant if the user entered 2 it will print even if the user entered 3 it will pint odd.
Q4-
Write a program in C++. Your program should print the square of the first 20 digits you
can take input or hardcore the value to the integer.
Q5- Write
a program in C++. Your program should take input from the user (infinitely) it
will terminate when the user press 0 only.
Q6-
Write a program in C++. Your program should print the pattern shown below:
Output:
Output:
*****
*****
*****
*****
*****
**
**
***
****
*****
*****
*****
****
****
***
***
*****
*****
*****
*****
Q7- Write a program in C++. Your program should print the pattern shown below:
Output:
Output:
**
**
***
****
*****
Q8- Write a program in C++. Your program should print the pattern shown below:
Output:
Output:
*****
*****
****
****
***
***
for submission please click:
This comment has been removed by the author.
ReplyDeleteQ6
ReplyDelete#include
using namespace std;
void main()
{
int x=0;
int y=0;
while(x<5)
{
y=0;
while(y<5)
{
cout<<"*";
y++;
}
cout<<endl;
x++;
}
system("pause");
}
#include
ReplyDeleteusing namespace std;
void main()
{
int a,b;
a=1;
while(a<=5)
{
b=1;
while(b<=5)
{
cout<<"RANA\t\t\t";
b++;
}
cout<<endl<<endl<<endl<<endl;
a++;
}
system ("pause");
}
Aurang Zaib
405640
This comment has been removed by the author.
ReplyDelete#include
ReplyDeleteusing namespace std;
int main()
{
int c,b;
c=1;
while(c<=5)
{
b=1;
while(b<=5)
{
cout<<"*";
b++;
}
cout<<endl;
c++;
}
system("pause");
return 0;
}
Name:Muhammad Wasif Nazir
Class:BSCS section A1.
CMS NO.406084.
#include
ReplyDeleteusing namespace std;
void main()
{
int m,n;
m=1;
while(m<=5)
{
n=1;
while(n<=5)
{
cout<<"*\t";
n++;
}
cout<<endl;
m++;
}
system ("pause");
}
CMS number 406016
DeleteThis comment has been removed by the author.
ReplyDelete#include
ReplyDeleteusing namespace std;
int main()
{
int a =0;
int b =0;
while(a<5)
{
b=0;
while(b<5)
{
cout<<"*";
b++;
}
cout<<"*";
a++;
}
system ("pause");
return 0;
}
Name:Muhammad Mohid Mustafa
Cms no:406599
Section:A1
#include
ReplyDeleteusing namespace std;
int main()
{
int a,b;
a = 0;
b = 0;
while (a <= 5)
{
while (b <= 5)
{
cout << '*';
b++;
}
cout << endl;
b = 0;
a++;
}
getchar();
return 0;
}
#include
ReplyDeleteusing namespace std;
int main()
{
int a,b;
a = 0;
b = 0;
while (a <= 5)
{
while (b <= 5)
{
cout << '*';
b++;
}
cout << endl;
b = 0;
a++;
}
getchar();
return 0;
}
muhammad dawood 405909
#include
ReplyDeleteusing namespace std;
int main()
{
int a=0;
int b=0;
while(a<5)
{
b=0;
while(b<5)
{
cout<<"*"<<endl;
b++;
}
cout<<endl;
a++;
}
system("pause");
return 0;
}
Ijaz Ali shah
CMS : 407261
Section A1
#include
ReplyDeleteusing namespace std;
int main()
{
int i=1;
while(i<=5)
{cout<< "*****"<<endl;
i++;}
system("pause");
return 0;
}
This comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteSharafat ali
ReplyDelete406401
A2
27-nov-2019
#include
using namespace std;
void main()
{
int a=0;
while(a<=4)
{
cout<<"*****"<<endl;
a++;
}
system("pause");
}
Question # 6
ReplyDelete#include
using namespace std;
void main()
{
int a=0;
while(a<=4)
{
cout<<"*****"<<endl;
a++;
}
system("pause");
}
Muhammad Bilal
Section A2
CMS # 406670
#include
ReplyDeleteusing namespace std;
int main()
{
for(int a=1;a<=5;a++)
{
for(int b=1;b<=5;b++)
{
cout<<"*";
}
cout<<endl;
}
system("pause");
return 0;
}
Name: Abdullah Jaral
CMS: 406222
#include
ReplyDeleteusing namespace std;
void main()
{
for(int a=1;a<=5;a++)
{
for(int b=1;b<=5;b++)
{
cout<<"*";
}
cout<<endl;
}
system("pause");
}
name: M.Taha
cms: 406403
section: a2
Question No: 6
ReplyDelete#include
using namespace std;
int main()
{
for(int x = 1; x <= 5; x++)
{
for(int y = 1; y <= 5; y++)
{
cout << "*";
}
cout << endl;
}
system("pause");
return 0;
}
MUHAMMAD UMAIR ANJUM
CMS NO: 406266
#include
ReplyDeleteusing namespace std;
int main()
{
int a,b;
a = 0;
b = 0;
while (a <= 10)
{
while (b <= 10)
{
cout << '*';
b++;
}
cout << endl;
b = 0;
a++;
}
return 0;
}
sameer sohail
406423
This comment has been removed by the author.
ReplyDeleteSharafat ali
ReplyDelete406401
A2
Q no.6
#include
using namespace std;
void main()
{
int a=1;
while(a<=5)
{
a++;
cout<<"*****"<<endl;
}
system("pause");
}
Question # 7
ReplyDelete#include
using namespace std;
void main()
{
int a=1;
while(a<=2)
{
cout<<"**"<<endl;
a++;
}
for(int b=3;b<=5;b++)
{
int c=1;
while(c<=b)
{
cout<<"*";
c++;
}
cout<<endl;
}
system("pause");
}
Muhammad Bilal
Section A2
CMS # 406670
Question#6:
ReplyDelete#include
using namespace std;
int main()
{
for(int a=1; a<=5; a++)
{
for(int b=1; b<=5; b++)
{
cout<<"*";
}
cout<<endl;
}
return 0;
system("pause");
}
Madni Fareed
CMS# 406323
A2
Question#6:
ReplyDelete#include
using namespace std;
void main()
{
for(int row=5;row>0;row--)
{
for(int clm=5;clm>0;clm--)
{
cout<<"*";
}
cout<<"\n";
}
system("pause");
}
Sarmad hassan
CMS#406324
Section:A2
#include
ReplyDeleteusing namespace std;
void main()
{
for(int a=0;a<=4;a++)
{
for(int b=2;b<=a;b++)
{
cout<<"*";
}
cout<<"**";
cout<<endl;
}
system("pause");
}
name: M.Taha
cms: 406403
section: a2
#include
ReplyDeleteusing namespace std;
int main()
{
for(int a=1; a<=5; a++)
{
cout<<"*****"<<"/n";
}
return 0;
}
#include
using namespace std;
int main()
{
for(int a=1; a<=2; a++ )
{
cout<<"**"<<"\n";
}
for(int a=1; a<=1; a++)
{
cout<<"***"<<"\n";
}
for(int a=1; a<=1; a++)
{
cout<<"****"<<"\n";
}
for(int a=1; a<=1; a++)
{
cout<<"*****"<<"\n";
}
return 0;
}
#include
using namespace std;
int main()
{
for(int a=1; a<=2; a++ )
{
cout<<"*****"<<"\n";
}
for(int a=1; a<=2; a++)
{
cout<<"****"<<"\n";
}
for(int a=1; a<=2; a++)
{
cout<<"***"<<"\n";
}
return 0;
}
M. Ahmad saleem
cms=406242
Sharafat ali
ReplyDelete406401
A2
Q n.7
#include
using namespace std;
void main()
{
int a=1;
while(a<=2)
{
cout<<"**"<<endl;
a++;
}
for(int b=3;b<=5;b++)
{
for(int c=1;c<=b;c++)
{
cout<<"*";
}
cout<<endl;
}
system("pause");
}
Saqib Aleem
ReplyDelete406204 A2
#include
using namespace std;
void main()
{
int x=0;
int y=0;
while(x<5)
{
y=0;
while(y<5)
{
cout<<"*";
y++;
}
cout<<endl;
x++;
}
system("pause");
}
Question# 7:
ReplyDelete#include
using namespace std;
int main()
{
cout<<"*";
for(int a=1; a<=5; a++)
{
for(int b=1; b<=a; b++)
{
cout<<"*";
}
cout<<endl;
}
return 0;
system("pause");
}
Madni Fareed
CMS# 406323
A2
Q#8 #include
ReplyDeleteusing namespace std;
void main()
{
for(int a=5;a<=5;a++)
{
for(int b=5;b<=5;b++)
{
cout<<"*****"<<endl;
cout<<"*****"<<endl;
cout<<"****"<<endl;
cout<<"****"<<endl;
cout<<"***"<<endl;
cout<<"***"<<endl;
}
cout<<endl;
}
system("pause");
}
name: M.Taha
cms: 406403
section: a2
QUESTION NO: 7
ReplyDelete#include
using namespace std;
void main()
{
for (int x =1; x <=2; x++)
{
cout<<"**"<<endl;
}
for(int y=3; y<=5 ; y++)
{
for (int z =1; z <=y; z++)
{
cout<<"*";
}
cout<<endl;
}
system("pause");
}
MUHAMMAD UMAIR ANJUM
CMS NO: 406266
Q:7
ReplyDelete#include
using namespace std;
int main()
{
for(int a=2;a<=2;a++)
{
for(int b=2;b<=2;b++)
{
cout<<"**"<<endl;
cout<<"**"<<endl;
cout<<"***"<<endl;
cout<<"****"<<endl;
cout<<"*****"<<endl;
}
cout<<endl;
}
system("pause");
return 0;
}
Name: Abdullah Jaral
CMS:406222
#include
ReplyDeleteusing namespace std;
void main()
{
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
cout<<"*****"<<endl;
}
}
system("pause");
}
NAME:SHEHERYAAER
CMS:406231
#include
ReplyDeleteusing namespace std;
void main()
{
for(int i=5;i<=5;i++)
{
for(int j=5;j<=5;j++)
{
cout<<"*****"<<endl;
cout<<"*****"<<endl;
cout<<"****"<<endl;
cout<<"****"<<endl;
cout<<"***"<<endl;
cout<<"***"<<endl;
}
}
system("pause");
}
NAME:SHEHERYAAR
CMS:406231
#include
ReplyDeleteusing namespace std;
int main()
{
int a,b, c;
for (a=1;a<=10;a++)
{
for(b=10;b>=a;b--)
{
cout<<" ";
}
for (c=5;c<=b;c++)
{
cout<<"*";
cout<<" ";
}
cout<<endl;
}
system("pause");
}
CMS number 406016
Delete#include
ReplyDeleteusing namespace std;
int main()
{
int a,b, c;
for (a=1;a<=10;a++)
{
for(b=10;b>=a;b--)
{
cout<<" ";
}
for (c=5;c<=b;c++)
{
cout<<"*";
cout<<" ";
}
cout<<endl;
}
system("pause");
}
Aurang Zaib
405640
#include
ReplyDeleteusing namespace std;
int main()
{
int s,c,i;
for(s=1;s<=10;s++)
{
for(c=10;c>=s;c--)
{
cout<<" ";
}
for(i=1;i<=c;i++)
{
cout<<"*";
cout<<" ";
}
cout<<endl;
}
system("pause");
return 0;
}
Name:Muhammad wasif nazir.
Class:BSCS section A1.
CMS NO.406084.
CMS 405646
ReplyDelete#include
using namespace std;
int main()
{
int a,b,c;
for(a=1;a<=10; a++)
{
for(b=10; b>=a; b--)
{
cout<<" ";
}
for(c=1;c<=b;c++)
{
cout<<"*";
cout<<" ";
}
cout<<endl;
}
system("pause");
return 0;
}
Huzaifa khalid
ReplyDeleteCMS 406172
#include
using namespace std;
int main()
{
int i=1;
while(i<=5)
{cout<< "*****"<<endl;
i++;}
system("pause");
return 0;
}
Huzaifa khalid
ReplyDeleteCMS 406172
BsCS section A(A2)
#include
using namespace std;
int main()
{
int a, b, space;
for(a = 2; a <= 9; a++)
{
for(space = a; space < 8; space++)
{
cout << " ";
}
for(b = 1; b <= (2 * a - 2); b++)
{
cout << "*";
}
cout << "\n";
}
system("pause");
return 0;
}
#include
ReplyDeleteusing namespace std;
int main ()
{
int a,b,r=4,s;
for(a=1;a<=r;a++)
{
for(s=a;s<r;s++)
{
cout<<" ";
}
for(b=1;b<=(a*2-1);b++)
{
cout<<"*";
}
cout<<endl;
}
system("pause");
return 0;
}
Muhammad Bilal
Section A2
CMS # 406670
Sheheryaar
ReplyDeleteCMS 406231(A2)
#include
using namespace std;
int main()
{
int a, b, space;
for(a = 2; a <= 9; a++)
{
for(space = a; space < 8; space++)
{
cout << " ";
}
for(b = 1; b <= (2 * a - 2); b++)
{
cout << "*";
}
cout << "\n";
}
system("pause");
return 0;
}
#include
ReplyDeleteusing namespace std;
void main ()
{
int a,b,r=8,s;
for(a=2;a<=8;a=a=a+2)
{
for(s=a;s<r;s++)
{
cout<<" ";
}
for(b=1;b<=(a*2-1);b++)
{
cout<<"*";
}
cout<<endl;
}
system("pause");
}
Sharafat ali
406401
A-2
#include
ReplyDeleteusing namespace std;
int main()
{
int j,x;
for(j=1;j<=5;j++)
{
for(x=1;x<=5;x++)
{
cout<<"*";
}
cout<<endl;
}
system("pause");
return 0;
}
405646
#include
ReplyDeleteusing namespace std;
int main()
{
cout<<"**"<<endl;
cout<<"**"<<endl;
cout<<"***"<<endl;
cout<<"****"<<endl;
cout<<"*****"<<endl;
system("pause");
return 0;
465646
#include
ReplyDeleteusing namespace std;
int main()
{
cout<<"*****"<<endl;
cout<<"*****"<<endl;
cout<<"****"<<endl;
cout<<"***"<<endl;
cout<<"***"<<endl;
system("pause");
return 0;
}405646
#include
ReplyDeleteusing namespace std;
int main()
{
cout<<"*"<<endl;
cout<<"***"<<endl;
cout<<"*****"<<endl;
cout<<"*******"<<endl;
system("pause");
return 0;
}
Name:MuhammadMohidMustafa
Cms no:406599
Section:A1
#include
ReplyDeleteusing namespace std;
void main()
{
for(int row=1;row<=5;row++)
{
for(int sp=row;sp<5;sp++)
{
cout << " ";
}
for(int clm=1;clm<=row;clm++)
{
cout << "**";
}
cout<<endl;
}
system("pause");
}
Sarmad hassan
406324
Section:A2
Ahmad Muneeb
ReplyDelete407203 A2
Class Task:
#include
using namespace std;
int main()
{
for(int a=1;a<=4;a++)
{
for(int b=3;b>=a;b--)
{
cout<<" ";
}
for(int c=1;c<=a;c++)
{
cout<<"*";
}
for(int d=1;d<=a;d++)
{
cout<<"*";
}
cout<<endl;
}
system("pause");
return 0;
}
#include
ReplyDeleteusing namespace std;
int main()
{
for(int i=1; i<=4; i++)//1
{
for(int j=4; j>=i; j--)//4
{
cout<<" ";
}
for(int k=1; k<=i; k++)
{
cout<<"*";
}
for(int l=1; l<=i; l++)
{
cout<<"*";
}
cout<<endl;
}
system("pause");
return 0;
}
M.Umar
CMS# 407278
A2
#include
ReplyDeleteusing namespace std;
void main()
{
for(int a=1;a<=4;a++)
{
for(int b=4;b>=a;b--)
{
cout<<" ";
}
for(int c=1;c<=a;c++)
{
cout<<"*";
}
for(int d=1;d<=a;d++)
{
cout<<"*";
}
cout<<endl;
}
system("pause");
}
name: M.Taha
cms: 406403
section: A2
hamza 407313
ReplyDeleteQ #1
#include
using namespace std;
int main()
{
int a;
{
for(a=1;a<=10;a++)
cout<<a<<"\n";
}
}
hamza 407313
ReplyDeleteques#3
#include
using namespace std;
int main()
{
int a;
cout<<"Enter the Value for the Number:";
cin>>a;
if(a%2==0)
cout<<"Number is Even";
else
cout<<"Number is Odd";
}
Post a Comment