| Loop practice questions in C and C++ | |
|
|
Author | Message |
---|
Admin Administrator
Number of posts : 22 Age : 34 Registration date : 2008-11-12
| Subject: Loop practice questions in C and C++ 19/11/08, 07:04 pm | |
| Practice Problem 1
Print the pattern like shown below with loops , where the number of stars in first row rectangle is input by user
Example if no of stars are 11 in first rectangle
* - *********** *** - ********* ***** - ******* ******* - ***** ********* - *** *********** - *
Practice Problem 2
Print the pattern like shown below with loops , where the X(variable) is input by user
Example if X is 6
*** ****** ****** *** *** ****** ****** *** *** ****** ****** ***
Example if X is 8
**** ******* ******* **** **** ******* ******* **** **** ******* ******* **** **** ******* ******* **** | |
|
| |
nouman Level 0
Number of posts : 1 Registration date : 2008-11-15
| Subject: Re: Loop practice questions in C and C++ 25/11/08, 09:59 pm | |
| hey ppl sort me out m nt good at loops ....meri watt lag rai hai in main ..can any body help me for this ..i will b very very greatfull to that person ...thnx i mean how to implement nested loops in a right way ..... | |
|
| |
ammar26 Administrator
Number of posts : 18 Age : 34 Registration date : 2008-11-12
| Subject: Practice ..... 25/11/08, 10:05 pm | |
| You just have to practice more and more in order to get command
Remember some points while doing nested loops
1 - Make sure you use different variables for controlling the counter of both loops ( if you are working with counter control loops)
2 - Always reset the counter of inner loop before its start in outer loop
3 - Always make sure to use delimiters inside loops and right align the inner loop so that you can understand easily
4 - Do not make such conditions in which both loops can collide | |
|
| |
Admin Administrator
Number of posts : 22 Age : 34 Registration date : 2008-11-12
| Subject: Get familer with syntax and sequence ........ 26/11/08, 04:41 pm | |
| Like While Loop Syntax
The while loop executes as long as the conditional expression remains true (therefore it is required that the code within the loop manipulates the conditional expression in such a way as to eventually result in the stopping condition so that the loop does not execute forever...) while (condition) { statement 1; statement 2; . . statement n; }
Consider the following example
int x; while (x < 10) { DoSomething; // any statement x = SomeCodeResult; }
The loop code manipulates the variable x and should eventually return a result that makes x greater than or equal to ten so that the loop terminates at some point. | |
|
| |
Admin Administrator
Number of posts : 22 Age : 34 Registration date : 2008-11-12
| Subject: I'll make some posts for nested loops 26/11/08, 04:44 pm | |
| I'll make some posts for nested loops practice
nouman I'll create a specific topic in forums and will do some posts related to nested loops instructions So that you will be more familiar with nested loops structure | |
|
| |
Hafiz Talha Level 0
Number of posts : 2 Registration date : 2008-11-15
| Subject: must do for begginers 05/12/08, 03:33 pm | |
| these are very gud and intrsting questions and these are must do for the begginers. a very gud loop practice. i'll try to solve n post the solutions of these problems | |
|
| |
precious.logic Level 0
Number of posts : 2 Age : 36 Registration date : 2008-11-20
| Subject: Here is the solution of question 16/12/08, 03:35 pm | |
| Problem Num 1: #include<iostream.h> #include<conio.h>
main (){
int a=1,b=0; clrscr(); cout<<"Enter The num of to print the star="; cin>>b; while(b>0){
int c=0,d=0; while(c<a){ cout<<"*"; c++; } cout<<"-";
while(d<b){
cout<<"*"; d++; } cout<<"\n" ;
a=a+2; b=b-2;
}
getch (); return 0;
}
Problem Num 2:
# include<iostream.h> # include<conio.h> main (){ int a=0,b=0,e=0; clrscr (); cout<<"Enter a num to print the stars"; cin>>b; a=b/2; e=b; while(b>0){ int c=0,d=0; if(b%2==1){ while(c<a){ cout<<"*"; c++; } cout<<" " ;
while(d<e){
cout<<"*"; d++; } } else {
while(d<e){
cout<<"*"; d++; } cout<<" " ;
while(c<a){ cout<<"*"; c++; } }
cout<<"\n"; b--;
}
getch(); return 0; }
| |
|
| |
ammar26 Administrator
Number of posts : 18 Age : 34 Registration date : 2008-11-12
| Subject: Thanks Faizan ... 17/12/08, 05:19 am | |
| Thanks precious.logic for solution of practice questions in C | |
|
| |
brajraj Level 0
Number of posts : 1 Age : 50 Registration date : 2012-04-23
| Subject: Re: Loop practice questions in C and C++ 23/04/12, 06:47 am | |
| Practice Problem 1
[#include #include main() { int i=1,j=1,m=2; for(i=1;i<=6;i++) { for(j=1;j<=12;j++) { if(m==j) { cout<<"-"; } cout<<"*"; } m=m+2; cout<<"\n"; } getch(); } ] | |
|
| |
Sponsored content
| Subject: Re: Loop practice questions in C and C++ | |
| |
|
| |
| Loop practice questions in C and C++ | |
|