 CPP All : Simple Loops Concept Learning Code Sheets #### Repeating a message with for loop #1982

Using for loop construct the same code piece can be repeated a fixed number of times.

#### Sum of natural numbers within a range – for loop #1986

We use a for loop to sum numbers from 1 to 10. This can changed to any other range also.

#### Sum of natural numbers within a range – while loop #1989

We use a while loop to sum numbers from values filled in begin and end.

#### Print table of an integer number #1992

Printing the table of an integer number like 5×1=5, 5×2=10, 5×3=15 and so on.

#### For and while loop comparison #1995

Comparing simple for and while loop with a simple number printing example

#### Odd number series with double step counter – for loop #1997

Printing odd number series by incrementing counter in double step using for loop

#### Odd number series with divisibility test – for loop #1999

Printing odd number series by testing divisibility test on each number in the for loop

#### Multiple initialisation and multiple step instructions in for loop #2003

A demonstration program to show how multiple initialisation and multiple step instructions in for loop can be used

#### Print till Z with loop count calculation #2034

As user enters a lower case or upper case alphabet the program prints from that alphabet till z.

#### Print till Z with while loop boundary crossing #2038

As user enters a lower case or upper case alphabet the program prints from that alphabet till z using the faster technique of while loop iteration and checking for outside boundary condition

#### Factorial program with limit checking and digit series printing #2042

A program that finds the factorial of a program and checks the limit of number when incorrect outputs may occur due to data size.

#### Continuous sum using do-while exit on 0 input #2061

This program sums the user inputs continuously and if user input 0 it terminates the loop and displays the sum.

#### Redo of program activity using a do-while loop #2064

Use of do-while loop to redo or re-perform a program activity using a do-while loop. Here we shall redo the activity of printing a number table.

Solved Problems #### Sum of natural numbers up to a value given by the user. #2005

Write a c++ program to collect a positive integer value from the user and pri

Write a c++ program to collect a positive integer value from the user and print the list of all natural numbers up to that number. Also print the sum of the natural number printed.

#### Print all divisors and count of divisors #2016

Write a c++ program to ask an integer number from the user and then print all

Write a c++ program to ask an integer number from the user and then print all its divisors in one line, followed by the count of divisors. The output should be as per example given below.

Enter an integer :45
The divisors of 45 are –
1 3 5 9 15 45
The count of divisors = 6

#### Output writing – double increment in loop #2031

Write the output of the following code. Assume that required header files are

Write the output of the following code. Assume that required header files are included.

```int main()
{
for(int i=10;i<=50;i+=10)
cout<<i++/2<<" ";
return 0;
}```

#### Printable ascii codes #2045

Write a c++ program to print ascii codes and given printable characters from

Write a c++ program to print ascii codes and given printable characters from decimal ascii code 32 to 126 (The printable ascii code range). Print each code and character set in a new line.

#### Secret code with alphabet divisible by five #2047

Instead of sending his secret code a programmer tells his friend that my secr

Instead of sending his secret code a programmer tells his friend that my secret code is a series of characters which are upper and lower alphabet sequence in their ascending order which are divisible by five. Can you help the programmer’s friend to decipher this code.

#### Square and square root of numbers in a range #2049

Write a c++ program to output the square and square roots of a number in a ra

Write a c++ program to output the square and square roots of a number in a range where range begin variable is initialised in the variable ‘from’ and range end variable is initialised in the variable ‘to’.

#### Powering a number using a loop #2053

Write a c++ program to power a base number with the given exponent by using a

Write a c++ program to power a base number with the given exponent by using a for loop.

#### Repeating a character using loop #2218

Write a program to repeat the character as given by the user for number of ti

Write a program to repeat the character as given by the user for number of times as desired by the user. Show the repeated character in the same line. Use for loop for repeatition.

#### Output writing – loop with unary operators in cout chain #2346

Solve the following program to show its output. Assume that required header f

Solve the following program to show its output. Assume that required header files are included

```int main()
{
int a,b;
a=(b=10);
for(int i=10;i<30;i+=10)
{
cout<<a++ + i<<" "<<(b=++b -i)<<endl ;
}
return 0;
}

```

#### For to while conversion #3001

Write the following program again using the while loop instead of for loop

Write the following program again using the while loop instead of for loop

#### Suggesting Error Corrections in Natural Number Addition #3221

A student has written this program to sum natural numbers from 1 to 10. His�

A student has written this program to sum natural numbers from 1 to 10. His program is not giving correct results. Please help in correcting this program with right explanations as well.

#### Error identification #5184

Identify errors in the following program. assume that appropriate header file

Identify errors in the following program. assume that appropriate header files are included.

```int main()
{
x=1;
for(int j=10; j>5; j--)
cout>>x;
return 0;
}```

#### Output writing – Three for loops #5362

Write the output of the following code. Assume all header files are included.

Write the output of the following code. Assume all header files are included.

```int main()
{
int i;
for(i=10; i>6; i=i-2) cout<<i<<" ";
cout<<endl;
for(i=-5; i> -8; i--) cout<<i+1<<" ";
cout<<endl;
for(i=-3,sum=0; i<3; i--) cout<<sum++<<" ";
cout<<endl;
return 0;
}```

#### Code Error Finding #5368

Rewrite the following program after removing possible compiler errors. Mark e

Rewrite the following program after removing possible compiler errors. Mark each correction by underlining it. Assume that relevant header files are included.

```int main()
{
const int d 5;
number = 15;
for(int i=1; i<=5; i++, number-=3)
if(number%d==0) cout<<number/d<<" ";
else cout<<number+d<<" ";
}```

Code Sheets:13  Solved Problems:14
×