Lab 6
Lab 6
Semester 1 2022/2023
CRN: 1202
Loops
Objective
On completion of this experiment, the student will be able to write compile and run a C++
program
Introduction
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;
avg=(sum+=x)/x; //avg
cout<<"the avg of this number is: "<<avg <<endl; //print this loop
{
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
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)