0% found this document useful (0 votes)
8 views

Loop Examples

The document contains 7 code snippets that solve various problems: 1) Calculates sum, average, max, min of 10 numbers 2) Calculates sum of even and odd numbers from 10 user inputs 3) Prints characters until user inputs 'z' 4) Calculates factorial of a user-input number 5) Differentiates a polynomial equation given coefficients and power 6) Tracks income and distance from taxi trips until total income exceeds 300, outputs number of trips, total distance, shortest trip 7) Solves an additional problem, details not provided

Uploaded by

Zien Mohamed
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Loop Examples

The document contains 7 code snippets that solve various problems: 1) Calculates sum, average, max, min of 10 numbers 2) Calculates sum of even and odd numbers from 10 user inputs 3) Prints characters until user inputs 'z' 4) Calculates factorial of a user-input number 5) Differentiates a polynomial equation given coefficients and power 6) Tracks income and distance from taxi trips until total income exceeds 300, outputs number of trips, total distance, shortest trip 7) Solves an additional problem, details not provided

Uploaded by

Zien Mohamed
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

Write C++ program that calculates the sum, the


average, the maximum and the minimum of 10 numbers entered
by the user.

# include <iostream>
using namespace std;
void main()
{
float num,sum=0,avg,Max=0,Min=99999;

cout<< " please enter num";


for(int i=0;i<10;i++)
{
cin>>num;
sum+=num;

if(num >Max)
Max=num;
if(num <Min)
Min=num;
}
avg=sum/10;
cout<<" average"<<avg <<endl;
cout<<" Max="<<Max<<endl ;
cout<<" Min="<<Min ;
system("pause");
}

2\Write C++ program that reads 10 integer numbers from the


user then the program should calculates the sum of odd
numbers and the sum of even numbers.

# include <iostream>

using namespace std;

void main()
{ int a,sumodd=0,sumeven=0;
cout<< " please enter num";
for(int i=0;i<10;i++)
{
cin>>a;
if((a%2)==0)
sumeven+=a;
else
sumodd+=a;
}

1
cout<<" sumeven"<<sumeven;
cout<<" sumodd="<<sumodd ;
system("pause");
}

3. Write C++ program that print characters till user types


character z.

# include <iostream>
using namespace std;
void main()
{
char m;
cout<<"please enter chararter";
cin>>m;
while(m != 'z')
{
cout<<m;
cin>>m;
}
system("pause");
}

4. Write a C++ program that calculates the factorial to


the number entered by the user.

#include <iostream>
using namespace std;

void main ()
{
int f=1,a;

cout<<"Please enter num";


cin>>a;

for(int i=a;i>=1;i--)
{
f*=i;
}
cout<<"factorial="<<f;
system("pause");

2
5. Write a program to compute the differentiation equation.
The program will read the coefficient, the power and the
variable. Then display the given equation and the equation
after it differentiates.
Example: 2X4
=>8X3
#include <iostream>
using namespace std;

void main ()
{
int p,c;

cout<<"Please enter c and p";


cin>>p>>c;

for(int i=p;i>=1;i--)
{
cout<<c<<"X"<<i<<endl;
c*=i;
}
system("pause");

7. Assume that there is a driver of a taxi and he asked you


to develop a program to him as the followings:

He will enter the information for each trip during his day.

NOTE: trip's data will be:

- The income of the trip.

- The distance of the trip.

NOTE: the deriver will stop the program when he got total

income in his day > 300 L.E.

 Display how many trips he made in his day.

 Display the total distance he made during his day.

 Determine which trip was the shortest trip.

# include <iostream>

3
using namespace std;
void main()
{
float income,distance, tincome=0,tdistance=0,
shortest=99999;
int count=0;

while(tincome < 300)


{
cout<<"please enter income,distance";
cin>>income>>distance;
tincome+=income;
tdistance+=distance;
if(distance<shortest)
shortest=distance;
count++;

}
cout<< " how many trips he made is"<< count ;
cout<<" the total distance"<<tdistance ;
cout<<" the shortest trip"<<shortest ;

system("pause");
}

You might also like