You are on page 1of 14

Labsheet 5- Loops (Part 1)

1. Write a program that displays all multiples of 5 between 0 and 100 inclusive.

// a program that displays all multiples of 5 between 0 and 100 inclusive

#include <iostream>
using namespace std;

int main()
{

int x=0;

while (x<=100){

if(x%5==0){
cout<<x<<endl;
}

x++;
}
return 0;
}
2. Write a program that displays the sum of all multiples of 5 between 0 and 100 inclusive.

//a program that displays the sum of all multiples of 5 between 0 and 100 inclusive.

#include <iostream>
using namespace std;

int main()
{

int x=0;
int sum=0; //accumulator

while (x<=100){

if(x%5==0){
sum=sum+x;
}

x++;
}

cout<<"The sum is:"<<sum<<endl;

return 0;
}
3. Write a program that allows the input of an integer value n and displays all multiples
of 3 which are less than or equal to n, as well as the sum of the square of these values.

#include <iostream>
using namespace std;

int main()
{

int n, x;
int sum=0; //accumulator

cout<<"Enter a number:";
cin>>n;

while (n>=0){

if(n%3==0){
sum=sum+(n*n);
cout<<n<<endl;
}

n--;
}

cout<<"The sum is:"<<sum<<endl;

return 0;
}
4. Write a program that allows you to input an integer value n. If n is greater than 100, it
displays the message ‘Wrong Input’, otherwise it displays all factors of n.

#include <iostream>
using namespace std;

int main()
{

int n;

cout<<"Enter a number less than 100:";


cin>>n;
cout<<endl;

while (n>100){

cout<<"Wrong input"<<endl;
cout<<"Input a number less than 100:";
cin>>n;
cout<<endl;
}

cout<<"Factors of "<<n<<" are:"<<endl;

for (int x=1; x<=n; x++){

if (n%x==0){
cout<<x<<endl;
}
}

return 0;
}
5. Write a program to input a set of numbers and print out their average. The program will
start by prompting the user for the number of numbers to be input and will then prompt
for the individual numbers.

#include <iostream>
using namespace std;

int main()
{
int num, numset;
float sum=0;

cout<<"Input how many numbers are to be averaged:";


cin>>num;
cout<<"Input numbers:"<<endl;

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


cin>>numset;
sum=sum+numset;
}

cout<<"The average is "<<sum/num;

return 0;
}
6. Modify the previous program to print out the largest and smallest number read in as
well as the average. Also change the prompt to show the number of values still to be
entered.

#include <iostream>
using namespace std;

int main()
{
int num, numset, large, small;
float sum=0;
int x=0;

cout<<"Input how many numbers are to be averaged:";


cin>>num;
cout<<"Input numbers:"<<endl;

while(x<num){

cin>>numset;
sum=sum+numset;

if (x==0)
{
large=numset;
small=numset;
}

else
{
if(numset>large)
{
large=numset;
}

else if(numset<small)
{
small=numset;
}
}

x++;
cout<<"Number of integers left to be input:"<<num-x<<endl;
}

cout<<"The largest number is "<<large<<endl;


cout<<"The smallest number is "<<small<<endl;
cout<<"The average is "<<sum/num;

return 0;
}
7. Write a program to prompt the user for an integer and calculate the sum of all the
integers up to and including the input value. The program should also display the result.

#include <iostream>
using namespace std;

int main()
{
int x;
int sum=0;

cout<<"Input an integer:";
cin>>x;

while (x>=0){
sum=sum+x;
x--;
}

cout<<"Sum is "<<sum;

return 0;
}
8. Modify the previous program to add up the reciprocals of all the integers up to and
including the input value.

#include <iostream>
using namespace std;

int main()
{
float x;
float sum=0;

cout<<"Input an integer:";
cin>>x;

while (x>0){
sum=sum+(1/x);
x--;
}

cout<<"Sum is "<<sum;

return 0;
}
9. To calculate x to power n, (where n is a positive integer), we can use a loop that
multiplies x by itself n times. Thus the code can be as follows:
p=1
i=1
while (i<=n):
p= p*x;
Include the code in a program that allows for the input of values for x and n and
calculates and displays xn

#include <iostream>
using namespace std;

int main()
{
int x, n;
int i=1;
int p=1;

cout<<"Enter an integer, x:";


cin>>x;

cout<<"Enter the power, n, to which it is raised:";


cin>>n;

while (n<0){
cout<<"\nInvalid input. n should be positive"<<endl;
cout<<"Enter the power to which it is raised:";
cin>>n;
}

while (i<=n){
p=p*x;
i++;
}

cout<<"\nx to the power of n is "<<p;

return 0;
}
10. The factorial of a positive integer n is defined as 1*2*3……n. Write a program that
allows for the input of a value n and calculates and displays the factorial of n.

#include <iostream>
using namespace std;

int main()
{
int n;
int a=1; //accumulator
int x=1;

cout<<"Input an integer:";
cin>>n;

while (x<=n){
a=x*a;
x++;
}

cout<<"Factorial of "<<n<<" is "<<a;

return 0;
}
11. A person currently has an amount P of money, in a bank, which pays an annual
interest rate of r%. Write a program that allows the input of values for P, r as well as n,
where n represents the number of years for which the money has remained in the bank.
The program must calculate and display the total amount in the person’s account at the
end of each year for the n years. Display the results in an appropriate format.
Note that this problem involves the calculation of compound interest.

#include <iostream>
using namespace std;

int main()
{
double p;
float r;
int n;
int x=1;

cout<<"Input principal balance deposited in Rs:";


cin>>p;

cout<<"\nInput % annual interest rate:";


cin>>r;

cout<<"\nInput number of years since deposit:";


cin>>n;

double a=p;

while (x<=n){
a=a*(1+(r/100));

cout<<"The total amount accumulated after year"<<x<<" is Rs"<<a<<endl;

x++;
}

return 0;
}
12. Write a program that uses a while loop to determine how long it takes for an
investment to double at a given interest rate. The input will be an annualized interest rate,
and the output is the number of years it takes an investment to double.
Note: the amount of the initial investment does not matter; you can use MUR 100.

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
float invest=1;
float r;
float interest;
int y=0;

cout<<"Input annual interest rate:";


cin>>r;

float p=r/100;

while(invest<2)
{
interest=(p*invest);
invest=invest+interest;
y++;
}

cout<<"Number of years taken to double investment is "<<y<<endl;

return 0;
}
13. Write a program that uses an interactive loop and allows the input of a number of
positive values and displays the number of even values input as well as the sum of the
even values

#include <iostream>
using namespace std;

int main()
{
int num ;
float sum=0;
int x=-1;

cout<<"Input a set of integers to find the number of even numbers and their sum.";
cout<<"\nTo stop inputing numbers, input a negative number."<<endl;

do
{

if (((num%2)==0)&&(num>=0))
{
sum=sum+num;
x++;
}

cin>>num;

while(num>-1);

cout<<"The number of even numbers is "<<x;


cout<<"\nThe sum of even numbers is "<<sum;

return 0;
}

You might also like