C++ exercises and solutions: C++ loop statements


C++ loop statements

1. Write a C++ program that will print the following pattern


*******
******
*****
****
***
**
*

Solution:


#include <cstdlib>
#include <iostream>
#include<iomanip>
#include<cmath>
 
using namespace std;
 
int main(int argc, char *argv[])
{
 int i=7;
 int j=7;
 
 for(i=1;i<=7;i++){
  for(j=7-i;j>=1;--j)cout<<"*";
  cout<<"\n";
}
  
   cout<<"\n";
   system("PAUSE");
   return EXIT_SUCCESS;
}

2. Write a program that will print the following pattern:

       1******

       12*****

       123****

       1234***

       12345**

       123456*

       1234567

Solution:


#include <cstdlib>
#include <iostream>
#include<iomanip>
#include<cmath>
 
using namespace std;
 
int main(int argc, char *argv[])
{
 int i;
 int j;
 int k;
 
 for(i=1;i<=7;i++){
  for(j=1;j<=i;++j)
    cout<<j;               
               
  for(k=7-i;k>=1;k--)cout<<"*";                
  
  cout<<"\n";
}
  
   cout<<"\n";
   system("PAUSE");
   return EXIT_SUCCESS; }


Comments

Maksud alam rony comment

 Maksud alam rony

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


2018-02-25
kavyashree comment

 kavyashree

#include<iostream>
using namespace std;

void print_pattern(int num)
{
for (int i=num;i>0;i--){
cout << "*";
}
}

int main()
{
int iteration = 6;
for (int i=iteration; i>0;i--){
print_pattern(i);
cout <<endl;
}

}


2017-09-08
vipin bahar comment

 vipin bahar

#include <iostream>
using namespace std;
int main()
{
int k,i,j,n;

cout<<"enter the value of n";
cin>>n;
for(i=1;i<=n;i++)
{

{
for(k=1;k<=i;k++)
cout<<k;
}
{
for(j=n-i;j>0;j--)
cout<<"*";
cout<<"\n";
}

}

}


2017-07-19
mohamed bardana comment

 mohamed bardana

int main()
{
for (int lines =1 ; lines <=7 ; lines++ )
{
for (int stars =1; stars<=8-lines ; stars++)
cout << "*";
cout << endl ;
}

return 0;

}


2017-06-02
Sumon comment

 Sumon

This is an easy way to solve .....

#include <iostream>
using namespace std;

int main ()
{

for (int i=0; i<=6; i++)
{
for(int j=6; j>=i; j--)
cout << "*" ;
cout << endl;
}
cout << endl;

}


2017-05-28
Hamza  comment

 Hamza

#include <iostream>

using namespace std;

int main() {
char array[7] = { '.', '.', '.', '.', '.', '.', '.' };
//cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
//cout<<sizeof(array)<<endl;
//cout<<sizeof(char)<<endl;
//cout<<array<<endl;
//cout<<array[0]<<endl;
//cout<<array[6]<<endl;

for (unsigned int i = 0; i <= sizeof(array) / sizeof(char)-1; i++) {

if (i == 0) {
cout << "1******" << endl;
} else if (i == 1) {
cout << "12*****" << endl;
} else if (i == 2) {
cout << "123****"<<endl;
} else if (i == 3) {
cout << "1234***" << endl;
} else if (i == 4) {
cout << "12345**" << endl;
} else if (i == 5) {
cout << "123456*" << endl;
} else {
cout << "1234567" << endl;
}
}

return 0;
}


2017-04-19
hamna comment

 hamna

#include "stdafx.h"
#include <cstdlib>
#include <iostream>
#include<iomanip>
#include<cmath>

using namespace std;

int main()
{
for(int i=1;i<=7;i++)
{
for(int j=1;j<=i;j++)
{
cout<<j;
}
for(int k=6;k>=i;k--)
{
cout<<"*";
}
cout<<endl;
}
system("PAUSE");
return 0;
}


