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

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;
} ShadyUsing 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 lolusplease help Write a C++ program that will print the following pattern using while loops. ******* ****** ***** **** *** ** *2018-03-26 Maksud alam rony#include 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 <=1; i--) { for (int j=i; j>=1; j--) { cout <<"* "; } cout <> x; } }2017-04-06 HMZ1919898#include 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< #include 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 #include #include 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 wA 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 wint 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#include #include char* minimumCost(char* input1[],int input2) { //Write code here } 2015-11-22