Sunday, January 24, 2016

C++ loop statements (con)

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

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; 
}

No comments

Post a Comment