2017-04-15
shaa comment

 shaa

dgood


2017-03-17
Jim comment

 Jim

#include<iostream>


int main()
{
int k = 0;
int n = 7;
while (n-- > 0) {
//std:cout << "** " << n << "*** " << endl;
k = 7 - n;
for (int j = 0; j < k; ++j) {
std::cout << (j+1);
}

for (int i = 7; i > k; --i) {
std::cout << ".";
}
std::cout << std::endl;
}



return 0;
}



2017-02-16
siddharth jadhav comment

 siddharth jadhav

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main()
{clrscr();
int a,b,c,n;
cin>>n;
for(a=1;a<=n;a++)
{
for(b=1;b<=a;b++)
{cout<<setw(2)<<b;}
for(c=1;c<=(n-a);c++)
{cout<<setw(2)<<"*";}
cout<<"\n";
}
getch();
}


2017-01-05
BIOS comment

 BIOS

I used while for this (ignore the redundant directives):

#include <iostream>
#include <cmath>
#include <string>
#define n "\n"
using namespace std;
int main()
{
int i(1),j(0),b(1),num;
cin>>num;
cin.ignore();
while (i<=num) {
while (b<=i) {
cout<<b;
b++;
}
while (j<num-i) {
cout<<"*";
j++;
}
cout<<n;
i++;
b=1;
j=0;
}
cin.ignore();
}


2016-12-30
ashish comment

 ashish

#include<iostream.h>
#include<conio.h>
void main()
{
int i,j;
for(i=1;i<=7;i++)
{
for(j=7;j>=i;j--)
{
cout<<".";
}
cout<<"\n";
}
getch();
}


2016-12-30
Steve comment

 Steve

I used a slightly different method by treating the numbers as a sequence; i.e. next number in the sequence is the current value times 10 and then add the value of i (the number we are at in the sequence) then a for loop to put the stars in.


2016-12-03
saptarshi saha comment

 saptarshi saha

here's your answer:
#include<iostream.h>
int main()
{
for(int i=1;i<=7;++i)
{
for(int j=1;j<=i;++j)
cout<<j;
for(int k=1;k<=7-i;++k)
cout<<"*";
cout<<endl;
}
return 0;
}


2016-11-22
zain comment

 zain

#include<iostream>
using namespace std;
int main()
{
for(int j=1;j<=7;j++)
{
for(int i=1;i<=7;i++)
{
if(i<=j)
{
cout<<i;
}
else
{

cout<<"*";
}
}
cout<<endl;
}


system("pause");
}


2016-08-17
zain comment

 zain

#include<iostream>
using namespace std;
int main()
{
for(int j=1;j<=7;j++)
{
for(int i=1;i<=7;i++)
{
if(i<=j)
{
cout<<i;
}
else
{

cout<<"*";
}
}
cout<<endl;
}


system("pause");
}


2016-08-17
Bilal Mughal comment

 Bilal Mughal


#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int num;

cout<<"enter the number where you want to go";
cin>>num;

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

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

if(j<=i)
{

cout<<j;

}
else
{
cout<<" ";
cout<<"*";

}

}

cout<<"\n";

}

getch();
return 0;

}


2016-06-13
Bilal Mughal` comment

 Bilal Mughal`

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int n;
cout<<"enter the value of N\n";
cin>>n;



for(int i=n;i>=1;i--)
{

for(int j=i;j>=1;j--)
{

cout<<"*";

}
cout<<"\n";

}


getch();
return 0;

}


2016-06-12
Bilal Mughal` comment

 Bilal Mughal`

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int n;
cout<<"enter the value of N\n";
cin>>n;



for(int i=n;i>=1;i--)
{

for(int j=i;j>=1;j--)
{

cout<<"*";

}
cout<<"\n";

}


getch();
return 0;

}


2016-06-12
arkadip jana comment

 arkadip jana

its nice.


2016-01-04



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.