Login


Lost your password?

Don't have an account ? Register ×
  
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.

#include <iostream>
using namespace std;
int main()
{
for(int n=0;n<10;n++)
cout<<"Hello developer!"<<endl;
return 0 ;
}

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.

#include <iostream>
using namespace std;
int main()
{
int sum=0;
for(int i=1;i<=10;i++)
{
sum=sum+i;
}
cout<<"Sum of Numbers from 1 to 10 = "<<sum<<endl;
return 0;
}

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.

#include <iostream>
using namespace std;
int main()
{
int sum=0,begin=5,end=15;
int i=begin;
while(i<=end)
{
cout<< i <<" ";
sum=sum+i;
i++;
}
cout<<endl
<<"Sum of above numbers = "
<<sum<<endl;
return (0);
}

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.

#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"Print a table of : ";
cin>>n;
for(int i=1;i<=10;i++)
{
cout<<n<<"x"<<i<<"="<<n*i<<endl;
}
return 0;
}

For and while loop comparison #1995

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

#include <iostream>
using namespace std;
int main()
{
for(int i=1;i<=10;i++)
cout<<i<<" ";
cout<<endl;
int j=1;
while(j<=10)
{
cout<<j<<" ";
j++;
}
cout<<endl;
return 0;
}

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

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

#include <iostream>
using namespace std;
int main()
{
for(int i=1;i<=20;i+=2)
cout<<i<<" ";
return 0;
}

Odd number series with divisibility test – for loop #1999

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

#include <iostream>
using namespace std;
int main()
{
for(int i=1;i<=20;i++)
if(i%2!=0) cout<<i<<" ";
return 0;
}

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

#include <iostream>
using namespace std;
int main()
{
int sum=0;
for(int i=1,j=1;i<=10;i++,j++)
{
sum=sum+i+j;
}
cout<<"Sum of alternate numbers from 2 to 20 = "<<sum<<endl;
return 0;
}

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.

#include <iostream.h>
using namespace std;
int main()
{
char ch;
int lc;
cout<<"Enter an alphabet :";
cin>>ch;
int n=ch;
if(n>=65 && n<=90) lc=90-n+1;
else if (n>=97 && n<=122) lc=122-n+1;
else {cout<<"Entry was not an alphabet"; return 0;}

for(int i=0;i<lc;i++)
cout<< ((char) n++);
return 0;
}

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

#include <iostream>
using namespace std;
int main()
{
char ch;
cout<<"Enter an alphabet :";
cin>>ch;
if(ch>=65 && ch<=90)
while(ch!=91) cout<< ch++;
else if(ch>=97 && ch<=122)
while(ch!=123) cout<< ch++;
else cout<<"Entry was not an alphabet";
return 0;
}

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.

#include <iostream>
using namespace std;
int main()
{
int n,i=1;
int f=1;
cout<<"Enter a positive integer (<=12) :";
cin>>n;
if(n>12)
cout<<"Given value may give wrong output"<<endl;
while(i<=n)
{
cout<<i<<(i<n?"x":"");
f *=i;
++i;
}
cout<<" = "<<f<<endl;
cout<<n<<"! = "<<f<<endl;
return 0;
}

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.

#include <iostream>
using namespace std;
int main()
{
double number, sum = 0.0;
do {
cout<<"Enter a number: ";
cin>>number;
sum += number;
}while(number != 0.0);
cout<<"Total sum = "<<sum;
return 0;
}

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.

#include <iostream>
using namespace std;
int main()
{
int i = 1, a, t;
char yn;
do{
cout << "Table of which number to print : ";
cin >> a ;
while(i<=10)
{
t = a*i;
cout<<t;
cout<<"t";
i++;
}
cout<<endl;
t=0;i=1;
cout<<"want to try another value y/n?";
cin>>yn;
}while(yn=='Y' ||yn =='y');
return(0);
}
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

Print all divisors and count of divisors #2016

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

Output writing – double increment in loop #2031

Write the output of the following code.

Printable ascii codes #2045

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

Secret code with alphabet divisible by five #2047

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

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

Powering a number using a loop #2053

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

Repeating a character using loop #2218

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

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

Solve the following program to show its output.

For to while conversion #3001

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Ā


Code Sheets:13  Solved Problems:11 
Back