C++ exercises and solutions: C++ loop statements (con)


C++ loop statements (con)

3. Write a C++ program that will print the pattern as shown below:


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

Solution:


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

3. Write a program that will ask the user to input n positive numbers. The program will terminate if one of those number is not positive.

Solution


#include <cstdlib>
#include <iostream>
 
using namespace std;
 
int main(int argc, char *argv[])
{
 int x;
do{
            cout<<"Enter a number:";cin>>x;
            cout<<"\n";
     }while(x>0);
  
 
   system("PAUSE");
   return EXIT_SUCCESS;
}


Comments

Shady comment

 Shady

Using nested loops, write a C++ program that draws sin(x) function approximation pattern.
Hint: draw 20 stars when sin(x) is max “i.e. sin (90) & sin (270) “


2018-04-15
lolus comment

 lolus

please help

Write a C++ program that will print the following pattern using while loops.
*******
******
*****
****
***
**
*


2018-03-26
Maksud alam rony comment

 Maksud alam rony

#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
for (int i=1; i<=n/2; i++)
{

for (int j=1; j<=i; j++){
cout <<"* ";
}
cout <<endl;
}
for (int i=n/2; i>=1; i--)
{
for (int j=i; j>=1; j--)
{
cout <<"* ";
}
cout <<endl;
}
}


2018-02-25
Mark comment

 Mark

This works as well:

int x = 0;
while (!(x < 0)) {
cout << "Type positive numbers: \n;
cin >> x;
}
}


2017-04-06
HMZ1919898 comment

 HMZ1919898

#include<iostream>
using namespace std;
int main()
{
int r,c,k;
for( r=1; r<=5; r++ )
{
for(c=1; c<=6-r; c++)
{
cout<<"*";
}
for(k=1; k<=r; k++)
{
cout<<" ";
}
for(c=1; c<=6-r; c++)
{
cout<<"*";
}
cout<<endl;
}
/*for(r=2; r<=5; r++)
{
for(c=1; c<=r; c++)
{
cout<<"*";
}
for(k=1; k<=5-r; k++)
{
cout<<" ";
}
for(c=1; c<=r; c++)
{
cout<<"*";
}
cout<<endl;
}
return 0;*/
}
//adhamza234@gmail


2016-09-30
Bilal Mughal` comment

 Bilal Mughal`


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


for (;num>=0;)
{

cout<<"enter the positive number\n";
cin>>num;

}

cout<<"you entered negative number!ppp";
cout<<"\nprogram terminated";

getch();
return 0;

}


2016-06-13
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<=i;j++)
{

cout<<"*";
}

i++;
cout<<"\n";
}

for(int i=num;i>=1;i=i-2)
{
for(int j=1;j<=i;j++)
{
cout<<"*";

}
cout<<"\n";
}



getch();
return 0;

}


2016-06-13
m w comment

 m w

A fundamental understanding of how odd numbers are defined in number theory helps here. Odd numbers are defined as 2n - 1 (or 2n + 1) where n is a member of the set of integers. Counting i up and down to 5 and letting j = 2i - 1 each time gives the odd numbers you need in each loop for printing the correct number of *s.


2016-02-20
m w comment

 m w

int i;
int j;

for (i = 1; i <= 5; i++)
{
for (j = 2 * i - 1; j >= 1; j--) std::cout << "*";
std::cout << std::endl;
}
for (i = 5; i >= 1; i--)
{
for (j = 2 * i - 1; j >= 1; j--) std::cout << "*";
std::cout << std::endl;
}
return 0;


2016-02-20
shaan comment

 shaan

#include<stdio.h>
#include<string.h>

char* minimumCost(char* input1[],int input2)
{
//Write code here
}



2015-11-22



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.