0% found this document useful (0 votes)
11 views5 pages

Lab3 Loops

Uploaded by

Nardos Tesema
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views5 pages

Lab3 Loops

Uploaded by

Nardos Tesema
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

# include<iostream>

# include<iomanip>

using namespace std;

int main()

for(int i=1;i<=50;i=i+2)

cout<<i<<"\t";

// >>>>>>>>>>>>>>>>>>>>>>>>>>

int i, sums;

sums=0;

for(i=1;i<=50;i++)

sums+=i;

cout<<"the sum of 1 to "<<i<<" is "<<sums<<endl;

cout<<"sum= "<<sums;

// computing factorial of n

int n;

int fact =1;

cout<<"please enter, what factorial u want to compute(enter a no): ";

cin>>n;

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

fact=fact*i;
}

cout<<endl;

cout<<"the factorial of "<<n<<" is: " <<fact;

// the sum and average of n numbers,

int no,sum,num,avg,counter;

sum=0;

cout<<"enter a number ... ";

cin>>no;

cout<<endl;

for(int counter=1;counter<=no;counter++)

cout<<"enter the no to be added ";

cin>>num;

sum=num+sum;

cout<<endl;

avg=sum/no;

cout<<"the sum of "<<no<<"is: "<<sum<<endl;

cout<<"the average is: "<<avg<<endl;

// prime numbers up to 100 using nested loop

int a, b;

for(a=2; a<100; a++) {


for(b=2; b<= (a/b); b++)

if(!(a%b)) break; // if factor found, not prime

if(b > (a/b)) cout << a << " is prime\n";

//
******************************************************************************
***************************

// tree drawing using while loop

int height; // Height of tree

cout << "Enter height of tree: ";

cin >> height; // Get height from user

int rows = 0; // First row, from the top, to draw

while (rows < height) { // Draw one row for every unit of height

// Print leading spaces

int count = 0;

while (count < height - rows) {

cout << " ";

count++;

// Print out stars, twice the current row plus one:

// 1. number of stars on left side of tree

// = current row value


// 2. exactly one star in the center of tree

// 3. number of stars on right side of tree

// = current row value

count = 0;

while (count < 2*rows + 1) {

cout << "*";

count++;

// Move cursor down to next line

cout << endl;

// Change to the next row

rows++;

//
*************************************************************************************

// multiplication table using nested loop

int size; // The number of rows and columns in the table

cout << "Please enter the table size: ";

cin >> size;

// Print a size x size multiplication table

// First, print heading: 1 2 3 4 5 etc.

cout << " ";

// Print column heading

int column = 1;

while (column <= size) {

cout << setw(4) << column; // Print heading for this column.
column++;

cout << endl;

// Print line separator: +------------------

cout << " +";

column = 1;

while (column <= size) {

cout << "----"; // Print line for this column.

column++;

cout << endl;

// Print table contents

int row = 1;

while (row <= size) { // Table has size rows.

cout << setw(2) << row << " |"; // Print heading for this row.

int column = 1; // Reset column for each row.

while (column <= size) { // Table has size columns.

int product = row*column; // Compute product

cout << setw(4) << product; // Display product

column++; // Next element

row++; // Next row

cout << endl; // Move cursor to next row

You might also like