0% found this document useful (0 votes)
9 views14 pages

C++ Loop and Control Structure Examples

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)
9 views14 pages

C++ Loop and Control Structure Examples

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

Lab==5

NAME: SHOAIB AHMED GHANGHRO

SECTION=C

CMS ID: 023-25-0185

EMAIL ADRESS: shoaibahmed.bscssaif25@[Link]

Task01 (OBSERVING FOR/WHILE/DO-WHILE BEHAVIOR) Write a


program to ask user input START/END and iterate loop from start to
end and perform following tasks:
a) Print numbers using FOR/WHILE/DO WHILE.
b) Test on following values and observe output (You may be asked
anytime during lab regarding this observation).

c) While testing on last two inputs, it may work abnormally. Now


modify above program so that it should work t time where t is
difference between start/end.
d) How for/while/do-while loop behaves?

Code
#include <iostream>
using namespace std;
int main(){

int nstart;
cout<<"enter starting value";
cin>>nstart;

int nend;
cout<<"enter ending value";
cin>>nend;

int i= nstart;

for(int i=nstart; i<=nend; i++){


cout<<i<<endl;
}

output

Using while loop


Code:
#include <iostream>
using namespace std;
int main(){
int nstart;
cout<<"enter starting value =";
cin>>nstart;

int nend;
cout<<"enter ending value=";
cin>>nend;
int i=nstart;
while(i<=nend){

cout<<i<<endl;
i++;
}

return 0;
}

Output:

Using while loop for descending order:


Code:
#include <iostream>
using namespace std;
int main(){
int nstart;
cout<<"enter starting value =";
cin>>nstart;

int nend;
cout<<"enter ending value=";
cin>>nend;
int i=nstart;

if(i <= nend ){


while(i<=nend){
cout<<i<<","<<endl;
i++;

}
//cout<<i<=end;
}
else{
while(i>=nend){

//cout<<i<<","<<endl;
cout<<i<<endl;
i--;
//cout<<i<<endl;
}
cout<<endl;
}
return 0;
}

Output:
Task02 (GRADE CALCULATION) Write a program to ask user input n_courses (Total
Courses), then iterate loop n_courses time and ask user marks in each course and perform
following tasks:
a) Calculate average of all courses marks.
b) Calculate total percentage of all courses.
c) Tell the user whether s/he is PASS/FAIL, FAIL  less than 60, and otherwise PASS.

Code:
#include <iostream>
using namespace std;
int main(){

int ncourses;
cout<<"enter courses=";
cin>>ncourses;

int i=0;
int sum=0;
int totalpercentage=0;
while(i<ncourses){
i++;
int marks,total;
cout<<"\n";
cout<<"enter marks of courses=";
cin>>total;
cout<<"enter obtained marks of course=";
cin>>marks;

float percentage=(marks*100)/total;
cout<<"percentage"<<percentage<<"%"<<endl;

sum=sum+marks;
totalpercentage=totalpercentage+percentage;

}
cout<<"\n";
cout<<"average marks"<<sum/ncourses<<endl;
cout<<"percentage"<<totalpercentage/ncourses<<endl;

if(totalpercentage>=60){
cout<<"status is : Pass=";

}
else{
cout<<"status is :Pass=";

return 0;
}

Output:
Task03 : Write a program to output the N terms of H
ARMONIC series and their SUM.:
Input:5
Output: 1/1 + 1/2 + 1/3 + 1/4 + 1/5
Total Sum is: 2.28333
Code:
#include<iostream>
using namespace std;
int main(){

int i=1;
int num;
cout<<"enter a num=";
cin>>num;
float harnomic_sum =0.0;

for(int i=1;i<=num;i++){

cout<<"1/"<<i;
if(i !=num){
cout<<"+";
}

harnomic_sum +=1.0/i;

}
cout<<endl;

cout << "Sum = " << harnomic_sum << endl;

return 0;
}

Output:

Task04: (CONTINUE/EXIT) Write a program to ask user NAME and print


“Welcome Dear NAME”, then ask user whether user wants to continue or
exit (y  continue, e  exit). Print appropriate message to tell user how to
EXIT/CONTINUE. Note: In following each task, you have to ask user
whether CONTINUE/EXIT, if continue then program should RUN AGAIN
otherwise TERMINATE.
Code:
#include <iostream>
using namespace std;

int main() {
string name;
char choice;

do {
cout << "Enter your name: ";
cin >> name;

cout << "Welcome Dear " << name << "!" << endl;

cout << "Continue or Exit? (y to continue / e to exit): ";


cin >> choice;

} while (choice == 'y' || choice == 'Y');

cout << "Program ended. Goodbye!" << endl;


return 0;
}

Output:

Task05: (PRIME NUMBERS) Write a program to ask user input N and


perform following tasks:
a) Check whether N is PRIME/NOT?
b) Generate first N PRIME numbers.
Note: PRIME numbers are those which are perfectly divided by 1 and
itself.

Code:
#include<iostream>
using namespace std;
int main(){
int n;
bool isprime=true;
cout<<"enter a prime number=";
cin>>n;

for(int i=2; i<n; i++){


if(n%i==0){
isprime = false;
break;
}
}
if (isprime)

cout<<n<<" is a prime number.";

else

cout<<"number is not prime";

return 0;
}

Output:

Task06 (FIBONACCI NUMBERS) Write a program to ask user input N and perform
following tasks:
a) Check whether N comes in FIBONACCI series or NOT?
b) Generate FIBONACCI series till N.
Note: FIBONACCI series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55…
Code:
#include <iostream>
using namespace std;
int main (){
int t1=0,t2=1,nextTerm=0;
int n;
cout<<"enter the number of a term=";
cin>>n;

cout<<"fubonacci series=";

for(int i=1;i<=n;i++){
if(i==1){

cout<<", "<< t1<<",";


continue;
}

if( i==2 ){
cout<<t2<<", ";
continue;
}
nextTerm=t1+t2;
t1=t2;
t2=nextTerm;

cout<<nextTerm<<", ";
}

return 0;
}

Output:
Task07 (SIMPLE PATTERNS) Write a program to ask user input N and design
following patters: Input N = 5 Output:

Code:
#include <iostream>
using namespace std;
int main (){

int i,j ,rows;


cout<<"\n enter number of rows=";
cin>>rows;
int n=1;

cout<<"\n";

for(i=0 ; i<=rows;i++)
{
for(j=0; j<=i;j++){
cout<<n<<" ";
n++;
}

cout<<" \n";

return 0;

Output:
Code :
For character:
#include <iostream>
using namespace std;
int main (){

int i,j ,rows;


cout<<"\n enter number of rows=";
cin>>rows;
int n=1;

cout<<"\n";

for(i=0 ; i<=rows;i++)
{
for(j=0; j<=i;j++){
cout<<n<<" ";
n++;
}

cout<<" \n";

return 0;
}
Output:

You might also like