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

Lab 6

The document provides instructions for an experiment on loops in C++ programming. It includes examples of programs to print numbers from 1-10 using a for loop, print numbers from 10-1 using a for loop, calculate the sum and average of numbers from 1-10 using for loops, calculate the sum of odd numbers from 1-10 using a for loop, and print a range of numbers based on user input using a for loop.
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

Lab 6

The document provides instructions for an experiment on loops in C++ programming. It includes examples of programs to print numbers from 1-10 using a for loop, print numbers from 10-1 using a for loop, calculate the sum and average of numbers from 1-10 using for loops, calculate the sum of odd numbers from 1-10 using a for loop, and print a range of numbers based on user input using a for loop.
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

Introduction to Programming C++ – EMIS2005

Semester 1 2022/2023

CRN: 1202

Laboratory Experiment No.6

Loops

Student Names and IDs:

Name ID Total Grade

1 Khaleifah Saif Alabdouli A00061336

Report Grade: ------------------

Objective

On completion of this experiment, the student will be able to write compile and run a C++
program

Introduction

C++ is a powerful general-purpose programming language. It can be used to develop operating


systems, browsers, games, and so on. C++ supports different ways of programming like
procedural, object-oriented, functional, and so on. This makes C++ powerful as well as flexible.
Procedure

 Write a C++ program that prints the number from 1-10 using for loop
#include <iostream>//this code allow data to print on screen
using namespace std;//to be defind cin and cout
int main()//function main begins program
{
int x;
cout<<"the numbers from 1 to 10 are: "<<endl;
for(x=1;x<=10;x++)//for loop (start loop;endloop;increase)
{
cout<< x<<endl; //print this loop
}
return 0;// the progam end function successfully
}//end main function
 Write a C++ program that prints the number from 10-1 using for loop
#include <iostream>//this code allow data to print on screen
using namespace std;//to be defind cin and cout
int main()//function main begins program
{
int x;
cout<<"the numbers from 10 to 1 are: "<<endl;
for(x=10;x>=1;x--)//for loop (start loop;endloop;decrease)
{
cout<< x<<endl; //print this loop
}
return 0;// the progam end function successfully
}//end main function
 Write a C++ program that prints the sum numbers from 1-10 using for loop
#include <iostream>//this code allow data to print on screen
using namespace std;//to be defind cin and cout
int main()//function main begins program
{
int x,sum;

for(x=1;x<=10;x++)//for loop (start loop;endloop;increase)


sum=sum+x;
cout<<"the sum of this number is: "<< sum<<endl; //print this loop

return 0;// the progam end function successfully


}//end main function
 Write a C++ program that prints the average of numbers from 1-10
#include <iostream>//this code allow data to print on screen
using namespace std;//to be defind cin and cout
int main()//function main begins program
{
int x=0;

double sum=0, avg=0;

for(x=1;x<=10;x++)//for loop (start loop;endloop;increase)

avg=(sum+=x)/x; //avg

cout<<"the avg of this number is: "<<avg <<endl; //print this loop

return 0;// the progam end function successfully


}//end main function
 Write a C++ program that prints the sum of odd numbers from 1-10
#include <iostream>//this code allow data to print on screen
using namespace std;//to be defind cin and cout
int main()//function main begins program
{
int x=0;

double sum=0, avg=0;

for(x=1;x<=10;x++)//for loop (start loop;endloop;increase)

{
if(x%2!=0)// to get odd numbers
{
sum=sum+x;
}
}

cout<<"the sum of odd number between 1 to 10: "<<sum <<endl; //print this
loop

return 0;// the progam end function successfully


}//end main function
 Write a C++ program that prints the numbers between a specific range entered by
the user using loops. For example, if the user enters 3 and 7, your program should
print 3,4,5,6,7.
#include <iostream>//this code allow data to print on screen
using namespace std;//to be defind cin and cout
int main()//function main begins program
{
int x=0,y=0,z=0;

cout<<"enter two numbers to get specific range (start with small number then
big): "<<endl;
cin>> y>>z;
cout <<"the numbers between a specific range : "<<endl;
for(x=y;x<=z;x++)//for loop (start loop;endloop;increase)

cout<<x <<"\t"; //print this loop

return 0;// the progam end function successfully


}//end main function

You might also like